Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save sethschori/7a7605ff7aeb17391d26a12d18be47a8 to your computer and use it in GitHub Desktop.
Save sethschori/7a7605ff7aeb17391d26a12d18be47a8 to your computer and use it in GitHub Desktop.
https://repl.it/C1Th/119 created by sethopia
/*
Advanced Counting
We're going to rewrite our countdown function, but this time let's support negative numbers. If you pass the function a positive number, it counts down to 1. If you pass the function a negative number, it should count up to -1. You can assume the function will never be passed 0.
Please complete this problem in 2 ways.
1. With a while loop
2. With a for loop
count(4)
// 1
// 2
// 3
// 4
count(-4)
// -4
// -3
// -2
// -1
*/
function count(n) {
if (n < 0) var start = n, end = -1;
else var start = 1, end = n;
for (var i = start; i <= end; i++) console.log(i);
}
count(-10);
Native Browser JavaScript
>>> -10
-9
-8
-7
-6
-5
-4
-3
-2
-1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment