예제
<script>
/*
[문제]
반복문을 사용해서 보기와 같이 출력하시오.
[보기]
0 30
2 28
4 24
6 18
8 10
*/
</script>
HTML
복사
정답_for문
<script>
/*
[문제]
반복문을 사용해서 보기와 같이 출력하시오.
[보기]
0 30
2 28
4 24
6 18
8 10
*/
let total = 30;
let a = 0;
for(let i = 0; i < 5; i++) {
document.write(a, " ", total, "<br>");
a += 2;
total -= a;
}
document.write("<br>");
let total2 = 30;
for(let i = 0; i < 10; i+=2){
total2 -= i;
document.write(i, " ", total2, "<br>");
}
</script>
HTML
복사
정답_while문
<script>
/*
[문제]
반복문을 사용해서 보기와 같이 출력하시오.
[보기]
0 30
2 28
4 24
6 18
8 10
*/
let total = 30;
let a = 0;
let i = 0;
while(i < 5) {
document.write(a, " ", total, "<br>");
a += 2;
total -= a;
i += 1;
}
document.write("<br>");
let total2 = 30;
let i2 = 0;
while(i2 < 10) {
total2 -= i2;
document.write(i2, " ", total2, "<br>");
i2 += 2;
}
</script>
HTML
복사


