for (let counter = 0; counter < 4; counter++) {
console.log(counter);
}
//0 1 2 3
for문을
--
을 이용해서 감소하는 반복문을 작성해봅시다.
for (let counter = 3; counter >= 0; counter--){
console.log(counter);
}
array
에서.length
를 이용해서 반복문을 작성해봅시다.
const vacationSpots = ['Bali', 'Paris', 'Tulum'];
for(let i = 0 ; i< vacationSpots.length;i++){
console.log(`I would love to visit ${vacationSpots[i]}`);
}
이중
for
문을 작성해봅시다.
for (let outer = 0; outer < 2; outer += 1) {
for (let inner = 0; inner < 3; inner += 1) {
console.log(`${outer}-${inner}`);
}
}
/*
Output:
0-0
0-1
0-2
1-0
1-1
1-2
*/
// A for loop that prints 1, 2, and 3
for (let counterOne = 1; counterOne < 4; counterOne++){
console.log(counterOne);
}
// A while loop that prints 1, 2, and 3
let counterTwo = 1;
while (counterTwo < 4) {
console.log(counterTwo);
counterTwo++;
}
while
: 조건 확인 후 loop -> 조건이 거짓일 경우 실행 x
do-while
: 실행한 뒤 조건이 충족이 되는지 확인 후 loop -> 즉 1번은 무조건 실행
const firstMessage = 'I will print!';
const secondMessage = 'I will not print!';
// A do while with a stopping condition that evaluates to false
do {
console.log(firstMessage)
} while (true === false);
// A while loop with a stopping condition that evaluates to false
while (true === false){
console.log(secondMessage)
};