Loops Flashcards
Log to the console each item in the array below using a loop.
let vacationSpots = [‘Mozambique’, ‘Thailand’, ‘Bolivia’];
for (let i = 0; i < vacationSpots.length; i++) { console.log(vacationSpots[i]);
}
How would you write a loop to run backwards through an array?
for (let i = vacationSpots.length -1; i >= 0; i--) { console.log(vacationSpots[i]) }
Write a loop which will loop through a deck of cards until it finds a spade, give example.
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!’);
What is the difference between a for loop and a while loop?
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.
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
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());
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
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());
Write a function that will return the factorial of a number. There are two ways, write both
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); }
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.
function simpleAdd(num) { var sum = 1; for (var i = 2; i <= num; i++) { sum += i; } num = sum; return num; }