2

Fairly new to programming. I've set up a prompt command to have the user input text or a number which is stored to an array, but the while loop seems to be rewriting over the array value each time it loops.

The array is acting like a variable only storing one value

var course = new Array();
var grade = new Array();



while(confirm("Would you like to add a course?"))

    {course = prompt("Enter the course code. Example - ABC1234");

1 Answer 1

2

To add an element to an array, use array.push.

Change the last line to look like this:

course.push( prompt("Enter the course code. Example - ABC1234") );

You also have a missing } at the end. So your entire code snippet looks like this:

var course = new Array();
var grade = new Array();



while(confirm("Would you like to add a course?")){
    course.push( prompt("Enter the course code. Example - ABC1234") );
};
Sign up to request clarification or add additional context in comments.

9 Comments

Cheers mate, but now the program is running weirdly. I've got the course array aswell as a grade array. below it are if functions making sure it meets certain conditions, slicing the array, checking the value length etc & now it throw up a false since I added the .push
I understand your concerns, but it's usually better to open a new question if the original one is answered. It helps other people (who will get your question by using search) to navigate SO more efficiently. And it helps you as well, too. )
@user1638050 You should add a new question as said above and give us a link.
@user1638050 I think you should really take a tutorial on using JS before jumping into making a web app (even if it's this simple).
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.