You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: loops/exercises/for-Loop-Exercises.js
+64-18Lines changed: 64 additions & 18 deletions
Original file line number
Diff line number
Diff line change
@@ -3,22 +3,68 @@
3
3
b. Print only the ODD values from 3 - 29, one number per line.
4
4
c. Print the EVEN numbers 12 to -14 in descending order, one number per line.
5
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
+
letnumbers=(20);
6
7
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(leti=0;i<=20;i++){
9
+
console.log(i)
10
+
}
11
+
12
+
letnums=29
13
+
for(leti=3;i<=29;i=i+2){
14
+
console.log(i)
15
+
}
16
+
17
+
letnum=12;
18
+
for(leti=12;i>=-14;i-=2){
19
+
console.log(i)
20
+
}
21
+
22
+
letchall=50;
23
+
for(leti=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
+
letword="LaunchCode";
39
+
letarr=[1,5,'LC101','blue',42];
40
+
letreversed="";
41
+
42
+
for(leti=0;i<arr.length;i++){
43
+
console.log(arr[i])
44
+
}
45
+
46
+
for(leti=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
+
letthings=[2,3,13,18,-5,38,-10,11,0,104];
57
+
leteven="";//leave this as open parenthese, otherwise the value will print with the along the numbers we are deparating.
58
+
letodd="";
59
+
60
+
for(leti=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
0 commit comments