Order of execution - can I control it? [duplicate] - javascript

This question already has answers here:
How can I control Javascript execution order?
(2 answers)
How do I return the response from an asynchronous call?
(41 answers)
Closed 8 years ago.
Its an ASP.Net application, and this is part of a javascript function:
//x & y are global variables
x = document.getElementById(MapXVal).value;
y = document.getElementById(MapYVal).value;
if ((x === undefined || x === null || x === "") &&
(XVal !== undefined && XVal !== null && XVal !== "")) {
x = document.getElementById(XVal).value;
y = document.getElementById(YVal).value;
point = new esri.geometry.Point(x, y, new esri.SpatialReference({ "wkt": ...}));
var OutSR = new esri.SpatialReference({ "wkid": 102113 });
gsvc.project([point], OutSR, function (projectedPoints) {
var output = projectedPoints[0];
alert('deep inside');
x = output.x;
y = output.y;
});
}
alert('outer scope');
if (x !== null && y !== null && x !== "" && y !== "") {
//do something with x & y
}
What I am trying to do: call the function gsvc.project...etc to calcuate the values for my global variables x & y which will be used later on in the code.
Problem: the code within gsvc.project will be executed after its containing function is done executing (e.g. the message outer scope will be shown before deep inside) so I will not have the values for x & y until its too late.
I am not an expert in Javascript, so I've been looking for why this is happening and found nothing much up to this point. I already solved the problem by duplicating my code inside the gsvc.project..function(projectedPoints){ declaration, but I dont want to have duplicates and would like to know if there is a solution to my problem: control the execution, cause a delay, or maybe a better way to do this?

Related

"arguments" keyword in JavaScript

const add = (x=5, y=10) => console.log(x+y);
After we run the transpiler on this code, here is what the output would look like:
"use strict";
var add = function add() {
var x = arguments.length <= 0 || arguments[0] === undefined ?
5 : arguments[0];
var y = arguments.length <= 1 || arguments[1] === undefined ?
10 : arguments[1];
return console.log(x + y);
};
I got this snippet from Learning react book.
I have two question here
Can arguments.length be negative?
Does checking the second "||" condition be sufficient to check whether arguments[0] or arguments[1] is undefined?
Can arguments.length be negative?
No. How could you call a function and put a negative number of things between ( and )?!
Does checking the second condition be sufficient?
No. The function might be called with only one argument.

javascript .map(x => x && y); why result y [duplicate]

This question already has answers here:
The logical && and || operators in JavaScript
(2 answers)
Closed 4 years ago.
This code is result why [5,5,5,5] ? "&&" what is doing in code ? Could you light me up
var array1 = [1, 4, 9, 16];
const map1 = array1.map(x => x && 5);
console.log(map1);
In JavaScript, if x is truthy, then x && y will return y.
Your array contains numeric values that are all truthy. If one of your values was falsy, like 0 then x && y would return x instead - give it a try!
Learn more on MDN...
About truthy (links to falsey)
About the logical shortcut operators
x => x && 5 basically behaves the same as:
x => {
if (x) {
return 5;
} else {
return x;
}
}

Chained logical OR comparison [duplicate]

This question already has answers here:
Javascript if statement with multiple permissible conditions [duplicate]
(4 answers)
Closed 5 years ago.
Why doesn't this work:
if (x != (a || b || c)) {
doStuff();
}
It's meant to check whether x is NOT equal to a OR b OR c.
EDIT: How would I achieve a check for whether x is NOT equal to a OR b OR c?
EDIT: Ok, it's a duplicate. What do I do now, take minus points even after realizing my mistake? :P
To use multiples values like you wanna just:
var x = 'x';
var a = 'a';
var b = 'b';
var c = 'c';
function doStuff() {
console.log(1)
}
// exemple 1
if(x == a || x == b || x == c) {
doStuff();
}
function isSameValue(element, index, array) {
return element === x;
}
// exemple 2
if([a, b, c].some(isSameValue)) {
doStuff();
}
// exemple 3
[a, b, c].includes(x);

If then logic with && ||

Can someone explain to me or point me to documentation as to why the following function doesn't work?
var x = 1;
var y = 2;
var z = 1;
function logicTest() {
if ((x && y && z) === 1) {
return true;
} else {
return false;
}
}
console.log(logicTest())
I know that I can type it out the long way as follows:
var x = 1;
var y = 2;
var z = 1;
function logicTest() {
if (x === 1 && y === 1 && z === 1) {
return true;
} else {
return false;
}
}
console.log(logicTest())
But I'm really trying to understand why the first doesn't work and also if there is a better way of typing the second if/then statement or if that is just the way it will always have to be.
Thanks!
The expression
((x && y && z) === 1)
first involves the evaluation of (x && y && z). To evaluate that, JavaScript tests, in sequence, the values of x, y, and z. If, left to right, one of those values when coerced to boolean is false, evaluation stops with that (uncoerced) value as the value of the whole thing. Otherwise, the value of that subexpression will be the value of z, because it's the last subexpression in the && sequence.
In this case, x, y, and z are all non-zero numbers, so the overall result will be 1, because z is 1.
What you seem to want to be able to do is test whether all of a set of subexpressions are equal to the same value. That, as you've found, can only be determined by explicit comparison. It's also something that could be done by creating a list and then using array functions to perform the tests, which would be useful when there are more than just three subexpressions to test.
Also, on a stylistic note:
function logicTest() {
if (x === 1 && y === 1 && z === 1) {
return true;
} else {
return false;
}
}
Performing tests with relational operators like === generates boolean values. It's more concise to take advantage of that:
function logicTest() {
return x === 1 && y === 1 && z === 1;
}

Jasmine toThrow() syntax

I have an activity I am doing for an apprenticeship opportunity and the main engineer that I could discuss this with is not responding so I figure I ask on here.
Basically this is a 'triangle' exercise with the main purpose of writing in TDD with jasmine.
Now, I have written the simple ones and when I moved to writing the edge cases, I am having trouble with throwing invalid input. Looking at the Docs it seems simple but my test fails with invalid thrown as error
describe('Triangle', function(){
var Triangle = function(x, y, z){
if (x <= 0 || y <= 0 || z <= 0) {
throw 'invalid';}
if(x === y && y === z){
return 'equilateral';}
if(x === y || x === z || y === z){
return 'isosceles';}
else{return 'scalene';}
};
it('test triangles with no size are illegal', function() {
var triangle = Triangle(0,0,0);
expect(triangle).toThrow();
});
});
The problem is that you are actually calling the function here:
var triangle = Triangle(0,0,0);
Instead, you should let expect() call it by providing a callable:
expect(function() { Triangle(0, 0, 0); }).toThrow();

Categories