Loops Flashcards
1
Q
If I wanted to log each item in my array to the console, but didn’t want to write a different console.log to the console each time, how can I do this?
A
for (var i = 0; i < bucketList.length; i++) { console.log(bucketList[i]); }
2
Q
If I had two arrays and I wanted to see whether any items matched, how could I do this? (Use the your fav places and friend as an example)
A
var myPlaces = [‘Tokyo’, ‘Kiev’ ‘Lisbon’]; var friendPlaces = [‘New York’, ‘Kiev’ ‘Paris’];
For (var i = 0; i < myPlaces.length; i++) {
for (var j = 0; j < friendPlaces.length; j++) {
if(myPlaces[i] === friendPlaces[j])
console.log(‘Match: ’,myPlaces[I]);
}
}
3
Q
Use a while loop to loop through a deck of cards until it finds spade
A
var cards = [‘Heart’, ‘Spade’, ‘Diamond’, ‘Club’]; var currentCard = ‘Heart’;
while (currentCard !== ‘Spade’) { console.log(currentCard); randomNumber = Math.floor(Math.random() * 4); currentCard = cards[randomNumber]; } console.log(‘OMG a spade’);