-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
friend-or-foe.js
66 lines (59 loc) · 1.55 KB
/
friend-or-foe.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
function friend(friends){
// a place to store "real" friends
const realFriends = [];
// iterate over the friends
for (let i = 0; i < friends.length; i++) {
const friend = friends[i];
// if the friend name is length 4
if (friend.length === 4) {
// push into the real friends
realFriends.push(friend);
}
}
// return real friends
return realFriends;
}
function friend(friends){
const realFriends = [];
friends.forEach(friend => {
if (friend.length === 4) {
realFriends.push(friend);
}
});
return realFriends;
}
function friend(friends){
return friends.filter(friend => {
if (friend.length === 4) {
return true;
}
return false;
});
}
function friend(friends){
return friends.filter(friend => {
return friend.length === 4;
});
}
function friend(friends){
return friends.filter(friend => friend.length === 4);
}
function friend(friends){
for (let i = friends.length - 1; i >= 0; i--) {
const friend = friends[i];
if (friend.length !== 4) {
friends.splice(i, 1);
}
}
return friends;
}
// Alca!
function friend(friends) {
return friends.reduceRight((_, name, i, a) => {
return (name.length !== 4 && a.splice(i, 1), a);
}, null);
}
console.log(friend(["Ryan", "Kieran", "Mark"]), ["Ryan", "Mark"]);
console.log(friend(["Ryan", "Jimmy", "123", "4", "Cool Man"]), ["Ryan"]);
console.log(friend(["Jimm", "Cari", "aret", "truehdnviegkwgvke", "sixtyiscooooool"]), ["Jimm", "Cari", "aret"]);
console.log(friend(["Love", "Your", "Face", "1"]), ["Love", "Your", "Face"]);