Created
March 15, 2019 03:39
-
-
Save chikoski/0c0e00b6d85d81e3e6f84057ad603d7d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function withIndex(list){ | |
for(let i = 0; i < list.length; i++){ | |
if(i % 5 !== 0){ | |
list[i].value = list[i].value * 2; | |
} | |
} | |
} | |
function withForEach(list){ | |
list.forEach((item, index) => { | |
if(index % 5 !== 0){ | |
item.value = item.value * 2; | |
} | |
}); | |
} | |
function withFilter(list){ | |
list.filter((item, index) => { | |
return index % 5 !== 0 | |
}).forEach(item => { | |
item.value = item.value * 2; | |
}); | |
} | |
class ListItem{ | |
constructor(value){ | |
this.value = value; | |
} | |
valueOf(){ | |
return this.value; | |
} | |
toString(){ | |
return this + ""; | |
} | |
} | |
function create(n){ | |
return Array(n).fill(0).map((_, index) => new ListItem(index)); | |
} | |
function main(){ | |
const listA = create(10); | |
withIndex(listA); | |
console.log(listA.join(",")); | |
const listB = create(10); | |
withForEach(listB); | |
console.log(listB.join(",")); | |
const listC = create(10); | |
withFilter(listC); | |
console.log(listC.join(",")); | |
} | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment