Loops and Functions Flashcards
Write a for loop that counts to 50 by 5.
for (i = 0; i < 51; i += 5){
console.log(i)
}
What are the three parts needed for a for loop?
- ) Initialization
- ) Stopping condition
- ) Iteration statement
Write a for in loop that console logs each of the values in the following object. let pet = { name: "Finn", breed: "Shiba Inu", age: 2, goodBoy: true };
let pet = { name: "Finn", breed: "Shiba Inu", age: 2, goodBoy: true }; for (item in pet) { console.log(pet[item]) }
How do you invoke or call this function? function hi( ) { console.log('Hello') }
hi( )
Write and invoke a function that counts down from 10 to 0.
function countDown( ) { for (i = 10; i >= 0; i--) console.log(i) } countDown( )
Write a for in loop that console logs each item in the following array. let seasons = ['spring', 'summer', 'autumn', 'winter'];
let seasons = ['spring', 'summer', 'autumn', 'winter']; for (s in seasons){ console.log(seasons[s]) }
Write a function that accepts the parameter food and console logs “My favorite food is …” using the parameter.
function favFood(food){ console.log(`My favorite food is ${food}.`) } favFood('sushi')
Write a function that takes in the parameters crust, sauce, cheese, and topping to build a pizza. Have the function console log your pizza order.
function pizza(crust, sauce, cheese, topping){ console.log(`I would like a pizza with ${crust} crust, ${sauce}, ${cheese}, and ${topping}.`) } pizza('thin', 'marinara', 'mozarella', 'olives')
What does the keyword “Return” do?
It brings data out of the function to make it accessible in a wider scope.
In the following for loop what is i = 1?
for (i = 1; i <= 10; i++) {
console.log(i)
}
The initialization
In this case it set the variable of i to 1 initially
In the following for loop what is i <= 10?
for (i = 1; i <= 10; i++) {
console.log(i)
}
The stopping condition
In this case the for loop will not stop until is is no longer less than or equal to 10
In the following for loop what is i++?
for (i = 1; i <= 10; i++) {
console.log(i)
}
The iteration statement
In this case it is saying that the variable i will be incremented by one each time to loop runs.
For the following array write a for of loop that console logs “Super Mario” before each of the items.
let games = [64, “Sunshine”, “Galaxy”, “Odyssey”];
let games = [64, “Sunshine”, “Galaxy”, “Odyssey”];
for (game of games) {
console.log(“Super Mario”, game)
}