Skip to content

Commit 4d67f7d

Browse files
committed
for loop excercises completed
1 parent 2b916e8 commit 4d67f7d

File tree

1 file changed

+50
-2
lines changed

1 file changed

+50
-2
lines changed

loops/exercises/for-Loop-Exercises.js

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,69 @@
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). */
66

7+
//a
8+
for (let i = 0; i <= 20; i++) {
9+
console.log(i);
10+
}
711

12+
//b
13+
for (let i = 3; i <= 29; i += 2) {
14+
console.log(i);
15+
}
16+
17+
//c
18+
for (let i = 12; i >= -14; i -= 2) {
19+
console.log(i);
20+
}
21+
22+
//d
23+
for (let i = 50; i >= 20; i--) {
24+
if (i % 3 === 0) {
25+
console.log(i);
26+
}
27+
}
828

929

1030
/*Exercise #2:
1131
Initialize two variables to hold the string “LaunchCode” and the array [1, 5, ‘LC101’, ‘blue’, 42].
1232
13-
1433
Construct ``for`` loops to accomplish the following tasks:
1534
a. Print each element of the array to a new line.
1635
b. Print each character of the string - in reverse order - to a new line. */
1736

37+
let label = "LaunchCode";
38+
let randList = [1, 5, 'LC101', 'blue', 42];
1839

40+
//a
41+
for (let i = 0; i < randList.length; i++) {
42+
console.log(randList[i]);
43+
}
1944

45+
//b
46+
for (let i = label.length; i >= 0; i--) {
47+
console.log(label[i])
48+
}
2049

2150

2251
/*Exercise #3:Construct a for loop that sorts the array [2, 3, 13, 18, -5, 38, -10, 11, 0, 104] into two new arrays:
2352
a. One array contains the even numbers, and the other holds the odds.
24-
b. Print the arrays to confirm the results. */
53+
b. Print the arrays to confirm the results. */
54+
55+
let array3 = [2, 3, 13, 18, -5, 38, -10, 11, 0, 104];
56+
let evenArr = [];
57+
let oddArr = [];
58+
59+
for (let i = 0; i < array3.length; i++) {
60+
if (array3[i] % 2 === 0) {
61+
evenArr.push(array3[i]);
62+
}
63+
}
64+
65+
for (let i = 0; i < array3.length; i++) {
66+
if (array3[i] % 2 !== 0) {
67+
oddArr.push(array3[i]);
68+
}
69+
}
70+
71+
console.log(evenArr);
72+
console.log(oddArr);

0 commit comments

Comments
 (0)