Skip to content

Commit 733b636

Browse files
authored
completed for loop
1 parent c9493c5 commit 733b636

File tree

1 file changed

+54
-24
lines changed

1 file changed

+54
-24
lines changed

loops/exercises/for-Loop-Exercises.js

Lines changed: 54 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,54 @@
1-
/*Exercise #1: Construct for loops that accomplish the following tasks:
2-
a. Print the numbers 0 - 20, one number per line.
3-
b. Print only the ODD values from 3 - 29, one number per line.
4-
c. Print the EVEN numbers 12 to -14 in descending order, one number per line.
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-
7-
8-
9-
10-
/*Exercise #2:
11-
Initialize two variables to hold the string “LaunchCode” and the array [1, 5, ‘LC101’, ‘blue’, 42].
12-
13-
14-
Construct ``for`` loops to accomplish the following tasks:
15-
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-
18-
19-
20-
21-
22-
/*Exercise #3:Construct a for loop that sorts the array [2, 3, 13, 18, -5, 38, -10, 11, 0, 104] into two new arrays:
23-
a. One array contains the even numbers, and the other holds the odds.
24-
b. Print the arrays to confirm the results. */
1+
// Exercise #1
2+
// a. Print the numbers 0 - 20, one number per line.
3+
for (let i = 0; i <= 20; i++) {
4+
console.log(i);
5+
}
6+
7+
// b. Print only the ODD values from 3 - 29, one number per line.
8+
for (let i = 3; i <= 29; i += 2) {
9+
console.log(i);
10+
}
11+
12+
// c. Print the EVEN numbers 12 to -14 in descending order, one number per line.
13+
for (let i = 12; i >= -14; i -= 2) {
14+
console.log(i);
15+
}
16+
17+
// d. Challenge - Print the numbers 50 - 20 in descending order, but only if the numbers are multiples of 3.
18+
for (let i = 50; i >= 20; i--) {
19+
if (i % 3 === 0) {
20+
console.log(i);
21+
}
22+
}
23+
24+
// Exercise #2
25+
let str = "LaunchCode";
26+
let arr = [1, 5, 'LC101', 'blue', 42];
27+
28+
// a. Print each element of the array to a new line.
29+
for (let i = 0; i < arr.length; i++) {
30+
console.log(arr[i]);
31+
}
32+
33+
// b. Print each character of the string - in reverse order - to a new line.
34+
for (let i = str.length - 1; i >= 0; i--) {
35+
console.log(str[i]);
36+
}
37+
38+
// Exercise #3
39+
let nums = [2, 3, 13, 18, -5, 38, -10, 11, 0, 104];
40+
let evenNums = [];
41+
let oddNums = [];
42+
43+
// a. One array contains the even numbers, and the other holds the odds.
44+
for (let i = 0; i < nums.length; i++) {
45+
if (nums[i] % 2 === 0) {
46+
evenNums.push(nums[i]);
47+
} else {
48+
oddNums.push(nums[i]);
49+
}
50+
}
51+
52+
// b. Print the arrays to confirm the results.
53+
console.log('Even numbers:', evenNums);
54+
console.log('Odd numbers:', oddNums);

0 commit comments

Comments
 (0)