Https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-algorithm-scripting/finders-keepers

Hi you awesome wizards from the internet, I am no understanding the resolution of this function,


function findElement(arr, func) {
  let num = 0;

  for (var i = 0; i < arr.length; i++) {
    num = arr[i];
    if (func(num)) {
      return num;
    }
  }

  return undefined;
}

console.log(findElement([1, 3, 5, 8, 9, 10], function(num) { return num % 2 === 0; }))

this returns 8 in the console

can anyone tell me what is that { return num % 2 === 0; })) is doing for it to return an 8? console.log(findElement([1, 3, 5, 8, 9, 10], function(num) { return num % 2 === 0; }))

and why when its like this: console.log( findElement([1, 3, 5, 9], function(num) { return num % 2 === 0; })) it returns undefined

i dont get the num % 2 === 0;

thanks electric wizards

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.

markdown_Forums

% is the modulusreminder operator
you can review this lesson for that: https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/finding-a-remainder-in-javascript

Because in your loop you have

num = arr[i];
    if (func(num)) {
      return num;
    }

8 is the first number in the array that is divisible by 2.

Because you put this after the loop.

  return undefined;

None of the numbers in that array are divisible by 2.

How did you expect your logic to work when you wrote it? In what way is it behaving differently than expected?

thanks, i wasnt aware that %2 === 0 meant that it would check for every number until the remainder was 0, sorry i think i should have googled the term, but i thank you for the explanation, is just so difficult to get this symbols in my head sometimes, i feel like they just stay for a while and then i go back looking for tem in my brain and they are not there, sometimes i question myself if a good programmer knows by heart all this symbols meanings, or they are just very fast google researchers jaja

thanks, i wasnt aware that %2 === 0 meant that it would check for every number until the remainder was 0, sorry i think i should have googled the term, but i thank you for the explanation, is just so difficult to get this symbols in my head sometimes, i feel like they just stay for a while and then i go back looking for tem in my brain and they are not there, sometimes i question myself if a good programmer knows by heart all this symbols meanings, or they are just very fast google researchers jaja

That’s not what %2 === 0 does. It returns true if the remainder of the number divided by 2 is zero. The loop is why it checks every number. Have you done the early JavaScript sections that explain the % operator and loops?

We are very fast and efficient Googlers and we have learned how to read the results that we get, but we can read a lot of code without Google because we have written so much of it.

2 Likes

no, that is not what it does. The modulusreminder operator gives the reminder of a single division
the rest of the stuff is the loop logic plus the function passed in as argument

the function function (num) {return num % 2 === 0} is just a test that returns true or false
everything else is done by the loop logic

the function is called on each element of the array using func(num) and then if it returns true the return num line is executed

1 Like

Not to split hairs, but technically it is a remainder, not modulo (in JS and other languages at least).

Links:
https://2ality.com/2019/08/remainder-vs-modulo.html
https://exploringjs.com/deep-js/ch_remainder-vs-modulo.html

1 Like

yup ive done them, i guess i have not digested them, by heart

The first value in the array where are[i] % 2 === 0 is true causes the function to break out of the passed in function. Since that first value is 8 in your passed in array, that’s why you get 8.