JavaScript Knowledge 3 Flashcards

1
Q

For Loops - Fundamentals.

What is going on when we get in a for loop?

A

. Initial Expression — this is simply a place for you to initialize any JavaScript expression (an expression is a bit of JavaScript code that evaluates to a single value). Most often, it’s used to declare a variable, in this case i, that will be used as a counter for how many times the loop iterates. You should definitely follow this pattern until you’re comfortable enough to try a more complex expression.

  1. Conditional Expression — this should be a JavaScript expression that will evaluate to a true or false. In the example above, since i , which is currently set to 0 is indeed less than 10 , this will evaluate to true and execute the block of code inside of the curly braces {}. This condition is checked every time the loop runs, just before the code is executed. If this condition ever evaluates to false, the loop will stop and JavaScript will move on with executing code below it.
  2. Increment Expression — this should be an expression that increments your loop counter as the loop runs. It will be executed after the code inside of the loop block is run. In the example above, i++ is shorthand for increasing the value of i by 1.

Finally, once you’ve declared your loop conditions, you can insert the code that you want to be repeated inside of the curly braces {}. Again this code will be executed until your condition (#2 above) evaluates to false.

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

Challenge: For Loops and Arrays

  1. Iterate through the synonyms array using a for loop, pushing a greeting string with the format “Have a [synonym] day!” into the greetings array.
  2. Use a second for loop to iterate through the greetings and console.log() each greetings.
A
const synonyms = ['fantastic', 'wonderful', 'great'];
const greetings = [];
for (let i = 0; i < synonyms.length; i++) {
  greetings.push('Have a ' + synonyms[i] + ' day!');
}
for (let i = 0; i < greetings.length; i++) {
  console.log(greetings[i]);
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How do you calculate array elements?
You are given an array of five numbers called increaseByTwo. Use a for loop to iterate through the array and increase each number by two.

const increaseByTwo = [1, 2, 3, 4, 5];

A

const increaseByTwo = [1, 2, 3, 4, 5];

for (let i = 0; i < increaseByTwo.length; i++) {
  increaseByTwo[i] = increaseByTwo[i] + 2;
}

console.log(increaseByTwo);

[3, 4, 5, 6, 7]

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

Use a loop to iterate through the numbers 1 through 16. Push each number into fb, but push the string “fizz” in place of numbers divisible by 3, “buzz” in place of numbers divisible by 5, and “fizzbuzz” in place of numbers divisible by both 3 and 5.

Log fb to the console to see the output.

const fb = [];

A

const fb = [];

for (let i = 1; i < 17; i++) {
 if (i % 3 === 0 && i % 5 === 0) {
  fb.push("fizzbuzz");
 } else if (i % 3 === 0) {
   fb.push("fizz");
 } else if (i % 5 === 0) {
   fb.push("buzz");
 } else {
   fb.push(i);
 }
}

console.log(fb);

[1, 2, ‘fizz’, 4, ‘buzz’, ‘fizz’, 7, 8, ‘fizz’, ‘buzz’, 11, ‘fizz’, 13, 14, ‘fizzbuzz’, 16]

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

How do we return/access the last letter of a string without numerically placing a value/index in a bracket?

A

To get the last character of a string, call the charAt() method on the string, passing it the last index as a parameter. For example, str. charAt(str. length - 1) returns a new string containing the last character of the string.

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