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

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

1 Like

Show us your attempted for loop.

Have you tried to write out an algorithm before trying to write any code? You must first understand the steps needed before you can decide what code to use to recreate those steps.

1 Like

Maybe the user just wanted some hints instead of the actual code? We were trying to engage the user instead of just posting a solution (which anyone can do).

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”

I meant not showing the solution at all (regardless of spoiler tags). The user can still see the solution by clicking on them. Maybe try to guide with hints next time. Both you and the user will gain something from doing it.

1 Like

But the user never showed us the code attempted.

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?

Why not try to give hints and not post the solution either way? Maybe the user needs to learn how to use a for loop first? Who knows until the user posts code attempted.

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.

I have a question myself. Could the first argument passed to the function be greater than or equal to the second argument passed to the function?

For example, could you have:

fillArray(10, 5)

And if so, what would be the expected return value of the function?

What would the expected return value be for:

fillArray(3, 3)

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!

That is great! We welcome users from all levels. If you are having trouble figuring out how for loops work, you might want to check out our Basic JavaScript curriculum which is full of challenges using for loops.

In general, before I tackle a problem, it is good to understand what can be assumed about the input received by a function, in order to make sure your function can handle any “edge cases”.

So do you have other sample inputs and their expected outputs that can be shared with us?

1 Like

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




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