0

I want to check if one of arguments equal 3. If it does i want to return true but i get undefined. Why is that ?

function isThree(...args){
  args.forEach(val => {
    if(val === 3){
      return true;
    } else{
      return false;
    }
  })
};
console.log(isThree(1,2,3,4,5));
Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
Dean Novak
  • 65
  • 3
  • 10

4 Answers4

2

Unlike a for loop which creates a new scope but is not executed in a separate function, forEach is executed in a separate function. I.e., returning from within a forEach handler won't return from the outer function as well.

let x = () => {
  [1, 2, 3].forEach(v => {
    console.log(v); // 1 2 3
    return v;
  })
  return 0;
};
console.log(x()); // 0

let y = () => {
  for (let v = 1; v <=3; v++) {
    console.log(v); // 1
    return v;
  }
  return 0; // never reached
};
console.log(y()); // 1
junvar
  • 11,151
  • 2
  • 30
  • 46
2

The reason you get undefined is because the function isThree() doesn't return anything. The two returns in your function return from the anonymous function inside foreach().

I think your function tries to determine if there's a value 3 in the array. There is already a function for that: includes(). Your function could be:

function isThree(...args) {
  return args.includes(3);
};

console.log(isThree(1,2,3,4,5));
KIKO Software
  • 15,283
  • 3
  • 18
  • 33
1

You're returning a value from the forEach function, but that doesn't mean you're returning a value from the isThree function. Also, try the filter function to find if there is a match for the number you're looking for.

Try this:

function isThree(...args){
  return args.filter(val => {
    if(val === 3){
      return true;
    }
  }).length > 0;
};
console.log(isThree(1,2,3,4,5));

In the previous example, I'm using filter to return every instance of the array in which the condition is met (in this case, val === 3). Returning true appends the result to an empty array, so the operation's response will be an array with every instance that met the condition. So by comparing the array size to > 0 I can find out if any element met the criteria.

You can even reduce the function further by using the shorthand args.filter(val => val === 3):

function isThree(args){
  return args.filter(val => val === 3).length > 0;
};

console.log(isThree(1,2,3,4,5));

Or, you can simplify further by using the prototype function some, which checks if any element of the array meets the criteria:

function isThree(args){
  return args.some(val => val === 3);
};

console.log(isThree(1,2,3,4,5));
Niche
  • 967
  • 5
  • 23
0

The problem is here about else statement because if if statement doesn't find, so obviously it will go for else statement, and here is a loop, so need to fix the loop

for(let a = 0; 10 > a; ++a )
{
   if( a === 3) console.log('We\'re done'); // I expect only this not else statement

   else console.log('Not found');
}

Output:

"Not found"
"Not found"
"Not found"
"We're done"
"Not found"

in the above example you see, a lot of times we make this mistake, Now Solve this problem

for(let a = 0; 5 > a; ++a )
{
   if( a === 3) 
    {
      console.log('We\'re done');
      return true; // I expect only this not else statement
    }

}

 console.log('Not found');
 return false;

I hope it'll give you an idea about how to solve the unexpected code in the loop;

Always happy to assist!

Ericgit
  • 6,089
  • 2
  • 42
  • 53