-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.

Tachi
  • 2,042
  • 3
  • 27
  • 44
  • What about an array of values ?? – Maraboc Jun 02 '15 at 10:37
  • possible duplicate of [Use a concatenated (dynamic) string as JavaScript object key?](http://stackoverflow.com/questions/9708192/use-a-concatenated-dynamic-string-as-javascript-object-key) – CodingIntrigue Jun 02 '15 at 10:38
  • @RGraham not exactly, it's using array positions in there, i need a variable declaration to be dynamic. – Tachi Jun 02 '15 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. – Mitul Jun 02 '15 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 :) – Tachi Jun 02 '15 at 11:28

3 Answers3

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;
}

}

}
ALI
  • 93
  • 1
  • 1
  • 9
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;
 }
Mitul
  • 3,431
  • 2
  • 22
  • 35
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.

Yeldar Kurmangaliyev
  • 33,467
  • 12
  • 59
  • 101