Build a First Element Finder - Build a First Element Finder

Tell us what’s happening:

I am having a hard time understanding what the ‘Build a First Element Finder’ means by using a test function. I should have two parameters for the findElement() function, one being an array and another being a function. I do not declare the function in my own code since the test cases will pass it on their end. I am confused about whether I should be calling the function from within the one I declared and assigning its return value to a variable. The prompt is rather vague and I tried to read over the test cases, but they all measure vastly different operations which leaves me confused. The main idea is a “truth test”. Basically return the first item in the array that passes that “truth test”.

Also, I was wondering since a function’s returned value is being assigned to a variable such as funcVal, would it need closed parentheses when referencing that variable? I recently read over the module about closures which details memory and private variables. A variable being assigned a function’s returned value and whenever that variable is used it uses closed parentheses.

Your code so far

function findElement(arr, num) {
  const funcVal = num();

  for (element of arr) {
    if (element === funcVal) {
      return element;
    }
  }

  // No return statement since default behavior returns 'undefined'.
}


Test Case Example:

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

Challenge Information:

Build a First Element Finder - Build a First Element Finder

A quick add–on to detail the revisions I made to my code.

function findElement(arr, num) {

  let funcVal;

  for (element of arr) {

    funcVal = num(element);

    if (funcVal === true) {

      return element;

    }

  }

  // No return statement since default behavior returns 'undefined'.

}

This still fails the test cases, but I suppose I’m thinking of it in a way where I pass the current element of the array to the function. It returns either true or false and I assign that return value to the funcVal variable. Afterwards, I see if it is equal to true and if it is I return that element. Otherwise the function returns undefined.

Yes; focus on this.

Note in the tests that the function passed to findElement takes a num parameter.

1 Like

Yes, I believe I’m heading in the right direction…maybe. I added a comment not long after the initial post where I made the following changes.

function findElement(arr, num) {

let funcVal;

for (element of arr) {

funcVal = num(element);

if (funcVal === true) {

  return element;

}

}

// No return statement since default behavior returns ‘undefined’.

}

However, I’m still encountering many failed test cases. In this revised code, I am passing an argument to that function which would be the current element being iterated over in the array.

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

When you run this test, what do you see in the console?

1 Like

Oh, that’s such a simple mistake…I keep making it actually. I didn’t define the variable in the for…of loop. I ran the console.log() and it said that element was undefined then I defined it (not trying to paint what the answer looks like as to not break any rules).

Thank you for bringing that to my attention, it works now!

Congrats on figuring it out! :confetti_ball:

1 Like

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.