Hi Cristian,
Yes, you’re doing the right thing. If you just copy and paste you won’t actually be learning.
You’ve got the console.log() in the right place in your code now. Hopefully, you can now see why your previous version wasn’t giving you the output of each iteration of the for loop, which is what you wanted to check that your array of the range is being generated correctly. So, now, when you run your code you will see that the range your code is generating is correct based on the start and end parameters. In order to check that the returned array is also correct and complete you need to add a function call and log that to the console too:
console.log(range(1, 10));
/* test with different start and end arguments */
If you want to log the state of the array at the end of each loop (instead of just the individual number added to it), then you need:
console.log(arr);
// instead of
console.log(i);
Once you’ve done this and understand how it works, you’re ready to move on to the next stages:
- Add the sum() function, which calculates the sum of all of the numbers in the array generated by your first function. In the exercise instructions, the sum function is called with the range function as the argument:
console.log(sum(range(1, 10)));
This means that your array of the range will be passed into your sum() function, where you can write code that iterates over each number in the range, adding it to a cumulative total.
Again, you can check that this cumulative calculation is being performed correctly by including an additional console.log within your for loop, which logs the variable storing your cumulative total at the end of each loop.
All of the remaining stages only require additions and modifications to your range() function. You don’t need to make any further changes to your sum() function, as it will always sum the numbers included in whatever array is produced and passed to it.
2. Add a 3rd parameter to your range() function called step
, which sets the interval between each number in the range.
3. Add additional functionality to your range() function, so that it can generate descending ranges as well as ascending ones. Descending ranges should be generated when start > end.
4. The final part is then to add a default for the step parameter, so that when the step parameter is omitted (i.e. left undefined) it will default to either 1 (for ascending ranges) or -1 (for descending ranges).
Don’t forget that for a bit of extra help (without actually being given the solution) you have the hints, which you can display in the online course book by clicking below the exercise instructions.
Good luck! If you want to check your progress at different stages, then post your full code here, and we’ll take a look at it. If you have any specific questions about a particular part of your code that you’re having problems with, then explain that in your post as well
And don’t worry at all if it takes you time to reply. We understand totally that everyone has busy lives and other commitments.