repl.it
JavaScript

Implementation of the Every method

fork
loading
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
every = function (collection, predicate, context) {

  if (Object.prototype.toString.call(collection) == '[object Object]') {
    for (var key in collection) {
      if (collection.hasOwnProperty(key)) {
        if (!predicate.call(context, collection[key], key, collection)) {
          return false; 
        }
      }
    }
  } else {
    for (var i = 0; i < collection.length; i++) {
      if (!predicate.call(context, collection[i], i, collection)) {
        return false; 
      } 
    }
  }
   
  return true;
};


var list1 = ['string', 'string', 'string', undefined, 'string'];

var isNotUndefined = every(list1, element => {
	return element !== undefined;
});

console.log(isNotUndefined);
// => false

Native Browser JavaScript