I am building a calculator for a class using AJAX. I am having issues with my if else statement not working. It is supposed to display an alert, telling the user that they cannot divide by zero except the alert never shows. I am a beginner to Ajax, if anyone could provide guidance, it'd be greatly appreciated - I know it's probably something simple that I'm overlooking. Below is my function:
...
}
function divFunc() {
var fn = "divide";
getResult(fn);
event.preventDefault();
return;
if (frmMain.txtBxSecNum.value == 0) {
$('#alert').text("You cannot divide by zero!");
}
else { (frmMain.txtBxFirstNum.value == 0)
$('#alert').text("You cannot divide by zero!");
}
}
...
Please remove 『return;』
This statement does not work because return is added above, so the following statement is not executed
Related
Trying to learn RxJs, and I found what looks like a nice tutorial on that topic at https://www.learnrxjs.io.
I'm going through their primer section, and I am not clear on what the return statement in the pipe() function actually does or what it means. These are a couple of screen shots from their tutorial:
In traditional programming, I've always understood a return statement to be an exit - if function A calls function B, and function B has the line return 1, then control goes back to function A.
Is that what happens here? If so, in either of these two examples, where am I returning to???
Or what if I don't want to return anywhere but act on the data immediately? For example, in the error handling example, instead of return makeRequest..., I want to do something like const result = makeRequest.... Can I do that?
In general I'm having some conceptual difficulties around all the returns I've seen used with observables, and any help in explaining what they do/are would be appreciated. So would any other tutorial sites on RxJs.
These are all very similar constructs in javascript
function adder0(a,b){
return a + b
}
const adder1 = (a,b) => {
return a + b
}
const adder2 = (a,b) => a + b
console.log(adder0(3,7)) // 10
console.log(adder1(3,7)) // 10
console.log(adder2(3,7)) // 10
lets re-write the code from that example but with explicit function definitions (instead of arrow syntax).
function maker(value){
return makeRequest(value).pipe(
catchError(handleError)
);
}
function actioner(value){
// take action
}
source.pipe(
mergeMap(maker)
).subscribe(actioner)
The thing to notice is that you never actually call these functions. RxJS does that for you. You give them a function and they'll call it when they're good and ready based on the spec for a given operator.
I am trying to understand the logic of promises here. But I can't wrap my head around the code.
Can someone help me understand?
What does (r)=> fn = r, means in this code.
function promise(a,b) {
return new Promise((resolve,reject) => {
if(a%2 !== 0) {
reject('ODD ');
return;
}
resolve('even');
});
}
let list_of_promise = [ promise(1),promise(2)
,promise(2),promise(2),promise(2),promise(2)
,promise(2),promise(2),promise(2),promise(2)
,promise(2),promise(2),promise(2),promise(2),promise(2)
,promise(2),promise(2),promise(2),promise(2)
,promise(2)];
function listOfPromise(list_of_promise) {
let arr = [];
let fn;
let count = 0;
list_of_promise.map( (p) => {
p.then((res)=>{
arr.push(res);
arr.length === 10 && fn(arr);
});
})
return new Promise((r,rj) => fn= r);
}
listOfPromise(list_of_promise).then((res)=>{
console.log("result set ", rest);
});
This is horrible code.
fn = r is just assigning the resolve() function from the new Promise() executor to a higher scope so it can be called from outside the executor, causing the promise to resolve. This is a very obtuse way to write the code. A design pattern for resolving a promise from the outside is typically referred to as a Deferred. 99.999% of the time, there is no need for the Deferred pattern and there's a bunch of good reasons why it was not built into the promise architecture. If you want to see a simple Deferred object, you can see here and here.
To rewrite this in a less obtuse way, I would need to understand what the objective of the code is (in terms of a real-world problem) so I could suggest the best way to solve the actual problem. Right now, it looks like demo code trying to demonstrate something, not trying to solve a real world problem. I prefer to focus coding on real world problems rather than theoretical discussions as the real world problem provides priorities for the actual coding strategy.
In the block of Code below, String(quakeTsunamiWarning) isn't returning with any response, and is giving the error Use of Unresolved Identifier 'quakeTsunamiWarning'
I'd like to apologise in advance if something is obviously wrong or blatantly obvious, but i'm still rather new when it comes to Programming with Swift. (Going off free tutorials I can find on Google here, as I don't have the funds to pay for a course or paid tutorial)
var quakeTsunami = 0
func newQuake() -> Void {
if quakeTsunami == 0 {
var quakeTsunamiWarning = "False"
} else if quakeTsunami == 1 {
var quakeTsunamiWarning = "True"
} else {
var quakeTsunamiWarning = "N/A"
}
println(String(quakeTsunamiWarning))
}
I've removed all the other code from the example, for sake of keeping this post relatively small, but here's a link to a gist if you want the full document. https://gist.github.com/kurisubrooks/0ba9f7547ee960657dec
Thanks in advance!
You have three separate quakeTsunamiWarning variables, each only visible inside the innermost containing {}. Move it outside the if/else structure:
var quakeTsunamiWarning: String
if quakeTsunami == 0 {
quakeTsunamiWarning = "False"
} …
And you don't need to convert it to String since it already is a String.
I know how to make the cache cleared for view :
.state('app.list', {
cache : false,
url: "/lists/:listId",
views: {
'menuContent': {
templateUrl: "templates/listDashboard.html",
controller: 'listDashboardCtrl'
}
}
})
, but I need something else - delete all the cache for the app in controller method. how to do it?
I found a solution, Wrap the clearCache and ClearHistory in a $timeout. Something Like this.
$scope.logout = function(){
$location.path('/signin')
$timeout(function () {
$ionicHistory.clearCache();
$ionicHistory.clearHistory();
$log.debug('clearing cache')
},300)
}
Edit:Changed Timeout seconds
You can use $ionicHistory. From documentation:
clearCache()
Removes all cached views within every ionNavView. This both removes the view element from the DOM, and destroy it's scope.
In your listDashboardCtrl write this:
function listDashboardCtrl($scope, $ionicHistory){
$ionicHistory.clearCache();
}
Well this is an old issue, but for anyone that's coming 2017 or later I will explain what really happens and how to solve it:
The code of $ionicHistory.clearCache():
clearCache: function(stateIds) { return $timeout(function() {
$ionicNavViewDelegate._instances.forEach(function(instance) {
instance.clearCache(stateIds); }); }); }
So, as you can see, it takes 1 parameter cllaed stateIds which is an array of stateId. Indeed i struggled to find out that stateId is nothing more than stateName.
So, let's go deeper. The code of $ionicNavView.clearCache which is used in the line above "instance.clearCache(stateIds)" is:
self.clearCache = function(stateIds) {
var viewElements = $element.children();
var viewElement, viewScope, x, l, y, eleIdentifier;
for (x = 0, l = viewElements.length; x < l; x++) {
viewElement = viewElements.eq(x);
if (stateIds) {
eleIdentifier = viewElement.data(DATA_ELE_IDENTIFIER);
for (y = 0; y < stateIds.length; y++) {
if (eleIdentifier === stateIds[y]) {
$ionicViewSwitcher.destroyViewEle(viewElement);
}
}
continue;
}
if (navViewAttr(viewElement) == VIEW_STATUS_CACHED) {
$ionicViewSwitcher.destroyViewEle(viewElement);
} else if (navViewAttr(viewElement) == VIEW_STATUS_ACTIVE) {
viewScope = viewElement.scope();
viewScope && viewScope.$broadcast('$ionicView.clearCache');
}
}
};
And as you can see in the code, this clearCache DOES NOT CLEAR ALL CACHES, instead, it destroy all cached views that matches a value in the stateIds array. If there's no parameter IT JUST DESTROY THE ACTUAL VIEW.
So the solution for this, using just the Ionic way is to call $ionicHistory.clearCache() with all your state names in an array as parameter.
E.g:
$ionicHistory.clearCache(['login', 'map', 'home']);
I cannot belive any Ionic developer didnt dug into the code before, or missed this simple datail.
I Hope someone takes advantage of this, even being so late.
UPDATE: Just to make it crystal clear, i want to point out where the bug itself is (if we can call it bug), maybe can be handy for devs:
self.clearCache = function(stateIds){
[...]
var viewElements = $element.children();
}
What the whole function does is basically:
Get all elements using JQLite
Loop the elements
Check if an element equals one in the StateIds array and destroy it; go to next element.
Check if element in the loop is cached or active, and in both true cases destroy it
I wont dig deeper into this but debugging it i could see that the elements gotten from var viewElements = $element.children(); is not an array of all your views content, not even the cached ones, intentionally or not it does not loop through out all your states to clear all those that matches 'ACTIVE' or 'CACHED'. If you want it to loop through ALL your states and destroy all cached views and data you need to explicity pass the stateIds array parameter.
Besides there's another strange behavior, because when i was debugging it i saw when the var viewElements array was filled up with 2 elements, and these 2 elements were from the same state, one resolved to 'CACHED' another resolver to 'ACTIVE', even resolving to the 2 types used in the if conditions the cache was not cleared at all.
I personally think that this is somekind wrong implemented or is wide used wrongly. The fact is that there's a lot of people cracking their heads on this and devs don't even give this simple explanation.
I'm trying to intercept events using Gdk.Window.AddFilter(Gdk.FilterFunc) in Mono. So far, I have been able to hook up the filter function, but now I am trying to use the events in the filter function.
This is what I have in the filter function so far:
private Gdk.FilterReturn FilterFunction(IntPtr xEvent, Gdk.Event evnt)
{
if (evnt.Type == Gdk.EventType.KeyPress)
{
Gdk.EventKey eventKey = (Gdk.EventKey)evnt; // fails here
if (eventKey.Key == this.key && eventKey.State == this.modifiers)
{
this.OnPressed(EventArgs.Empty);
}
}
return Gdk.FilterReturn.Continue;
}
How can I convert the Gdk.Event to Gdk.EventKey? I have tried casting it, but that doesn't seem to work.
Edit: Oops! The problem was that I had accidentally added a semicolon to the if statement, making it an empty statement. For some reason, the Gdk.Event does not correspond to the XEvent, so I am now pursuing a solution that uses the XEvent instead.
Why don't you try printing out the type so you can see what it really is? (it may not be EventKey)
Like:
Console.WriteLine (evnt.GetType ());
(or pause it in a debugger and examine it to see the type)