Click here to Skip to main content
15,562,816 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm working on a college assessment, and I need to update my program. I need to add code that will ask for what the amount of scores is, and display a prompt for each score, based on what that number was.

So something like...
scoreAmount = prompt("How many scores did you get?");

If the user inputs "3", then I want the user to be asked for the 3 scores. So, "What was your 1st score?", "What was your 2nd score?", and ""What was your 3rd score?".

I was thinking of using an array somehow, but I don't know how.

I tried looking around for a similar question already asked, or using what I already know about arrays, but I'm still clueless.

If I was to use arrays, I would probably need to turn the input number into an empty array, so if the user put in "3", it would create an array "[ , , ]" or "[0, 0, 0]". And then call the array values one by one for each prompt.

So if the array was called "scoreArray", each prompt would call a value of that array from left to right, until all values have been called for. Again, I've looked around on how to do this, but I still have no idea.

What I have tried:

I have not tried anything yet.
Updated 15-Dec-16 3:14am

1 solution is a great resource for learning new concepts. I suggest looking at their example on how to use the built-in JavaScript function prompt(), Window prompt() Method[^].

Get the input number, then do a loop.
Share this answer
Denisowator 15-Dec-16 8:54am    
I already know everything about prompt(). And the only thing I could see on the page you linked (that would work), was the switch function. But that would pretty much work the same if I just used a bunch of if statements, and would create way too many lines of code. Since I would have to create 1 prompt if the input was 1, then 2 prompts if the input was 2, and so on.

Is there no more effective way of doing this? (using fewer lines of code)
ZurdoDev 15-Dec-16 9:08am    
Do a loop. I guess I'm not sure where you are stuck.
Denisowator 15-Dec-16 9:16am    
I'm not stuck, I just don't think this would be an effective way of doing it, since it would result in hundreds upon hundreds lines of code.

Let's say the maximum number of scores a person can input is 100, I would have to make a switch function, and 100 case statements, each one with 1 more prompt than the last, since the program has to ask for each score seperately.

Let's say I input 50. I would be asked for the first score, then the second, and all the way up to the 50th. To do this, I would have to write 50 different prompts for that one case (if someone types 50). And then on the next case, if someone types 51, I have to copy/paste those 50 prompts, and put in a new one, instantly doubling the lines of code.

And it would be pretty much the same, if I did a loop.
ZurdoDev 15-Dec-16 9:42am    
No, a loop is very simple.

for (var i = 0; i< numberFromPrompt; i++){
// store scores in an array.

3 lines of code.
Maniraj.M 16-Dec-16 5:11am    
My suggestion is as same as RyanDev's for loop solution.It is more effective way than switch

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900