No else statement

Solution 1 says to create an if statement without an else statement in the loop. Doesn’t the else statement of undefined go inside the loop with the if? I am thinking that an else statement inside would cover the case that no element satisfies the test function. A loop can- I found the answer but lost the rest of the question.
This is how I thought.
Why is else statement not inside loop? Computer will run the test searching for a truthy first. If there is a truthy, something follows. Outside that idea, the false array rests.

Why did and else statement fail to load with wrap written and pass as return statement only? At this point, there is not much of an else.


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

Your browser information:

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

Challenge: Finders Keepers

Link to the challenge:

Everything inside the if block will run if the test condition is true. Everything inside the else block will be run if the test condition is false. The things after will be run regardless of the test condition.

Consider:

for (let i = 1; i <= 10; i += 1) {
  console.log('*** for number', i);
  if (i % 2 === 0) {
    console.log('I am even!');
  } else {
    console.log('I am odd!');
  }
  console.log('But I am always a number!');
}
1 Like

Thank you for your responsiveness. I researched what you said in else statements taking test conditions that are false. Return statements take what test cases are left that do not fit in the boxes of if statements.

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