Reputation: 5
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
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:
this
in your function.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
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>
Upvotes: 1
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
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