/    Sign up×
Community /Pin to ProfileBookmark

concatinating a command

I want to use a for loop to check the questions right in my quiz. I have a variable at the top for the number of questions to loop through. But how do I put the loop index in the command to check the value (document.Quiz.r1.value)? the code is below. Thanks

var numberOfQuestions = 5; // change this to the number of question the quiz contains
var questionsRight = 0;

function addScore()
{
for (i=1;i=numberOfQuestions;i++)
{
var question = document.Quiz.r + i + .value;
if (question!=”correct” || question!=”wrong” )
{
if (question==”correct”)
{
questionsRight = questionsRight +1;
}
}
else
{
prompt(“Please answer all the questions.”);
return;
}
}
document.write(‘You answered ‘+questionsRight+’ questions right out of ‘+numberOfQuestions+’ questions.’);
}

<li>Question 1: Why is the WS3 being Modernized?<br>
<input type=radio name=”r1″ value=”wrong” onclick=””>For a better looking display<br>
<input type=radio name=”r1″ value=”wrong” onclick=””>To provide faster alarm indication<br>
<input type=radio name=”r1″ value=”correct” onclick=””>Some parts are no longer supportable<br><br>
</li>

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@javaNoobieOct 01.2004 — document.forms['Quiz'].elements['r'+i]
×

Success!

Help @killiank spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...