Skip to content
Advertisement

How do I recursively count up to less than n [closed]

I am struggling with the problem of having applications of loops and arrays.

I have a variable “n” which represents the limit of the loop, i.e.

if n = 3, the array would look like:

arr[1,2,3,1,2,3,1,2,3];

or if n = 4, it would look like:

arr[1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4];

here’s my code so far, someone please let me know the mistake I have made in implementing the above problem…

JavaScript

Advertisement

Answer

This is the major mistake you have done…

arr[i] = n ;

You should update value after each interval of length n which can be controlled by the loop running with i and the value inside each interval could be controlled with the loop j. See that one change I have made in the code below…

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement