Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
The book that I'm studying says about iterating over arrays with every that:
The function these methods use must follow one ruleāit must accept
three arguments like the following code:
function functionName(value, index, array) {
// do something here
}
Does that mean that I must always use 3 arguments? If so then why does this code work?
var numbers = [ 1, 2, 2 ];
function isLessThan3(value) {
var returnValue = false;
if (value < 3) {
returnValue = true;
}
return returnValue; }
document.write(numbers.every(isLessThan3));
There is no limitation on how many arrguments you can put in a function with Javascript.
you have a very good explenation about this topic in the next answer by #Niet the Dark Absol
https://stackoverflow.com/a/22747272/1283672
i believe that the book was reffering to something more specific within it's scope.
And just to be clear you can put no arrgs in a function either.
It's a bit ugly, the code, you have, but there is help. You might use the following without a temporary variable. Just return the result of the comparison.
function allLessThan3(value) {
return value < 3;
}
var numbers = [1, 2, 2];
console.log(numbers.every(allLessThan3));
No, you can use from 0 to 3 arguments
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
handleChange(e) {
const index = Number(e.target.id.substring(e.target.id.length - 1, 19));
const copyFormArray = JSON.parse(JSON.stringify(this.state.educationData));
copyFormArray[index][e.target.name] = e.target.value;
this.setState({
educationData: copyFormArray,
});
};
What does this handleChange method do, specifically in the index variable? I understand what substring and the Number constructor do but the second argument I cannot find any answers for this specific question although it works as intended.
Substring works in a tricky way, if second argument (in your case 19) is less than first argument - then they will be "swapped", and if not - it will just take text between indexes (argument1 and argument2).
So say for example i have a string:
var x = "aaaaa-5";
if i will write:
x.substring(x.length-1)
It will give me "5".
Then if i will write:
x.substring(3,6)
It will give me "aa-"
But then if i will write:
x.substring(6,3)
It will be exactly the same as previous call - "aa-"
So internally logic of substring is kind of like this:
function substring(start,end) {
if(start > end) {
var buf = end;
end = start;
start = buf;
}
//... other logic
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Here is my code, it gives me undefined i have also tried indexof() method
let Numbers = [2,3,1,5,6,7,8 ];`
console.log("Unsorted array " + Numbers);
for(var i=0 ;i<Numbers.length;i++){`
alert(Numbers.findIndex[i]);
}
The below code would work after some changes in your code -
let Numbers = [2, 3, 1, 5, 6, 7, 8];
console.log("Unsorted array " + Numbers);
for (var i = 0; i < Numbers.length; i++) {
console.log(Numbers.indexOf(Numbers[i]));
}
In your case, you were using findIndex() which takes a function and executes it for each element of the array. You were passing it a number which is not correct. Also, the invocation of function you were doing was not correct - use () brackets and not [] brackets for function call.
Also, the i itself is the index. I don't know why you would need to use indexOf to get the index of element which you already know is present at a particular index. This method wouldn't be practical unless your array has duplicates and you need to find the first occurring index number for each element of array.
As a side tip, avoid using alert for such purposes. Stick with console log.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
Suppose my code is as follows:
const days = ['Monday', 'Tuesday'];
for (const day of days) {
console.log(day);
}
It prints out 'Monday' and 'Tuesday'.
If for whatever reason, days is undefined, then I get a runtime error.
I could handle this by putting in code like the following:
if (undefined == days) {
// logic here
}
To me, that seems a little clunky. Is there a way to have the for ... of loop ignore undefined values? Basically, treat it like a zero-length array and so the logic inside the for loop won't execute, but it won't throw an error either.
You could always use a forEach, and a logical or || within a parenthesis block.
(days||[]).forEach(day=>{console.log(day)});
I suppose you could also use this in your for loop.
for(let day of (days||[])) {
console.log(day);
}
The best way to check for a empth value is to initialize a variable before using it, Either is a string, object or array. This way the programm understand the variable and can easily suggest function for you.
//Initializing Varable
let stringVariable = '' ;
let objectVariable = {};
let arrayVariable = [] ;
In your case
let days = []
for(let day of (days)) {
console.log(day);
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I am trying no to use a loop as Im practicing map, filter etc. and wonder why my code doesn't work?
function checking(array,item){
var temporary=true;
array.map(function(x){
if (x!=item){
temporary=false;
}
});
return temporary;
}
checking([1, 2, 3], 2);
Why not use Array#some, it is perfect for checking and returnig a boolean value. And the best is the short circuit if the callback return is true.
function checking(array, item) {
return array.some(function (x) {
return x === item;
});
}
document.write(checking([1, 2, 3], 2));
Because you are not checking if 2 is in array, but you are checking if array has a different value than 2.
You should wether check if it there eg :
let temporary=false;
array.map(function(x){
if (x==item){
temporary=true;
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I want to do this
if ($(this).val() in (1,2,3) ) {
...
}
I have tried this and it works but It's not what I want
if ($(this).val()==1 || $(this).val()==2 || $(this).val()==3 ) {
...
}
I notice OP is using jQuery but this should work for anyone who isn't
if([1,2,3].indexOf(yourValue) > -1) {
...
}
Edit, Gah, beaten by Blazemonger by 20 seconds
Use an array and $.inArray():
if ($.inArray(parseInt($(this).val()), [1, 2, 3]) > -1) {
}
Attempting to synthesize information that is currently scattered in a number of different answers and comments... Your code is not working because of two things:
You are using in which checks for keys, not values in an array. Thus, for example, 3 in [1,2,3] will be false, since [1,2,3] has keys (indices) 0,1,2.
Quite possibly, your value is a string
The solution is to make sure that your value is of the right type, and to use the correct function. There are two obvious choices:
indexOf - returns the index of the (first) element that matches
inArray - is available if you are using jQuery
Sample code:
var v = parseInt($(this).val()); // radix 10
var r = [1,2,3]; // the values to check against
if(r.indexOf(v) > -1) { // method 1
if($.inArray(v, r)) { // method 2
if ([1,2,3].indexOf($(this).val()) > -1) {
or
for (i in [1,2,3])
if (i==$(this).val()) {
...
}