JS function Array

I got the following problem: fill an Array between n and m (in a function), console (fillArray(0,3)), through what method to fill an array between 0 and 3?

What have you done so far? What part do you need help with? What language are you writing this in? What questions do you have?

Hi, it’s JS I need a help to figure out what method of Array to use to fill the following (it’s a basic test problem from JS lecture I’m taking, so it’s not a part of any program)

function fillArray (n,m) {

??? - what is supposed to be in the body? tried for loop, not working
}
console.log(fillArray(0,3));

So this should return an array with [0,1,2,3]?

Good point wrapped in spoiler tags. Though they did state in a subsequent post ā€œ??? - what is supposed to be in the body? tried for loop, not workingā€

yes, it should return 0,1,2,3

That’s true. Tell you what I will delete it for now and if the original poster specifically asks for the solution, is it ok if I repost it?

Fair enough. Can we see what you attempted so far?

The problem is right above, can you show it to me with FOR loop? didn’t work for me.
You right, I need to learn a lot of things as I’m super new to computer programming.

Lets try the hints first and if that does not work, we can go with the full solution.

You can do this with a for loop using the parameters that are passed into the function.

Randell, I started learning JS and introducing myself to computer programming 2 months ago coming from a completely different background :slight_smile: but I do most certainly appreciate your question!

His questions impact how the solution would need to be written. For example, my original solution would not handle the case:

fillArray(10, 5)

It can still be done, but how you would need to do it would change and it also depends on the expected output. Should it be

[10,9,8,7,6,5]

or

[5,6,7,8,9,10]

In problem it would go (5,6,7…)

Then you are going to need logic to check is the first number passed in is larger than the second and if it is, swap what you use for the start and end value of the for loop.

That is if the problem actually allows for that. One of the problem constraints could be that the first number must be smaller than the second. Then the question is, what do you do if they are not? throw an error?

So what I have is this

function fillArray(n, m) {
**where n<m, always. And the following expected outputs

}
console.log(fillArray(0,3));
console.log(fillArray(3,5));
console.log(fillArray(20,25));

show us the loop you tried, we can tell you how near you are

show us what you tried so we can give you hints

So with my solution (and someone may have a better one). You are going to:

  1. Need to create an array to be filled and returned at the end of the function.
  2. You are going to use the n and m parameters passed to the function to create the start and end values for the loop.
  3. The values are added to the array inside the loop using the loop’s counter variable.

I’d write them from n to m because n<m for all equations, and continue do so while n<m, and returning index plus one that’s how I would know the next number.

Thank you, that really helped, the code is still ā€˜undefined’ but at least I have less doubts.