repl.it
JavaScript

Implementation of the Some 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
33
34
35
36
37
38
some = 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 true; 
        }
      }
    }
  } else {
    for (var i = 0; i < collection.length; i++) {
      if (predicate.call(context, collection[i], i, collection)) {
        return true; 
      } 
    }
  }
   
  return false;
};

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

var hasUndefined = some(list1, e => {
	return e === undefined;
});

console.log(hasUndefined);
// => true

var list2 = ['string', 'string', 'string', 'string', 'string'];

var hasUndefined = some(list2, e => {
	return e === undefined;
});

console.log(hasUndefined);
// => false
Native Browser JavaScript