how is undefined treated while looping [duplicate] - javascript

This question already has answers here:
Why does JavaScript map function return undefined?
(13 answers)
Removing undefined values from Array
(14 answers)
Closed 2 years ago.
[1, 3, 4, 5, 5, undefined, 4, 3, 4].map((item) => {
if (item) {
return item
}
})
The output of the above code is
Why is undefined also returned, shouldn't it be stopped by the if loop?

Your map function doesn't return anything explicitly if the item is falsy, hence it returns undefined (per default).
As another comment has pointed out, you are probably looking for Array.prototype.filter:
console.log([1,3,4,5,5,undefined,4,3,4].filter((item)=>{
if(item){
return item;
}
}));
Note that the above is slightly verbose, .filter(x => x) would suffice, filter calls ToBoolean on the return anyways. It will still filter out any falsy values though, e.g. 0.

Related

Assigning value to an array or object inside a function [duplicate]

This question already has answers here:
Is JavaScript a pass-by-reference or pass-by-value language?
(33 answers)
What is the scope of variables in JavaScript?
(27 answers)
Closed 1 year ago.
I am learning javascript and I read that in javascript array and objects are by default passed as a reference. So when I do this:-
var a = [2, 3, 4]
function pushEl(a, num) {
a.push(num)
}
pushEl(a, 5)
console.log(a)
The output is as expected which is
[2,3,4,5]
But what I am not able to understand is that when I assign value to an array or an object inside the function the original array/object is not changed.
var a = [2, 1, 3]
function change(a) {
a = [1, 2]
}
change(a)
console.log(a)
I expect the output to be [1,2] but the output is [2,1,3].
If the array is passed by reference then the changes should have been reflected in the original array too.
Can anybody tell me what concept I'm missing here?

Understanding this JS syntax [duplicate]

This question already has answers here:
How do JavaScript closures work?
(86 answers)
Higher-order functions in Javascript
(5 answers)
Understanding Higher Order functions in Javascript
(2 answers)
Closed 3 years ago.
I am practising ES6 and I have this code:
const over = (...fns) => (...args) =>
fns.map(fn => fn.apply(null, args));
const minMax = over(Math.min, Math.max);
console.log(minMax(1, 2, 3, 4, 5));
console.log(minMax(1, 2, 5, 4, 3));
console.log(minMax(1, 2, 5, -4, 3));
The goal is to get the minimum and the maximun values between the numbers passed as arguments.
I could understand almost everything, the dynamic is very clear, with one exception, I know that args refers to the parameters coming from minMax(), but I couldn't get how the code recognize it.
My guess is: since we have two functions, over() and minMax(), when called, they are automatically read in this order, that is why the code knows that the first anonymous function refers to over() and the second one to minMax(). But this is just a guess, I don't know if I am right.
What is exactly happening here?

Can I capture the return value of a 'for' statement in a variable without using 'eval'? [duplicate]

This question already has answers here:
Javascript while loop return value
(3 answers)
Closed 4 years ago.
Javascript's for statement returns undefined, at least when I use it in Chromium's JS repl:
> for (i=0;i<1;i++);
: undefined
Therefore, I would expect the following statement to interpret thusly:
> a = for (i=0;i<1;i++);
: undefined
Instead I get
a = for (i=0;i<1;i++);
VM488:1 Uncaught SyntaxError: Unexpected token for
The only workaround I can think of is a = eval("for (i=0;i<1;i++);"), which does work. However, as my question states, I want to do this without using eval. Is it possible?
the for statement allowed you to iterate over a collection of items, which mean will be executing an instruction between the body of the for an example:
for (i=0;i<1;i++){
//this is the body
console.log(i);
}
the for statement doesn't return a value, what you are seeing is the dev chrome tools returning undefined.
read more https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for
what you might want to do is:
var myValues = []; // an array
for (i=0;i<5;i++){
values.push(i)
}
console.log(values); // [0, 1, 2, 3, 4]

Math.min give NaN [duplicate]

This question already has answers here:
Why does Math.min([1,2]) return NaN?
(3 answers)
Closed 5 years ago.
I am creating array using $.map as below
var correctScores = $('[id^="dvAns_"] input[type="text"]').map(function () {
return $(this).val().trim() == "" ? 0 : parseFloat($(this).val());
}).get();
it gives me array as [5, 0]
when I am writing Math.min(correctScores) it returns NaN instead of 0.
Why so?
You will have to use apply to be able to use Math.min() in connection with an array. In its native form it expects individual arguments.
See here: Math.min.apply(0, array) - why?
It needs to be out of array, like this;
Math.min(correctScores[0],correctScores[1]); // 0

how to simplify this statement using indexOf? [duplicate]

This question already has answers here:
Determine whether an array contains a value [duplicate]
(18 answers)
Closed 7 years ago.
How can I simplfy the following text inside the if statement in Javascript using "indexof"?
if (a === 1 || a === 2 || a === 3) {
return "correct";
};
I am guessing an array needs to be made for 1,2, and 3, but am unsure of how to us instanceof after that
*edited to say indexOf instead of instanceOf
The instanceof operator tests whether an object has in its prototype chain the prototype property of a constructor.
In your case, instanceof wont help you. You can use indexOf() with array as follow.
var arr = [1, 2, 3];
// Check if a is present in the arr array
if (arr.indexOf(a) > -1) {
return "correct";
}

Categories

Resources