Laurence L Laurence L - 6 months ago 29
jQuery Question

Combining two loops

I want to get this with looping 01 to 10 and a - j

I have the loops separate but don't know how to join them.

//A - Z
for (var i = 65; i <= 90; i++) {
var letterEnd = String.fromCharCode(i).toLowerCase();
}

//01 - 10
var i;
for(i=1;i<=10;i++) {
var precedingZero = (i<10) ? '0' : '';
var currentMenuClass = ".p" + precedingZero + i;
var currentMenuSansP = precedingZero + i;

$(currentMenuClass + 'a .plus_sign.plus' + currentMenuSansP).hide();
$(currentMenuClass + 'a .minus_sign.minus' + currentMenuSansP).show();


The final should be as this with 01 to 10 and a - j

$(.p01a .plus_sign.plus01).hide();
$(.p01a .minus_sign.minus01).show();


Thanks for the help.

Answer

You need to use nested loops.

var i;
for(i=1;i<=10;i++) {
  var precedingZero = (i<10) ? '0' : '';
  var currentMenuClass = ".p" + precedingZero + i;
  var currentMenuSansP = precedingZero + i;
  for (var j = 97; j <= 112; j++) {
    var letterEnd = String.fromCharCode(j);
    $(currentMenuClass + letterEnd + ' .plus_sign.plus' + currentMenuSansP).hide();
    $(currentMenuClass + letterEnd + ' .minus_sign.minus' + currentMenuSansP).show();
  }
}
Comments