Created
July 31, 2016 17:16
-
-
Save anonymous/3dd829b5b6d18f1904b2ef9aad8a1aa7 to your computer and use it in GitHub Desktop.
https://repl.it/CTU3/128 created by sethopia
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
/* | |
UP & DOWN | |
Create a function countUpThenDown() that takes a number n and logs all the numbers 0 to n and back to 0. | |
eg) | |
countUpThenDown(2) ==> 0 1 2 1 0 | |
countUpThenDown(11) ==> 0 1 2 3 4 5 6 7 8 9 10 11 10 9 8 7 6 5 4 3 2 1 0 | |
*/ | |
var countUpThenDown = function(n) { | |
// loop from 0 to n and push to an array | |
var arr = []; | |
for (var i = 0; i <= n; i++) { | |
arr.push(i); | |
} | |
// loop down from n-1 and log along the way back to 0 | |
for (i = n-1; i >= 0; i--) { | |
arr.push(i); | |
} | |
// log arr | |
console.log(arr.join(" ")); | |
} | |
countUpThenDown(2); | |
countUpThenDown(11); |
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
Native Browser JavaScript | |
>>> 0 1 2 1 0 | |
0 1 2 3 4 5 6 7 8 9 10 11 10 9 8 7 6 5 4 3 2 1 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment