Precourse - Part 2

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

for loops accepts three conditions:

1. 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. for (let i = 0...) 2. 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. for (let i = 0; i < example.length...) 3. 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. for (let i = 0; i < example.length; i++) 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.

Challenge 1

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.

Pair Programming

Following this model of navigator (the explainer) and driver (the implementer) requires you to rationalize (at least a little) why you're following your approach while still jumping into the code without spending your time only researching. Learning Resources Easy - Codeacademy - Team Treehouse - Following tutorials Hard - CSX Challenges - Codewars, Hackerank - MIT Opencourseware - Building projects

Challenge 5

Initialize a variable addThis to 0 and a variable sum to 0. Use a while loop to repeat a code block as long as addThis is less than 10. In the code block, add the value of addThis to sum, then increment addThis by 1. After the while loop runs, the value of sum should be the sum of the numbers 0 through 9. let addThis = 0; let sum = 0; while (addThis < 10) { sum = addThis + sum; addThis++; } console.log(sum);

Challenge 7

Iterate through the array and multiply a number by 10 if it is greater than or equal to 5.

Challenge 2

Loop through the arrays and push a string with the format "My name is [firstName] [lastName] and I am from [place]" into the array holding the respective bios

pop method

To remove items from the end of an array numbers.pop(); console.log(numbers); // should log: [1, 2, 3, 4]

Challenge 9

This time, use the Object.values() method to return an array of checkObj's values, and assign this array to a constant called objToArray. Next, use a for loop to iterate through objToArray and determine if any of the numbers are divisible by 6. If so, reassign the value of divBy6 to true.

push method

To add an element to the end of an array let numbers = [1, 2, 3, 4]; numbers.push(5); console.log(numbers); // should log: [1, 2, 3, 4, 5]

Challenge 6

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. Hint: Check out the remainder/modulo operator: %.

Challenge 4

Use a while loop to increment count by 2 on each repetition of the block of code. Run the code block of your while loop until count is 8. let count = 2; let i = 2; while (i < 8) { count = count + i; i++; } console.log(count);

Challenge 6

Use an if statement to check if num is greater than 100. If num is greater than 100, reassign the value of final to null. Otherwise, set final to be two times the value of num. const num = 40; let final; if (num > 100) { final = null; } else if (num < 100) { final = num * 2; } console.log(final);

Challenge 3

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.

Challenge 12

You are given an object called sumMe containing several key/value pairs and a variable called total whose initial value is 0. Using a for... in loop, iterate through the keys of sumMe; if the value corresponding to a key is a number, add it to total.

Challenge 11

You are provided with an array, possibleIterable. Using a for loop, build out the object divByThree so that each key is an element of possibleIterable that is divisible by three. The value of each key should be the array index at which that key can be found in possibleIterable

Challenge 10

You are provided with an empty array called nestedArr. Using a for loop starting at index 0, add 5 subarrays to nestedArr, with each nested array containing the string 'loop' concatenated with the corresponding index in nestedArr as its first element, and the index as its second element. Example of a subarray: ['loop3', 3].

Challenge 8

You are provided with an empty array called objToArray. Using a for... in loop, fill the array with all of the numbers from the checkObj object if they are greater than or equal to 2.


Set pelajaran terkait

Module 3 - Mental Health Concepts

View Set

Exercise Physiology: Health and Physical Fitness

View Set

2.5 Identify Basic Features of Mobile Operating Systems Computer operating systems are not the only type of operating system

View Set

Lesson 1 Estructura 1.3 Present tense of ser Review

View Set

World Geography Unit 7 US and Canada Vocab

View Set

EPA Practice Test Flash Cards (CORE)

View Set

CNPS 365: C9: Behavioural Therapy

View Set