Migo
Migo

Reputation: 41

Trying to write a JavaScript "for loop" in the form of "while loop" while maintaining the same function

I was trying to convert this "for loop"

    for(rows=0;rows<26;rows++ ){
    for(seats=0;seats<100;seats++ ){
        console.log(rows+"-"+seats);
    }
}

to a while loop in javaScript and that's what I got

rows=0;
seats=0;
while(rows<26){

    while(seats<100){
        console.log(rows+"-"+seats);
        seats++;
    }
    rows++ ;
}

but the output wasn't the same and I think there is an issue with the while loop that I don't know .. I hope that anyone can help

Upvotes: 0

Views: 38

Answers (1)

sonEtLumiere
sonEtLumiere

Reputation: 4562

You need to reset seats = 0 for each row.

rows=0;
seats=0;
while(rows<26){
    seats = 0;
    while(seats<100){
        console.log(rows+"-"+seats);
        seats++;
    }
    rows++;
}

Upvotes: 1

Related Questions