-1

So I have this simple condition:

var q1 = document.forms["quizForm"]["q1"].value;

And I have to repeat it 5 times like this:

var q1 = document.forms["quizForm"]["q1"].value;
var q2 = document.forms["quizForm"]["q2"].value;
var q3 = document.forms["quizForm"]["q3"].value;
var q4 = document.forms["quizForm"]["q4"].value;
var q5 = document.forms["quizForm"]["q5"].value;

But Instead I want to use a simple cycle like this:

for (n = 1; n < 5; n++) { 
        var qn = document.forms["quizForm"]["qn"].value;
    }

So how do I add number n to a variable q? So instead of q1 it looked something like qn in the cycle?

So If I use this loop:

function submitAnswers(){
    var total = 5;
    var score = 0;

    for (n = 1; n <= 5; n++) { 
        var qn = document.forms["quizForm"]["q" + n].value;
    }

    //Validation 
    if( ! q1){
        alert('You missed question 1');
        return false;
    }

}

Validation doesn't trigger, like if q1 wasn't existing.

But if I do next it triggers:

function submitAnswers(){
    var total = 5;
    var score = 0;

    var q1 = document.forms["quizForm"]["q1"].value;

    //Validation 
    if( ! q1){
        alert('You missed question 1');
        return false;
    }

}

So I guess qn isn't working it's declaring qn as a value instead.

I'm trying to implement something like this:

for (n = 1; n <= 5; n++) { 
        eval('var q' + n + '=document.forms['quizForm']['q' + n + '].value;');
        alert(q1);
    }

But no luck so far.

5
  • What about an array of values ?? Commented Jun 2, 2015 at 10:37
  • possible duplicate of Use a concatenated (dynamic) string as JavaScript object key? Commented Jun 2, 2015 at 10:38
  • @RGraham not exactly, it's using array positions in there, i need a variable declaration to be dynamic. Commented Jun 2, 2015 at 11:03
  • @Tachi after success what you want? You need to do score calculation only? Can you please confirm what operation will perform after success so i will help you more. Commented Jun 2, 2015 at 11:25
  • @Mitul Yes, I only want to calculate the overall score, I'm just following some tutorial and improving some things in it with simple things like loops. Thanks for the help :) Commented Jun 2, 2015 at 11:28

3 Answers 3

3

You can check your invalid question inside loop

function submitAnswers(){
var total = 5;
var score = 0;

for (n = 1; n <= 5; n++) { 
    var qn = document.forms["quizForm"]["q" + n].value;
 //Validation 
if( ! qn){
    alert('You missed question '+n);
    break;
}

}

}
Sign up to request clarification or add additional context in comments.

Comments

2

You can do the validation inside the loop and do the score calculation in same loop.

var total = 5;
var score = 0;
for (i = 1; i <=n; i++) {
    if(document.forms["quizForm"]["q" + i].value == ""){
        alert("You missed question " + i);
        return false;
    } 
    score += document.forms["quizForm"]["q" + i].value;
 }

Comments

1

It should work. Javascript implicitly converts types. In your case, it is enough to use + operator.

for (n = 1; n < 5; n++) { 
    var qn = document.forms["quizForm"]["q" + n].value;
}

By the way, according to your first example, your loop should look like

for (var n = 1; n <= 5; n++)

or it will not hit n === 5.

Comments

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.