craig
craig

Reputation: 581

Incremental looping

I have the following code:

if((dropdown1save=="1")&&(dropdown2save=="1")&&(dropdown3save=="1")){
    $('#feedbackbox').html(information[1]);
}

if((dropdown1save=="1")&&(dropdown2save=="1")&&(dropdown3save=="2")){
    $('#feedbackbox').html(information[2]);
  }

dropdown1save goes from 1 to 3 dropdown2save goes from 1 to 10 dropdown3save goes from 1 to 20

I need a loop whereby dropdown3save loops from 1 - 20 then dropdown2save goes up 1 and dropdown3 loops again

Then when dropdown2save has reached 10 dropdown1save goes up one and the loop for each one begins again

for example 111 112 (and so on until) 121 122 (and so on until 211 212 (etc)

every time the dropdown3save loops i need (information[x]] to go up 1 too (This will therefore go up to 600 as there are There are 600 combinations!)

Sorry thats a lot to figure out but I have no idea how to do it!

Upvotes: 0

Views: 79

Answers (3)

aamir
aamir

Reputation: 4043

for(var i=1; i<= 3; i++) {
    for(var j=1; j<= 10; j++) {
            for(var k=1;k<=20;k++) {
                    if((dropdown1save==i.toString() )&& (dropdown2save==j.toString()) && (dropdown3save==k.toString())){
                            $('#feedbackbox').html(information[200*(i-1) + 20*(j-1) + k]);
                    }
            }
    }
}

Upvotes: 0

Pascalz
Pascalz

Reputation: 2378

Try like this :

$('#feedbackbox').html(
    information[
        ((parseInt(dropdown1save) - 1)  * (10 * 20) ) + 
        ((parseInt(dropdown2save) - 1)  * 20) +
        parseInt(dropdown3save)
    ]
);

Upvotes: 2

JosephGarrone
JosephGarrone

Reputation: 4161

What you are describing is a looping. The following set of for statements will loop through the values from 1 to 20, then increase the value of j by one and loop from 1 to 20 again. Then when the value of j hits 10, it will increment i by one and repeat.

for (var i = 1; i < 3; i++) {
    for (var j = 1; j < 10; j++) {
        for (var k = 1; k < 20; k++) {
            count = i * j * k;
            if (dropdown1save == i.toString() && dropdown2save == j.toString() && dropdown3save == k.toString())
            {
                $("#feedbackbox").html(information[count]);
            }
        }
    }
}

That should do the trick.

Upvotes: 1

Related Questions