Basic Algorithm Scripting: Finders Keepers using Simple If else

Tell us what’s happening:

emphasized text

I am trying to complete the challenge Finders Keepers but when I am trying to print the vale of the result (i.e. the number divisible by 0). it gives me 10 and when I debugged it , it gave me the following values:

result:undefined
finders-keepers.js:11 num:
finders-keepers.js:14 arr.length inside for loop: 6
finders-keepers.js:17 num[i] inside for loop:1
finders-keepers.js:18 arr[i] inside for loop:1
finders-keepers.js:14 arr.length inside for loop: 6
finders-keepers.js:17 num[i] inside for loop:3
finders-keepers.js:18 arr[i] inside for loop:3
finders-keepers.js:20 num => num % 2 === 0
finders-keepers.js:22 num => num % 2 === 0
finders-keepers.js:24 num[i] inside for loop inside if loop: 3
finders-keepers.js:14 arr.length inside for loop: 6
finders-keepers.js:17 num[i] inside for loop:5
finders-keepers.js:18 arr[i] inside for loop:5
finders-keepers.js:20 num => num % 2 === 0
finders-keepers.js:22 num => num % 2 === 0
finders-keepers.js:24 num[i] inside for loop inside if loop: 5
finders-keepers.js:14 arr.length inside for loop: 6
finders-keepers.js:17 num[i] inside for loop:8
finders-keepers.js:18 arr[i] inside for loop:8
finders-keepers.js:20 num => num % 2 === 0
finders-keepers.js:22 num => num % 2 === 0
finders-keepers.js:24 num[i] inside for loop inside if loop: 8
finders-keepers.js:14 arr.length inside for loop: 6
finders-keepers.js:17 num[i] inside for loop:9
finders-keepers.js:18 arr[i] inside for loop:9
finders-keepers.js:20 num => num % 2 === 0
finders-keepers.js:22 num => num % 2 === 0
finders-keepers.js:24 num[i] inside for loop inside if loop: 9
finders-keepers.js:14 arr.length inside for loop: 6
finders-keepers.js:17 num[i] inside for loop:10
finders-keepers.js:18 arr[i] inside for loop:10
finders-keepers.js:20 num => num % 2 === 0
finders-keepers.js:22 num => num % 2 === 0
finders-keepers.js:24 num[i] inside for loop inside if loop: 10
finders-keepers.js:32 result:10

The thing is the loop (if (func) ) is not working, it just prints all the elements in the array and not the one which is divisible by 2. Why is that so? Can anybody suggest something?

Your code so far


// Your code here!

// Basic Algorithm Scripting: Finders Keepers
// var arr =[1, 3, 5, 9];


function findElement(arr, func) {
 var result;
 var num = [];
 console.log("result:" + result)
 console.log("num:" + num)
 for (var i = 0; i < arr.length; i++) {
  // console.log("i inside for loop: " + i)
  console.log("arr.length inside for loop: " + arr.length)

  num[i] = arr[i];
  console.log("num[i] inside for loop:" + num[i])
  console.log("arr[i] inside for loop:" + arr[i])
  if (num[i] != 1) {
   console.log(func)
   if ( (func)) { // problem is here.
   	console.log(func)
   	// console.log(valueOf(func))
   	console.log("num[i] inside for loop inside if loop: " + num[i])
    result = num[i]
   } else {
    result = "undefined";
   }
 }

}
  console.log("result:" + result)
}

findElement([1, 3, 5, 8, 9, 10], num => num % 2 === 0);


Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36.

Challenge: Finders Keepers

Link to the challenge:



**Your browser information:**

User Agent is: <code>Mozilla/5function findElement(arr, func) {
let num = 0;
return num;
}

findElement([1, 2, 3, 4], num => num % 2 === 0);
.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36</code>.

**Challenge:** Finders Keepers

**Link to the challenge:**
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-algorithm-scripting/finders-keepers

the paramater func in findElement is “just” a function declaration.

You have to call the function if you “want to use it”.

So when you write this

if ( (func)) {
console.log(func)
}

You are just checking that the argument func is defined and logging the function.

I imagine you want to know the result of the computation of that function, so you need to call func with the appropriate argument:
Since you’re passing func as num => num % 2 === 0

if( func(8) ) { // we are calling func with 8 as argument
 console.log('yep, 8 is divisible by 2')
}

Hope this helps :+1:

1 Like

Yes, that helped a lot. In fact, it solved the challenge. @Marmiz Thanks for helping me out. Here is the solution of the challenge(pls ignore the comments):

// Your code here!

// Basic Algorithm Scripting: Finders Keepers
// var arr =[1, 3, 5, 9];


function findElement(arr, func) {
 var result;
 var num = [];
 // console.log("result:" + result)
 // console.log("num:" + num)
 for (var i = 0; i < arr.length; i++) {
  // console.log("i inside for loop: " + i)
  // console.log("arr.length inside for loop: " + arr.length)

  num[i] = arr[i];
  // console.log("num[i] inside for loop:" + num[i])
  // console.log("arr[i] inside for loop:" + arr[i])
  if (num[i] != 1) {
   // console.log(func)
   if ( (func(arr[i]))) { // problem is here.
   	// console.log('yep, num[i] is divisible by 2: ' + num[i])
    result= num[i];
    break;
   	// console.log(valueOf(func))
   	// console.log("num[i] inside for loop inside if loop: " + num[i])
    // result = num[i]
   } else {
    // console.log("undefined")
    result="undefined"
   }
 }

}
  // console.log("result:" + result)
  return result;
}

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