user3216792
user3216792

Reputation: 5

Javascript: attempting to fill an array with a prompt but prompt is not displaying

I am attempting to call a method that asks the user how many entries they would like to make into an array and then prompts the user for each entry. I know this is likely a simple error but I cannot tell why my prompt is not working.

 <script>
function testScore(){
    var numberofScores = prompt("enter the number of scores:","");
    var scores = new Array();
    var whichScore=1;
    for(var i=0; i<numberofScores; i++; whichScore++){

    score[i]=prompt("enter score "+whichScore+":");
}
}
</script>

<a href="" onclick="testScore()">
Start Test score script
</a><br>

Upvotes: 0

Views: 72

Answers (4)

Scott Marcus
Scott Marcus

Reputation: 65835

A loop is configured with 3 sections and thus two semi-colons. You had whichScore++ in a 4th section after adding a third semi-colon. You could have added it to the end of the configuration with a comma. But, adding it to the loop body, and not part of the loop declaration is cleaner. That said, the variable is not even needed. Just use (i + 1) and note that we're not modifying i here, we're just using an offset of it for display purposes only.

Also, in the loop: score[i], needs to be scores[i] and your <a> element should have an href="#" instead of an empty attribute.

Lastly, don't use inline HTML event handling attributes as they:

  • Make the code more difficult to read and lead to code duplication.
  • Cause global wrapper functions to be created around your supplied attribute value that alter the binding of this in your function.
  • Don't follow the W3C DOM Event standard.

Use .addEventListener() in JavaScript instead:

// When the DOM content is ready
window.addEventListener("DOMContentLoaded", function(){

  // Get a reference to the hyperlink and create a click event handler for it:
  document.getElementById("makeScores").addEventListener("click", testScore);

  function testScore(){
    var numberofScores = prompt("enter the number of scores:","");
    var scores = new Array();
  
    for(var i = 0; i < numberofScores; i++){
      scores[i] = prompt("enter score " + (i + 1) + ":");
    }
  
    console.log(scores);
  }

});
<a href="#" id="makeScores">Start Test score script</a>

Upvotes: 4

Umang Gupta
Umang Gupta

Reputation: 16470

<script>
function testScore(){
    var numberofScores = prompt("enter the number of scores:","");
    var scores = new Array();
    var whichScore=1;
    for(var i=0; i<numberofScores; i++, whichScore++){
     scores.push(prompt("enter score "+whichScore+":"));
     //or 
     //scores[i]= (prompt("enter score "+whichScore+":"));
    }
}
</script>

<a href="" onclick="testScore()">
Start Test score script
</a><br>
  1. your variable is scores
  2. Use Array.push, since you did not declare the array length in array constructor. Edit : You can also use scores[i]. It will work as well.

Upvotes: 1

ibrahim mahrir
ibrahim mahrir

Reputation: 31712

for only have three sections (separated by a semicolon ;): initialization, the condition and the incrementation. If you want to initialize or increment more variables use a comma ,. Like this:

for(var i = 0; i < numberofScores; i++, whichScore++) {
    // ...

Since whichScore is basically just i + 1 you won't need to have two variables for that, just i will do:

for(var i = 0; i < numberofScores; i++) {
    score[i] = prompt("enter score " + (i + 1) + ":");
// ...

Note that the parenthesis in (i + 1) are necessary so the numbers are added instead of concatenated.

Upvotes: 0

calcazar
calcazar

Reputation: 1130

Here is a JSFiddle using your code. You have a lot going on here

https://jsfiddle.net/tcoedqkf/

First off, your for loop needs to have a comma besides the increments (although for readability I would do it in the for loop)

for(var i=0; i<numberofScores; i++,whichScore++){

Your variable name in the for loop is incorrect (missing an S)

scores[i]=prompt("enter score "+whichScore+":");

Upvotes: 1

Related Questions