Forked from anonymous/Advanced Counting (2.6) Extra Problems.js
Created
July 31, 2016 19:37
-
-
Save sethschori/7a7605ff7aeb17391d26a12d18be47a8 to your computer and use it in GitHub Desktop.
https://repl.it/C1Th/119 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
/* | |
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); |
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 | |
>>> -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