logo CodeStepByStep logo

countToBy

Language/Type: JavaScript recursion

Write a recursive function named countToBy that accepts integer parameters n and m and returns a string representing the sequential count to n in increments of m separated by commas. For example, a call to countToBy(10, 1) would return the string "1, 2, 3, 4, 5, 6, 7, 8, 9, 10", or the count to 10 by 1's. The following table shows several other calls and their expected return:

Call Meaning Return
countToBy(10,1) count to 10 by 1s "1, 2, 3, 4, 5, 6, 7, 8, 9, 10"
countToBy(25,4) count to 25 by 4s "1, 5, 9, 13, 17, 21, 25"
countToBy(30,4) count to 30 by 4s "2, 6, 10, 14, 18, 22, 26, 30"
countToBy(34,5) count to 34 by 5s "4, 9, 14, 19, 24, 29, 34"
countToBy(3,6) count to 3 by 6s "3"
countToBy(17,3) count to 17 by 3s "2, 5, 8, 11, 14, 17"

Notice that the increment does not have to be 1, such as when counting to 25 by 4s. The count must always end at n, but notice that at will not always be possible to start counting at 1, as shown in the output when n is 30 and m is 4. But the first number should always be in the range of 1 to m inclusive. It is possible that only one number will be returned in the string, as shown in the call table when n is 3 and m is 6.

If either m or n is less than 1, your function should return an empty string, "".

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.