Skip to content

Instantly share code, notes, and snippets.

@sethschori
Last active July 31, 2016 19:03
Show Gist options
  • Save sethschori/21afdfbec6c06ab294e5ad1b6fb10e68 to your computer and use it in GitHub Desktop.
Save sethschori/21afdfbec6c06ab294e5ad1b6fb10e68 to your computer and use it in GitHub Desktop.
/*
Count by m
Write a function that takes three arguments: n (number), m(number) and direction (string). The function should count to n by intervals of m. If the direction string is "Up", the function should count up to n, if the string is "Down", it should count down. You can assume that both n and m will be greater than 0. Do not print numbers greater than n.
Please complete the problem in 2 ways:
1. Using a for loop
2. Using while a loop
count(10, 2, "Up")
// 2
// 4
// 6
// 8
// 10
count(11, 2, "Down")
// 11
// 9
// 7
// 5
// 3
// 1
*/
function count(n, m, direction) {
if (direction === "Up") {
for (var i = m; i <= n; i += m) {
console.log (i);
}
} else {
for (var i = n; i > 0; i -= m) {
console.log (i);
}
}
}
count(10,2,"Up");
count(11,2,"Down");
Native Browser JavaScript
2
4
6
8
10
11
9
7
5
3
1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment