|
4 | 4 | c. Print the EVEN numbers 12 to -14 in descending order, one number per line.
|
5 | 5 | d. Challenge - Print the numbers 50 - 20 in descending order, but only if the numbers are multiples of 3. (Your code should work even if you replace 50 or 20 with other numbers). */
|
6 | 6 |
|
| 7 | +//a |
| 8 | +for (let i = 0; i <= 20; i++) { |
| 9 | + console.log(i); |
| 10 | +} |
7 | 11 |
|
| 12 | +//b |
| 13 | +for (let j = 3; j <= 29; j = j + 2) { |
| 14 | + console.log(j); |
| 15 | +} |
| 16 | + |
| 17 | +//c |
| 18 | +for (let k = 12; k >= -14; k = k - 2) { |
| 19 | + console.log(k); |
| 20 | +} |
| 21 | + |
| 22 | +//d |
| 23 | +for (let l = 50; l >= 20; l--) { |
| 24 | + if (l % 3 === 0) { |
| 25 | + console.log(l); |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +/*Exercise #2: |
| 30 | +Initialize two variables to hold the string “LaunchCode” and the array [1, 5, ‘LC101’, ‘blue’, 42]. |
| 31 | +
|
| 32 | +
|
| 33 | +Construct ``for`` loops to accomplish the following tasks: |
| 34 | + a. Print each element of the array to a new line. |
| 35 | + b. Print each character of the string - in reverse order - to a new line. */ |
| 36 | + |
| 37 | + |
| 38 | +let course = "LaunchCode"; |
| 39 | +let misc = [1, 5, 'LC101', 'blue', 42]; |
| 40 | +let reversed = ''; |
| 41 | +//a |
| 42 | +for (let m = 0; m < misc.length; m++) { |
| 43 | + console.log(misc[m]); |
| 44 | +} |
| 45 | + |
| 46 | +//b |
| 47 | +for (let n = course.length - 1; n >= 0; n--) { |
| 48 | + console.log(course[n]); |
| 49 | +} |
| 50 | + |
| 51 | +/*Exercise #3:Construct a for loop that sorts the array [2, 3, 13, 18, -5, 38, -10, 11, 0, 104] into two new arrays: |
| 52 | + a. One array contains the even numbers, and the other holds the odds. |
| 53 | + b. Print the arrays to confirm the results. */ |
| 54 | + |
| 55 | +let blocks = [2, 3, 13, 18, -5, 38, -10, 11, 0, 104]; |
| 56 | +let odd = []; |
| 57 | +let even = []; |
| 58 | + |
| 59 | +for (let o = 0; o < blocks.length; o++) { |
| 60 | + if (blocks[o] % 2 === 0) { |
| 61 | + even.push(blocks[o]); |
| 62 | + } else { |
| 63 | + odd.push(blocks[o]); |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | + |
| 68 | +console.log("Even numbers: ", even); |
| 69 | +console.log("Odd numbers: ", odd); |
8 | 70 |
|
9 | 71 |
|
10 | 72 | /*Exercise #2:
|
|
0 commit comments