1

Here's my code. I've tried a lot of things but it's not working. Please don't give me some complicated code, because I'm merely an intermediate programmer in javascript. So try to make things as simple as possible if you can. Thanks to everyone who helps.

function readNumberOfEntries() {
                var entryNumber, numberOfEntries, a;
                do {
                    numberOfEntries = Number(prompt("Enter Value Here"));
                    if (numberOfEntries < 2) {
                        alert("Error: Number is not greater than or equal to 2");
                    }
                } while (numberOfEntries < 2);
                for (entryNumber = 0; entryNumber < numberOfEntries; entryNumber++) {
                    a[entryNumber] = Number(prompt("Enter Value"));
                    a = new Array(numberOfEntries);
                    document.writeln(a);
                }
            }
2
  • Shouldn't a = new Array(numberOfEntries); and document.writeln(a); be outside the loop? Otherwise, you're creating a new array after each entered number, which deletes the previous one. And then you're printing an empty array numberOfEntries times. Commented Jul 24, 2015 at 19:21
  • Yea, that's probably why when I put 3 for number of entries in the prompt and then enter values: 1, 2, and 3, this is what I get: 1 1,2 1,2,3 Commented Jul 25, 2015 at 21:21

2 Answers 2

2

The issue with your code is that you are overwriting your array in each iteration of the for loop. This should fix your problem:

function readNumberOfEntries() {
    var entryNumber, numberOfEntries, a;

    do {
        numberOfEntries = Number(prompt("Enter Value Here"));
        if (numberOfEntries < 2) {
            alert("Error: Number is not greater than or equal to 2");
        }
    } while (numberOfEntries < 2);

    a = new Array(numberOfEntries);  

    for (entryNumber = 0; entryNumber < numberOfEntries; entryNumber++) {
        a[entryNumber] = Number(prompt("Enter Value"));
    }

    document.writeln(a);
}
Sign up to request clarification or add additional context in comments.

2 Comments

When I tried the code you gave me, this is what it gave me: 1,, 1,2, 1,2,3 So that's not working.
That is because the document.writeln(a) expression was in the for loop, so the contents of the array got written on each iteration. I was under the impression that you were just using that statement for debugging purposes, so I left it in the loop body.
1

You should initialize your array before assignment/adding value into it.

      function readNumberOfEntries() {
            var entryNumber, numberOfEntries, a=[];
            do {
                numberOfEntries = Number(prompt("Enter Value Here"));
                if (numberOfEntries < 2) {
                    alert("Error: Number is not greater than or equal to 2");
                }
            } while (numberOfEntries < 2);
            for (entryNumber = 0; entryNumber < numberOfEntries; entryNumber++) {
               // a = new Array(numberOfEntries);
                a[entryNumber] = Number(prompt("Enter Value"));

                document.writeln(a);
            }
        }

1 Comment

When I put 3 for number of entries in the prompt and then enter values: 1, 2, and 3, this is what I get: 1 1,2 1,2,3 It doesn't seem to be working, but I got it to work yesterday so thanks for the help.

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.