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]);
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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]);
}
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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’);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly