skip to Main Content

I am trying to create an "every" function for an array. I am aware that it is already built-in, but I just want to enhance my programming skills.

let every = (arr, f) => {
    for (let element of arr) {
        if (f(element)) {
            return true
        }
        else {
            return false
        } 
    }
} 

console.log(every([2, 4, 6], n => {n % 2 === 0})) // <-- false

Why is it giving me false. Could someone please spot the error? Thank you!

4

Answers


  1. because you function not return.

    try this:

    console.log(every([2, 4, 6], n => {return n % 2 === 0}))
    
    Login or Signup to reply.
  2. Two mistakes:

    1. Either change the test function to n => {return n % 2 === 0} or n => n % 2 === 0

    2. Your for loop should return false immediately if any element fails the test. Then, if nothing fails, you return true after the loop.

    let every = (arr, f) => {
      for (let element of arr) {
        if(!f(element)) {
          return false
        }
      }
      return true
    } 
    
    console.log(every([2, 4, 6], n => n % 2 === 0))
    Login or Signup to reply.
  3. The problem is that the every function is returning true or false based on the first element of the array that is passed into the function. The function should iterate through each element of the array and return false if any of the elements do not pass the test defined by the callback function f.
    Here is the corrected every function:

    let every = (arr, f) => {
        for (let element of arr) {
            if (!f(element)) {
                return false;
            }
        }
        return true;
    }
    console.log(every([2, 4, 6], n => {n % 2 === 0}));
    --> true
    

    or

    let every = (arr,f) => {return arr.map(i=>{return f(i)})}
    console.log(every([2, 4, 6], n => {return n % 2 === 0}));
    // --> [true, true, true]
    
    let every = (arr,f) => {return arr.map(i=>{return f(i)}).indexOf(!!0)===-1?!0:!!0}
    console.log(every([2, 4, 6], n => {return n % 2 === 0}));
    // --> true
    
    let every = (arr,f) => {return arr.map((i,n)=>{return {[n]:f(i)}})}
    console.log(every([2, 4, 5], n => {return n % 2 === 0}));
    // --> [{0: true},{1: true},{2: false}] return with array keys
    
    Login or Signup to reply.
  4. Here’s a revised version of your implementation that fixes two issues:

    1. Your evenness arrow function wasn’t returning anything, so it always evaluated to false in a boolean context.
    // doesn't return anything
    n => {n % 2 === 0}
    
    // fixed with explicit return
    n => {return n % 2 === 0}
    
    // fixed with implicit return by removing the {}
    n => n % 2 === 0
    
    1. Returning the result of the first evaluation:
    if (f(element)) {
      return true // <== this exits the function the first time it's encountered
    }
    else {
      return false // <== so does this
    } 
    

    Fixed by returning false if an entry doesn’t pass the test function, and returning true only after all elements have been evaluated.

    let every = (arr, f) => {
        for (let element of arr) {
            if (!f(element)) {
                return false // only need one negative to know we should return false
            }
        }
        return true;
    } 
    
    console.log(every([2, 4, 6], n => n % 2 === 0)) // true
    console.log(every([2, 5, 6], n => n % 2 === 0)) // false
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search