Why doesnt this .every() method work here? [closed] - javascript

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 1 year ago.
Improve this question
const allEvens = (arr) =>allEvens.every(item => return item %2 ===0)
}
Hey, so im trying here to check if every number is even, and if its not it should return false. But now Ive spent almost 2 hours trying to understand why this doesnt work for me, trying to change some things but none of them worked. Can someone give me any clue where's the problem here ? Thank you very much!

You have to remove the return keyword to make it return (item % 2 === 0) expression. But if you want to keep that return, you have to put the body of function inside curly braces.

When you omit "{...}" from the body of an arrow function, it automatically indicates that you want to directly return the content (the result of item % 2 === 0 in this case). So the following should help solve the issue:
const allEvens = (arr) => allEvens.every((item) => item % 2 === 0);
console.log(allEvens([12, 6, 8])) // true
console.log(allEvens([12, 6, 8, 1])) // false

Related

What is this handleChange method doing? [closed]

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
}

Iterating over an array with `every` [closed]

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

using map to check for true or false doesn't work [closed]

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;
}

Simple Javascript validation function [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I do not have any JavaScript experience and would like some assistance in creating a function as I am not sure on how to do it.
I would like to create a Function that validates that x between 1 and 17 it can also be equal to 1 and 17
If the value is not valid a simple messagebox/alert could be used to notify the user
I know this is a really stupid question but thank you in advance
A function like that would look something like:
function validateNumber(n) {
var ok = n >= 1 && n <= 17;
if (!ok) alert('The value is not valid. Values from 1 to 17 are allowed.');
return ok;
}
You can call it, and you get the status back if you want to use it for something:
if (validateNumber(x)) {
// the number was valid
} else {
// the number wasn't valid
}

I want to put into variables multiple value [closed]

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()) {
...
}

Categories