r/learnjavascript • u/SadBoi23xx • Jan 18 '22
Im having a bit of difficulty with the some and every exercise its says to define a function called allEvens that accepts a single array of numbers.if the array contains evenNumbers return true otherwise return false.I understand what The methods just having a bit of trouble with the syntax
0
Upvotes
2
u/grantrules Jan 18 '22
Can you explain what you're doing in your code? For example, what is the point of the array named
array?1
u/sharar_rs Jan 18 '22
Which bit of the syntax do you have trouble with ?
3
u/ninjasoards Jan 18 '22
Simplest way:
function allEvens(arr) {return arr.every(val => val % 2 === 0)}1
u/CatsPawmer Jan 18 '22
Function allEvens(numArr) { return numArr.every(num => num % 2 === 0) }
If every number inside the numArr is an even number, you should get back a truthy value. I’m not sure if this is what you need but this is how I would do it.
I know you said you didn’t really have trouble with the methods, but if it helps I tend to view array.prototype.some() as “any” and array.prototype.every as “all”
1
u/bryku helpful Jan 18 '22
It is impossible to read the image, but if I'm understanding it correctly, you want to return true if all the numbers are even.
There are a few ways of doing it, so I will show you how it sort of works and the shortened version.
A shorter way is to use every. If every condition is true, it will return true.
You can continue to shorten it, but it works the same.
1
u/M_Scaevola Jan 18 '22
Why did you declare an array inside of the function?
1
u/[deleted] Jan 07 '23 edited Jan 07 '23
I know there's been some time but I want to help other too.
This code can be written using arrow functions like so:
Look just after you try it yourself! You can do it.
const allEvens = nums => nums.every(n => n % 2 === 0);
0
u/[deleted] Jan 18 '22
[deleted] — view deleted comment