NESTED LOOP


Nested loop =


a loop inside of another loop

for(let i = 1; i <= 3; i+=1){
console.log(i)
}

result: 1, 2, 3

for(let i = 1; i <= 3; i+=1){
for(let j = 1; j <= 3; j+=1){ //loop inside loop
console.log(j)
}

result:
1, 2, 3
1, 2, 3
1, 2, 3

for(let i = 1; i <= 2; i+=1){
for(let j = 1; j <= 3; j+=1){ //loop inside loop
console.log(j)
}

result:
1, 2, 3
1, 2, 3

Html

<label id="rectangle"> </label>

Javascript

let rows = window.prompt('Enter # of rows')
let columns = window.prompt('Enter # of columns')

for(let i = 1; i <= 2; i+=1){
for(let j = 1; j <= 3; j+=1){ //loop inside loop
document.getElementById('rectangle').innerHTML += j
}
document.getElementById('rectangle').innerHTML += "<br>"
}
typing '3'rows and '9'columns ...

Monitor result ⬇️


Javascript

let symbol = window.prompt('Enter a symbol to use')
let rows = window.prompt('Enter # of rows')
let columns = window.prompt('Enter # of columns')

for(let i = 1; i <= 2; i+=1){
for(let j = 1; j <= 3; j+=1){
document.getElementById('rectangle').innerHTML += symbol
}
document.getElementById('rectangle').innerHTML += "<br>"
}

result: using '$' as symbol

$$$$$$$$$
$$$$$$$$$
$$$$$$$$$