Skip to content

Commit 658dae3

Browse files
committed
loop exercises in progress
1 parent bab44de commit 658dae3

1 file changed

Lines changed: 39 additions & 2 deletions

File tree

loops/exercises/for-Loop-Exercises.js

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,59 @@
33
b. Print only the ODD values from 3 - 29, one number per line.
44
c. Print the EVEN numbers 12 to -14 in descending order, one number per line.
55
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+
for (let i = 0; i < 21; i++) {
7+
console.log(i);
8+
}
69

10+
for (let i = 3; i < 31; i = i + 2) {
11+
console.log(i);
12+
}
713

14+
for (let i = 12; i >= -14; i = i - 2) {
15+
console.log(i);
16+
}
817

9-
18+
for (let i = 50; i >= 20; i--) {
19+
console.log(i);
20+
}
1021
/*Exercise #2:
1122
Initialize two variables to hold the string “LaunchCode” and the array [1, 5, ‘LC101’, ‘blue’, 42].
1223
1324
1425
Construct ``for`` loops to accomplish the following tasks:
1526
a. Print each element of the array to a new line.
1627
b. Print each character of the string - in reverse order - to a new line. */
28+
let str = 'LaunchCode';
29+
let arr = [1, 5, 'LC101', 'blue', 42];
30+
let reversed = "";
1731

32+
for (let i = 0; i < arr.length; i++) {
33+
console.log(arr[i]);
34+
}
35+
// 2.2 is not completed. error of i is not defined
36+
// for (let i = 0; i < str.length; i++); {
37+
// reversed = str[i] + reversed;
38+
39+
// }
40+
// console.log(reversed);
1841

1942

2043

2144

2245
/*Exercise #3:Construct a for loop that sorts the array [2, 3, 13, 18, -5, 38, -10, 11, 0, 104] into two new arrays:
2346
a. One array contains the even numbers, and the other holds the odds.
24-
b. Print the arrays to confirm the results. */
47+
b. Print the arrays to confirm the results. */
48+
let arr1 = [2, 3, 13, 18, -5, 38, -10, 11, 0, 104];
49+
let evens = [];
50+
let odds = [];
51+
52+
for (let i=0; i < arr1.length; i++) {
53+
if (arr1[i] % 2 == 0) {
54+
evens.push(arr1[i]);
55+
} else {
56+
odds.push(arr1[i]);
57+
}
58+
}
59+
60+
console.log(evens);
61+
console.log(odds);

0 commit comments

Comments
 (0)