JavaScript

for문을 이용하여 출력하기

초짜코딩 2022. 1. 19. 16:00

for문을 이용하여 반복문 만들기

for문을 이용하여 1~10까지 출력하기

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

for문을 이용하여 1~10까지 중 짝수만 출력하기

{
    for(let i=1; i<=10; i++){
        if(i % 2 == 0){
            console.log(i);
        }
    } 
}

for문을 이용하여 1~10까지 중 홀수만 출력하기

{
    for(let i=1; i<=10; i++){
        if(i % 2 !== 0){
            console.log(i);
        }
    } 
}

for문을 이용하여 2~9단까지 구구단 출력하기

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