Skip to content

Commit 5e4651d

Browse files
committed
Loops Exercises
1 parent 9757b2d commit 5e4651d

File tree

14 files changed

+3838
-8
lines changed

14 files changed

+3838
-8
lines changed

loops/exercises/for-Loop-Exercises.js

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,66 @@
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+
for (let i = 0; i < 21; i++) {
8+
console.log(i);
9+
}
710

11+
//i starts at 3, i += 2 counts upward adding 2, therefore i would be 3, 5, 7, etc..
12+
for (let i = 3; i < 30; i+=2) {
13+
console.log(i);
14+
}
815

16+
//i starts at 12, i -= 2 counts downward subracting 2, therefore i would be 12, 10, 8, etc..
17+
for (let i = 12; i > -15; i-=2) {
18+
console.log(i);
19+
}
20+
21+
for (let i = 50; i > 19; i--) {
22+
if (i % 3 === 0) {
23+
console.log(i);
24+
}
25+
}
26+
//OR Another possible way is below...
27+
//How to stop from printing the first iteration of 50? Maybe not possible in this syntax because the initial expression executes once before any loop interations.
28+
for (let i = 50; i > 19; i -= 2 + (i % 3 === 0)) {
29+
console.log('.',i);
30+
}
931

1032
/*Exercise #2:
33+
1134
Initialize two variables to hold the string “LaunchCode” and the array [1, 5, ‘LC101’, ‘blue’, 42].
1235
1336
1437
Construct ``for`` loops to accomplish the following tasks:
1538
a. Print each element of the array to a new line.
1639
b. Print each character of the string - in reverse order - to a new line. */
1740

41+
let str = "LaunchCode";
42+
let arr = [1, 5, 'LC101', 'blue', 42];
1843

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

20-
48+
for (let i = 0; i < arr.length; i++) {
49+
console.log(arr[i]);
50+
}
2151

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

loops/exercises/node_modules/.package-lock.json

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

loops/exercises/node_modules/readline-sync/LICENSE

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

loops/exercises/node_modules/readline-sync/README-Deprecated.md

Lines changed: 89 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)