Skip to content

Commit c526d94

Browse files
committed
for loop exercises
1 parent b434bc2 commit c526d94

File tree

1 file changed

+64
-18
lines changed

1 file changed

+64
-18
lines changed

loops/exercises/for-Loop-Exercises.js

Lines changed: 64 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,68 @@
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+
let numbers = (20);
67

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. */
8+
for (let i = 0; i <= 20; i++ ) {
9+
console.log(i)
10+
}
11+
12+
let nums = 29
13+
for (let i = 3; i <= 29; i = i + 2){
14+
console.log(i)
15+
}
16+
17+
let num = 12;
18+
for (let i = 12; i >= -14; i-= 2) {
19+
console.log(i)
20+
}
21+
22+
let chall = 50;
23+
for (let i = 50; i >= 20; i--){
24+
if (i % 3 === 0){
25+
console.log(i);
26+
}
27+
}
28+
29+
30+
31+
/*Exercise #2:
32+
Initialize two variables to hold the string “LaunchCode” and the array [1, 5, ‘LC101’, ‘blue’, 42].
33+
34+
35+
Construct ``for`` loops to accomplish the following tasks:
36+
a. Print each element of the array to a new line.
37+
b. Print each character of the string - in reverse order - to a new line. */
38+
let word = "LaunchCode";
39+
let arr = [1, 5, 'LC101', 'blue', 42];
40+
let reversed = "";
41+
42+
for (let i = 0; i < arr.length; i++){
43+
console.log(arr[i])
44+
}
45+
46+
for (let i = word.length - 1; i >= 0 ; i--){
47+
48+
console.log(word[i])
49+
}
50+
51+
52+
/*Exercise #3:Construct a for loop that sorts the array [2, 3, 13, 18, -5, 38, -10, 11, 0, 104] into two new arrays:
53+
a. One array contains the even numbers, and the other holds the odds.
54+
b. Print the arrays to confirm the results. */
55+
56+
let things = [2, 3, 13, 18, -5, 38, -10, 11, 0, 104];
57+
let even = ""; //leave this as open parenthese, otherwise the value will print with the along the numbers we are deparating.
58+
let odd = "";
59+
60+
for (let i = things.length - 1; i >= 0; i--) {
61+
if (things[i] % 2 === 0) { //in this if statement, things determines (i) the modulus takes integers of 2 or in this case even, because it starts at 0.
62+
even = things[i] + " " + even; // that way when printed down here, it will be just the even numbers
63+
} else {
64+
odd = things[i] + " " + odd;
65+
}
66+
}
67+
68+
console.log("Even values: " + even.trim());
69+
console.log("Odd values: " + odd.trim());
70+

0 commit comments

Comments
 (0)