Question:

Write a function that satisfies the following console.log assertions.

var callback = function (num) {
  return num % 2 === 0
}

console.log($$$.all([1, 3, 4], callback) === false)
console.log($$$.all([2, 6, 4], callback) === true)

 

Answer:

$$$ = {
  all: function (arr, callback) {
    for (var i = 0, l = arr.length; i < l; i ++) {
      if (!callback(arr[i])) {
        return false;
      }
    }
    return true;
  }
}

Keywords:

© 2017 QuizBucket.org