-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcustomEach.js
More file actions
36 lines (27 loc) · 951 Bytes
/
customEach.js
File metadata and controls
36 lines (27 loc) · 951 Bytes
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
/*
Source: Codewars
Difficulty: 6 kyu
Title: Custom each() Array method
Description:
JavaScript provides an Array.prototype.forEach method that allows
you to iterate over array values. For this exercise you will create
your own array method called 'each'. It will be similar to the forEach
method, except for one difference. If the callback function returns true
then the loop will stop and no additional values will be iterated.
Example:
The following shows a contrived example of how this new method would be used:
var letters = ['a', 'b', 'c', 'd', 'e']
var allowedLetters = []
letters.each(function(letter, index){
// break out of the loop if we reached a letter with the value 'd'
if(letter == 'd') {
return true;
}
allowedLetters.push(letter);
})
// allowedLetters should equal ['a', 'b', 'c']
*/
Array.prototype.each = function(fun)
{
return this.some(fun);
};