Last active
August 29, 2015 14:14
-
-
Save bendc/535fc29c1f10417676d4 to your computer and use it in GitHub Desktop.
Create 10 divs
This file contains 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
// Imperative style | |
const createDivs = num => { | |
for (var i = 0; i < num; i++) | |
document.body.insertAdjacentHTML("beforeend", "<div></div>"); | |
}; | |
createDivs(10); | |
// Functional style: recursion | |
const createDivs = num => { | |
if (!num) return; | |
document.body.insertAdjacentHTML("beforeend", "<div></div>"); | |
return createDivs(num-1); | |
}; | |
createDivs(10); | |
// Functional style: array iteration | |
const newArr = size => [...Array(size)]; | |
const createDivs = num => | |
newArr(num).forEach(() => | |
document.body.insertAdjacentHTML("beforeend", "<div></div>") | |
); | |
createDivs(10); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment