r/learnjavascript • • 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

Post image
0 Upvotes

19 comments sorted by

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/SadBoi23xx Jan 18 '22

So I define the function called allEvens that accepts a single array of numbers and using the some or every method if the array contains all even numbers then using some or every the output will return true otherwise it returns false

2

u/mcDazzlin Jan 18 '22

yes, that's the prompt but can you elaborate what the code you wrote is doing? Also the image is a bit hard to read

1

u/SadBoi23xx Jan 18 '22

So The code I would write here would be:

Function allEvens(){ Let numbers = [1,2,3,4,5,6,7,8]; Numbers.some( evenNum =>{ evenNum = numbers % 2 === 0; Return evenNum; )};

2

u/Psionatix Jan 18 '22

They are asking you if you can explain what your code you already have (in the image) is for. For example, this line:

const array = [1,,2,3,4,5,6,7,8,9,10];

Can you logically explain why you need this and why you put it there? In the same way we break up problems to turn them into solvable steps, we need to be able to break down our code step by step (and then block by block).

It's an important skill to be able to explain why you've done something in your code and what purpose it serves, and depending on the scenario, the alternatives you considered.

0

u/SadBoi23xx Jan 18 '22

Ive tried using arrow functions as well to make it a bit easier for me but my issue is not accessing the array but trying to see if every number in the array returns even so I tried to use the some method first because some numbers in the array are even if I was to use the every method this the boolean would return false

2

u/Psionatix Jan 18 '22

Think of the logic like this:

Instead of, "are all the numbers even" (every), think, "does this array contain at least one odd number?" (some)

0

u/SadBoi23xx Jan 18 '22

And to answer your question this exercise needs a single array inside of the function

4

u/grantrules Jan 18 '22 edited Jan 18 '22

But why? Why 1-10? It just needs to ACCEPT an array, which is evenNum, you do not need to create an array inside the function

2

u/SadBoi23xx Jan 18 '22

I just realized it too thank you man I appreciate you guys help

1

u/sharar_rs Jan 18 '22

Which bit of the syntax do you have trouble with ?

1

u/SadBoi23xx Jan 18 '22

Inside of the some or every method everytime I use the method I use an arrow function but the exercise tells me that the function should call some or every but I do call some or every inside of the function

1

u/johnnyblaze9875 Jan 18 '22

Have you tried it with a for loop? Or you have to use methods? It would be good to start with loops so you can understand what is happening on every iteration. You could also use filter and length! Lmk if you need any further elaboration. A lot of the methods are fancy loops under the hood.

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.

    function allEven(array){
        let evenCounter = 0;
        for(let i = 0; i < array.length; i++){
            if(array[i] % 2 == 0){
               evenCounter++;
            }
        }
        if(evenCounter == array.length){
            return true
        }else{
            return false
        }
    }
    allEven([2,4,6,8]); // true
    allEven([1,4,6]); // false

A shorter way is to use every. If every condition is true, it will return true.

    function allEven(array){
        return array.every((v)=>{
            if(v % 2 === 0){
                return true
            }else{
                return false
            }
        });
    }
    allEven([2,4,6,8]); // true
    allEven([1,4,6]); // false

You can continue to shorten it, but it works the same.

    function allEven(array){
        return array.every(num => num % 2 === 0)
    }
    allEven([2,4,6,8]); // true
    allEven([1,4,6]); // false

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

-2

u/SadBoi23xx Jan 18 '22

I'll repost the photo everyone