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;
}
}
Related
This question already has answers here:
Javascript compare 3 values
(7 answers)
Closed 2 years ago.
I am working on studying for an entry test, and being self learned I have been working a lot of functions problems. this one has stumped me,
I a to write a function testing to see if 3 values are equal. The code i have tired is:
Function equal(a,b,c){
return a==b==c;
}
as well as:
function equal(a,b,c){
let newEqual=a==b;
return newEqual===c
}
I feel like I am missing something rather simple but have not been able to put my finger on it.
thank you in advance for any insight
a == b == c will be evaluated as :
a == b then checks the result ( true ) and compares it with c => true == c which is false :
const a = 5;
const b = 5;
const c = 5;
const result = a == b == c ;
console.log(result); // false
const a1 = 5;
const b1 = 5;
const c1 = true;
const result1 = a1 == b1 == c1 ;
console.log(result1); // true
You should compare them separately :
const a = 5;
const b = 5;
const c = 5;
const result = a == b && b == c ;
console.log(result);
To check whether all three variables are equal or not, use && operator to add on queries.
&& returns true if all conditions are true.
function equal(a,b,c){
return a == b && b == c;
}
As mentioned by #mplungjan, for strict comparison use === instead of ==.
Try this.
function equal(a,b,c){
return a==b && b==c;
}
You can check it by doing the following:
function equal(a,b,c){
return (a==b) && (b==c)
}
That way you are checking if a == b is true, and b == c is true then all three are equal. In other words, true && true = true
This question already has answers here:
How does this recursion work?
(11 answers)
Closed 2 years ago.
I'm not trying to get or want anything from this code. I just have a confusion with a part of the following code. The author has returned a value with or || operator. my question is which value will be returned and when?
function findSolution (target) {
function find (current, history) {
if (current == target) {
return history
}else if (current > target) {
return null
} else {
return find (current + 5, `${history} + 5)`) ||
find(current * 3, `(${history} * 3)`)
}
}
return find(1, '1')
}
console.log(findSolution(24))
here is the part of the code is bothering me
return find (current + 5, `${history} + 5)`) ||
find(current * 3, `(${history} * 3)`)
can anyone please tell me, which value when return? and why?
From MDN:
If expr1 can be converted to true, returns expr1; else, returns expr2.
in other words, you can see it as a function like this:
const or = (fel, sel) =>{
if(fel) return fel;
return sel;
}
That's why this happens:
const x = 0 || 1; // x == 1
// same as:
//
// const or = (fel, sel) =>{
// if(fel) return fel;
// return sel;
// }
// const x = or(0,1)
This question already has answers here:
JavaScript Fibonacci breakdown
(3 answers)
Closed 3 years ago.
I tried playing around with recursion in javascript. I wrote a function for the Fibonacci sequence. However, if only works if the argument is 0
fib = (x)=>{
if (x == 0 || x ==1) {
return 1
} else {
return fib(x-1) + fib(x+1)
}
}
It returns 1 for 1, but a number above 0 I get the error maximum call stack size exceeded
This is not the Fibonacci sequence. This is the Fibonacci sequence:
fib = (x) => {
if (x == 0 || x == 1) {
return 1;
} else {
return fib(x - 1) + fib(x - 2);
}
}
for (let i = 0; i < 10; i++) {
console.log(fib(i));
}
In its simplest form, of course. If you go too far beyond 10, you'll experience what an exponential cost of computation can do to a computer.
You need the value of the second last iteration, no an iteration ahead.
Please have a look here, too: Fibonacci number.
const fib = x => {
if (x === 0 || x === 1) {
return 1;
}
return fib(x - 2) + fib(x - 1);
};
console.log(fib(7));
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);
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?