logo CodeStepByStep logo

rangeOfNumbers

Language/Type: JavaScript cumulative algorithms fencepost for loops

Write a function named rangeOfNumbers that accepts two integers as parameters and prints the sequence of numbers between the two parameters, separated by commas and spaces. Print an increasing sequence if the first argument is smaller than the second; otherwise, print a decreasing sequence. If the two numbers are the same, that number should be printed by itself.

For example, the call rangeOfNumbers(2, 8) should produce the following console output:

2, 3, 4, 5, 6, 7, 8

Your function should also be able to count downward if the end is less than the start. For example, the call rangeOfNumbers(18, 11) should produce the following console output:

18, 17, 16, 15, 14, 13, 12, 11 

If the two values are the same, simply print that number a single time. You may assume that the two parameters passed are integers. Use console.log() to print your result on a single line.

Function: Write a JavaScript function as described, not a complete program.

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.