Skip to content

Commit 9d4bdd5

Browse files
committed
loops exercise
1 parent 61803e3 commit 9d4bdd5

1 file changed

Lines changed: 48 additions & 6 deletions

File tree

loops/exercises/for-Loop-Exercises.js

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,64 @@
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-
7-
8-
6+
//a.
7+
for (let i = 0; i <= 20; i++) {
8+
console.log(i);
9+
}
10+
//b.
11+
for (let i = 3; i <= 29; i++) {
12+
if (i % 2 !== 0) {
13+
console.log(i);
14+
}
15+
}
16+
//c.
17+
for (let i = 12; i >= -14; i--) {
18+
if (i % 2 === 0) {
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+
}
928

1029
/*Exercise #2:
1130
Initialize two variables to hold the string “LaunchCode” and the array [1, 5, ‘LC101’, ‘blue’, 42].
1231
32+
let stringVariable = "LaunchCode";
33+
let arrayVariable = [1, 5, 'LC101', 'blue', 42];
1334
1435
Construct ``for`` loops to accomplish the following tasks:
1536
a. Print each element of the array to a new line.
16-
b. Print each character of the string - in reverse order - to a new line. */
17-
1837
38+
for (let i = 0; i < arrayVariable.length; i++) {
39+
console.log(arrayVariable[i]);
40+
}
1941
42+
b. Print each character of the string - in reverse order - to a new line. */
2043

44+
for (let i = stringVariable.length - 1; i >= 0; i--) {
45+
console.log(stringVariable[i]);
46+
}
2147

2248
/*Exercise #3:Construct a for loop that sorts the array [2, 3, 13, 18, -5, 38, -10, 11, 0, 104] into two new arrays:
2349
a. One array contains the even numbers, and the other holds the odds.
24-
b. Print the arrays to confirm the results. */
50+
51+
let originalArray = [2, 3, 13, 18, -5, 38, -10, 11, 0, 104];
52+
let evenArray = [];
53+
let oddArray = [];
54+
55+
for (let i = 0; i < originalArray.length; i++) {
56+
if (originalArray[i] % 2 === 0) {
57+
evenArray.push(originalArray[i]);
58+
} else {
59+
oddArray.push(originalArray[i]);
60+
}
61+
}
62+
63+
b. Print the arrays to confirm the results. */
64+
65+
console.log("Even Numbers Array:", evenArray);
66+
console.log("Odd Numbers Array:", oddArray);

0 commit comments

Comments
 (0)