Skip to content

Instantly share code, notes, and snippets.

@sethschori
Forked from anonymous/Up and Down (1.3).js
Created July 31, 2016 17:16
Show Gist options
  • Save sethschori/f55894b7e1401465e1800be6b8cb0e7f to your computer and use it in GitHub Desktop.
Save sethschori/f55894b7e1401465e1800be6b8cb0e7f to your computer and use it in GitHub Desktop.
https://repl.it/CTU3/128 created by sethopia
/*
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);
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