skip to Main Content

I’m not sure how to check every element to the callback and check if they are all true; I am also quite new to trying to code so sorry if it looks bad.

Write a function myEvery that accepts an array and a callback as arguments.
The function should return a boolean indicating whether or not all elements of
the array return true when passed into the callback.
Do not use the built in Array#every

let myEvery = function(arr, cb) {
   let bool = false;
   if (cb(arr) === true) {
      bool = true;
   }
   return bool;
};

2

Answers


  1. @Kooilnc’s response for ‘[].every()’ should be used in such cases, but as for an understanding about creating callbacks & functions that use callbacks, or validation functions within conditionals, I’ll write a little something that helped me understand them better.

    & this question is not really a callback context, but just a use of functions in a ‘first-class’ manner. Callbacks, to me, seemed to be designed and named for their ability to call a function/method when their execution context has completed, but def not limited to only this.

    To give an example of callbacks, and checking the values returned by the "callback" or "higher-order function" see below for an amateur example.

    p.s. I am not a professional, just a hobbyist who likes to learn!

    let myArr = [0,2,4,6,8,9,10,11,12,14];
    
    let isEvenOdd = function(val) {
      console.log('checking if value is even or odd.');
      return (val % 2 === 0) ? 'even' : 'odd';
    }
    
    let callback = function() {
      console.log('done!ntlogged within the cb!');
    }
    
    let firstOddValue = function(arr, validator, cb) {
      let tmp = arr;
      let val = tmp.shift();
      let eo = validator(val);
      if (eo === 'even') {
        console.log(`${val} is even`);
        return firstOddValue(tmp, validator, cb);
      } else if (eo === 'odd') {
        console.log(`${val} is odd`);
      }
      return cb();
    }
    
    firstOddValue(myArr, isEvenOdd, callback);
    
    

    Every time the runtime (or compiler depending on the language/context) approaches a function call within another function, the js runtime will pause the execution of the current function stack, create a new execution stack and run the new one until completes or returns a value, after which it resumes the previous execution context, etc.

    Login or Signup to reply.
  2. You could use for.. to loop through the array and return false immediately if the callback returns falsy value. Not that checking whether all array items are true should be done in the callback not in myEvery. All array filter/check methods work with truthy/false values returned from callbacks. Learn here:

    https://developer.mozilla.org/en-US/docs/Glossary/Falsy
    https://developer.mozilla.org/en-US/docs/Glossary/Truthy

    Also note that for checking whether array items truthy/false you could use Boolean() which is a constructor of a wrapper object of a boolean primitive which is used then as a true/false condition.

    let myEvery = function(arr, cb) {
       for(let i = 0; i < arr.length; i++){
          if (!cb(arr[i])) {
              return false;
          }
       }
       return true;
    };
    
    const arr = ['YOUR CASE: CHECKING IF ALL true',
    () => myEvery([true, true, true, true, true], item => item === true),
    
    'CHECKING true/false FROM CALLBACK',
    () => myEvery([1, 1, 1, 0, 1], item => item === 1),
    
    'CHECKING FALSY',
    () => myEvery([1, 1, 1, 0, 1], item => item),
    
    'CHECKING FALSY WITH ARRAY::EVERY()',
    () => [1, 1, 1, 0, 1].every(item => item),
    
    'CHECKING WITH BOOLEAN()',
    () => myEvery([1, 1, 1, 0, 1], Boolean),
    
    'CHECKING ALL TRUE',
    () => myEvery([1, 1, 1, 1, 1], item => item === 1)];
    
    for(let i = 0; i < arr.length; i+=2){
      console.log(arr[i]);
      console.log(arr[i+1].toString().slice(6));
      console.log(arr[i+1]());
    }

    Here’s a log of what happens:

    let myEvery = function(arr, cb) {
       for(let i = 0; i < arr.length; i++){
          const result = cb(arr[i]);
          console.log('result of callback for', arr[i], '=>', result);
          if (!result) {
              console.log('returning false');
              return false;
          }
       }
       console.log('all array items are ok, returning true');
       return true;
    };
    
    console.log(myEvery([1, 1, 1, 0, 1], item => item === 1));
    console.log(myEvery([1, 1, 1, 1, 1], item => item === 1));
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search