중첩 for문

for문 안에 for문을 사용한 것을 중첩 for문이라고 합니다.

중첩for문

for(let i = 0; 1<100; i++){ for(let j = 0; j<100; j++){ //반복코드 } }

for문을 이용해서 1~10까지 출력

for(let i=1; i<=10; i++){
    for(let j=1; j<=10; j++){
        document.write(i+","+j+"<br>");
    }
}
function func5(){
    for(i = 1; i <= 7; i++){
        for(j = 1; j <= i; j++){
            document.write(j);
        }
        document.write("<br>");
    }
}
func5();

구구단

//구구단
// i * j = sum
//i(2~9까지 출력)   for(i=2; i<=9; i++)
//j(1~9까지 출력)   for(j=1; j<=9; j++)
// 2 * 1 = 2    3 * 1 = 3
// 2 * 2 = 4    3 * 2 = 6
// 2 * 3 = 6    3 * 3 = 9
// 2 * 4 = 8    3 * 4 = 12
// 2 * 5 = 10   3 * 5 = 15
// 2 * 6 = 12   3 * 6 = 18
// 2 * 7 = 14   3 * 7 = 21
// 2 * 8 = 16   3 * 8 = 24
// 2 * 9 = 18   3 * 9 = 27

for(let i=2; i<=9; i++){
    for(let j=1; j<=9; j++){
        let sum =i*j;
        document.write(i + "*" + j + "=" + sum);
        document.write("<br>")
    }
}

테이블 만들기

let num = 1;
let table = "<table border='1'>";

    for(let i=1; i<=10; i++){
        table += "<tr>";
        for(let j=1; j<=7; j++){
            table += "<td>"+ num +"</td>";
            num++;
        }
        table += "</tr>";
    }
    table += "</table>"

document.write(table);

Last updated

Was this helpful?