Loops and Functions Flashcards

1
Q

Write a for loop that counts to 50 by 5.

A

for (i = 0; i < 51; i += 5){
console.log(i)
}

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

What are the three parts needed for a for loop?

A
  1. ) Initialization
  2. ) Stopping condition
  3. ) Iteration statement
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q
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 
};
A
let pet = { 
name: "Finn", 
breed: "Shiba Inu", 
age: 2, 
goodBoy: true 
};
for (item in pet) {
  console.log(pet[item])
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
How do you invoke or call this function?
function hi( ) {
console.log('Hello')
}
A

hi( )

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

Write and invoke a function that counts down from 10 to 0.

A
function countDown( ) {
  for (i = 10; i >= 0; i--)
  console.log(i)
}
countDown( )
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q
Write a for in loop that console logs each item in the following array.
let seasons = ['spring', 'summer', 'autumn', 'winter'];
A
let seasons = ['spring', 'summer', 'autumn', 'winter'];
for (s in seasons){
  console.log(seasons[s])
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Write a function that accepts the parameter food and console logs “My favorite food is …” using the parameter.

A
function favFood(food){
  console.log(`My favorite food is ${food}.`)
}
favFood('sushi')
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

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.

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

What does the keyword “Return” do?

A

It brings data out of the function to make it accessible in a wider scope.

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

In the following for loop what is i = 1?

for (i = 1; i <= 10; i++) {
console.log(i)
}

A

The initialization

In this case it set the variable of i to 1 initially

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

In the following for loop what is i <= 10?

for (i = 1; i <= 10; i++) {
console.log(i)
}

A

The stopping condition

In this case the for loop will not stop until is is no longer less than or equal to 10

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

In the following for loop what is i++?

for (i = 1; i <= 10; i++) {
console.log(i)
}

A

The iteration statement

In this case it is saying that the variable i will be incremented by one each time to loop runs.

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

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”];

A

let games = [64, “Sunshine”, “Galaxy”, “Odyssey”];

for (game of games) {
console.log(“Super Mario”, game)
}

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