루프 : 지정된 조건까지 일련의 지시를 반복하는 프로그래밍 도구
<For Loop>
for (let counter = 0; counter < 4; counter++) {
console.log(counter);
} // Output:
0
1
2
3
1. 초기화 루프를 시작하며 반복자 변수 선언
2. 정지조건은 반복자 변수가 평가되는 조건-
true로 평가되면 코드실행, false이면 정지
3. 반복문은 각 루프에서 반복자 변수를 업데이트하는데 사용됨
<Reverse Loop>
1. 반복기 변수를 초기화 식에서 가장 높은 값으로 설정
2. 반복기 변수가 원하는 양보다 작을 때 중지 조건 설정
3. 반복기는 각 반복 후 간격이 감소해야 함
for (let counter = 3; counter >= 0; counter--){
console.log(counter);
} // Output:
3
2
1
0
<Array Loop>
const animals = ['Grizzly Bear', 'Sloth', 'Sea
Lion'];
for (let i = 0; i < animals.length; i++){
console.log(animals[i]);
} // Output:
Grizzly Bear
Sloth
Sea Lion
다른 예
const vacationSpots = ['Bali', 'Paris',
'Tulum'];
for (let i = 0; i < vacationSpots.length; i++){
console.log('I would love to visit ' +
vacationSpots[i]);
} // Output:
I would love to visit Bali
I would love to visit Paris
I would love to visit Tulum
<중첩 Loop>
const myArray = [6, 19, 20];
const yourArray = [19, 81, 2];
for (let i = 0; i < myArray.length; i++) {
for (let j = 0; j < yourArray.length; j++) {
if (myArray[i] === yourArray[j]) {
console.log('Both loops have the number: ' +
yourArray[j])
}
}
};
다른 예
let bobsFollowers = ['Joey', 'Monica', 'Ross',
'Maria'];
let tinasFollowers = ['Cherry', 'Monica',
'Ross'];
let mutualFollowers = [];
for (let i = 0; i < bobsFollowers.length; i++){
for (let j = 0; j < tinasFollowers.length; j++){
if (bobsFollowers[i] === tinasFollowers[j])
{
mutualFollowers.push(bobsFollowers[i]);
}
}
};
<While Loop>
// 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++;
}
다른 예
const cards = ['diamond', 'spade', 'heart',
'club'];
let currentCard;
while (currentCard !== 'spade'){currentCard = cards[Math.floor(Math.random() * 4)];
console.log(currentCard);
}
<Do...While>
let countString = '';
let i = 0;
do {
countString = countString + i;
i++;
} while (i < 5);
console.log(countString);
다른 예
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)
};
다른 예
let cupsOfSugarNeeded = 4;
let cupsAdded = 0;
do{
cupsAdded++
console.log(cupsAdded + ' cup was added')
}while (cupsAdded< cupsOfSugarNeeded);
// Output:
1 cup was added
2 cup was added
3 cup was added
4 cup was added
<break>
for (let i = 0; i < 99; i++) {
if (i > 2 ) {
break;
}
console.log('Banana.');
}
console.log('Orange you glad I broke out the
loop!');
// Output:
Banana.
Banana.
Banana.
Orange you glad I broke out the loop!
다른 예
const rapperArray = ["Lil' Kim", "Jay-Z",
"Notorious B.I.G.", "Tupac"];
for (let i = 0; i < rapperArray.length; i++){
console.log(rapperArray[i]);
if (rapperArray[i] === 'Notorious B.I.G.'){
break;
}
}
console.log("And if you don't know, now you know.");
// Output:
Lil' Kim
Jay-Z
Notorious B.I.G.
And if you don't know, now you know.