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
+47-2Lines changed: 47 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -4,21 +4,66 @@
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
6
7
+
for(leti=0;i<21;i++){
8
+
console.log(i);
9
+
}
7
10
11
+
//i starts at 3, i += 2 counts upward adding 2, therefore i would be 3, 5, 7, etc..
12
+
for(leti=3;i<30;i+=2){
13
+
console.log(i);
14
+
}
8
15
16
+
//i starts at 12, i -= 2 counts downward subracting 2, therefore i would be 12, 10, 8, etc..
17
+
for(leti=12;i>-15;i-=2){
18
+
console.log(i);
19
+
}
20
+
21
+
for(leti=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(leti=50;i>19;i-=2+(i%3===0)){
29
+
console.log('.',i);
30
+
}
9
31
10
32
/*Exercise #2:
33
+
11
34
Initialize two variables to hold the string “LaunchCode” and the array [1, 5, ‘LC101’, ‘blue’, 42].
12
35
13
36
14
37
Construct ``for`` loops to accomplish the following tasks:
15
38
a. Print each element of the array to a new line.
16
39
b. Print each character of the string - in reverse order - to a new line. */
17
40
41
+
letstr="LaunchCode";
42
+
letarr=[1,5,'LC101','blue',42];
18
43
44
+
for(leti=str.length-1;i>=0;i--){
45
+
console.log(str[i]);
46
+
}
19
47
20
-
48
+
for(leti=0;i<arr.length;i++){
49
+
console.log(arr[i]);
50
+
}
21
51
22
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:
23
53
a. One array contains the even numbers, and the other holds the odds.
0 commit comments