Loops Flashcards

1
Q

Log to the console each item in the array below using a loop.

let vacationSpots = [‘Mozambique’, ‘Thailand’, ‘Bolivia’];

A
for (let i = 0; i < vacationSpots.length; i++) {
console.log(vacationSpots[i]);

}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How would you write a loop to run backwards through an array?

A
for (let i = vacationSpots.length -1; i >= 0; i--) {
console.log(vacationSpots[i])
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Write a loop which will loop through a deck of cards until it finds a spade, give example.

A
let cards = ['Diamond', 'Heart', 'Spade', 'Club']
let currentCard = 'Diamond';

while (currentCard !== ‘Spade’) {
console.log(currentCard);
currentCard = cards[Math.floor(Math.random() * 4)];

}
console.log(‘Omg a spade!’);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the difference between a for loop and a while loop?

A

A for loop will loop through an array a known amount of times and a while loop will loop through an array a unknown amount of times.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Write multiple functions in order to assign a person to the following:

  • A nationality
  • The nationality’s favorite foods
  • The nationality’s favorite footballer
  • Finally, welcome message to planet earth including the above information
A
const getNationalities = () => {
  return ['Italian', 'Dutch', 'German'];

}

const getRandomNationality = () => {
  const allNationalities = getNationalities();
  const nationality = allNationalities[Math.floor(Math.random() * 3)];
  return nationality;

}

const getNationalityFoods = nationality => {

if (nationality === ‘Italian’) {
return ‘Pizza, Pasta and Lasgne’;
}

if (nationality === ‘Dutch’) {
return ‘Stroop Waffel, Kroket and Bitterball’

}

if (nationality === ‘German’) {
return ‘Weiner Schnitzel, Frankfurter and Sauerkraut’;

}

}

const getNationalityFootballers = nationality => {

  let footballers = {Italian: 'Pirlo', Dutch: 'Robben', German: 'Neuer'}
  return footballers[nationality];

}

const getNationalityMessage = () => {
  const myNationality = getRandomNationality();

return ‘Welcome to Planet Earth, your nationality is ‘ + myNationality + ‘, your favorite foods must be ‘ + getNationalityFoods(myNationality) + ‘.’ + ‘ Your favorite footballer must be ‘ + getNationalityFootballers(myNationality) + ‘.’;

}
console.log(getNationalityMessage());

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

If we wanted to have a certain sentence translated into whale talk how could you do this?

Keep in mind:

Every letter that is a vowel is whale talk

A

let input = ‘I like turtles’;
let vowels = [‘a’, ‘e’, ‘i’, ‘o’, ‘u’]
let whaleTranslation = [];

for (I = 0; I < input.length; I++) {
if (input[I] === 'e') {
whaleTranslation.push(input[i]);
}
else if (input[I] === 'u' {
whaleTranslation.push(input[i]);
})
for (j = 0; j < vowels.length; j++) {
  if (input[I] === vowels[j]) {
   whaleTranslation.push(input[i]);
}

}

}
console.log(whaleTranslation.join(‘’).toUpperCase());

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Write a function that will return the factorial of a number. There are two ways, write both

A
function factorial(num) {
var fact = 1;
for (var i = num; i > 0; i--) {
fact *= i;
}
num = fact;
return num;
}
function factorial(num) {
if (num === 0) {
return 1;
}
return num * factorial(num - 1);
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Write a for loop in a function that does simple adding, by adding 1 to the given number. So if the input in the function is, the return should be the value of 1 + 2 + 3 + 4, so 10.

A
function simpleAdd(num) {
var sum = 1;
for (var i = 2; i <= num; i++) {
sum += i;
}
num = sum;
return num;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly