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
/*Exercise #1: Construct for loops that accomplish the following tasks:
2
2
a. Print the numbers 0 - 20, one number per line.
3
+
for (let i = 0; i <= 20; i++) {
4
+
console.log(i);
5
+
}
3
6
b. Print only the ODD values from 3 - 29, one number per line.
4
7
c. Print the EVEN numbers 12 to -14 in descending order, one number per line.
5
8
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
9
10
+
for(leti=0;i<=20;i++){//<= includes the number
11
+
console.log(i);
12
+
}
7
13
14
+
console.log();
15
+
16
+
for(leti=3;i<30;i++){//++ IS ASCENDING
17
+
if(i%2===1){//if remainder 2 is odd if remainder is 0 even
18
+
console.log(i);//print the value that will be looped
19
+
}
20
+
}
21
+
22
+
console.log();
23
+
24
+
for(leti=12;i>=-14;i-=2){//-= is descending and icnriment of 2
25
+
console.log(i);
26
+
}
27
+
28
+
for(leti=50;i>=20;i--){//-- is DESCENDDING
29
+
if(i%3===0){
30
+
console.log(i);
31
+
}
32
+
}
33
+
34
+
console.log();
8
35
9
36
10
37
/*Exercise #2:
11
38
Initialize two variables to hold the string “LaunchCode” and the array [1, 5, ‘LC101’, ‘blue’, 42].
39
+
*/
40
+
41
+
letvarOne="LaunchCode";
12
42
13
43
14
-
Construct ``for`` loops to accomplish the following tasks:
44
+
letcuteArray=[1,5,"LC101","blue",42];
45
+
46
+
/*Construct ``for`` loops to accomplish the following tasks:
15
47
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. */
48
+
b. Print each character of the string -
49
+
in reverse order -
50
+
to a new line. */
51
+
52
+
53
+
for(leti=0;i<cuteArray.length;i++){//the array length is the limitation condition
54
+
console.log(cuteArray[i]);//print the array index at i the loop information dictates i
55
+
}
17
56
57
+
console.log();
58
+
18
59
60
+
19
61
62
+
for(leti=varOne.length-1;i>=0;i--){//read as undefined so use -1 because was donig 1 extra
63
+
console.log(varOne[i]);
64
+
}
20
65
66
+
console.log();
21
67
22
68
/*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. */
69
+
70
+
a. One array contains the even numbers,
71
+
and the other holds the odds.
72
+
73
+
b. Print the arrays to confirm the results. */
74
+
75
+
letarrayNumbers=[2,3,13,18,-5,38,-10,11,0,104];
76
+
77
+
letevenNumberedArray=[];
78
+
79
+
letoddNumberedArray=[];
80
+
81
+
for(leti=0;i<arrayNumbers.length;i++){//PUT LESS THAN NOT <= or = BECAUSE WILL DO ONE EXTRA AND THATS WRONG
82
+
//console.log("index " + i);
83
+
//console.log(arrayNumbers[i]);
84
+
if(arrayNumbers[i]%2===0){
85
+
//console.log("EVE IN");
86
+
evenNumberedArray.push(arrayNumbers[i]);//TO PUT IN EVEN ARRAY THE ORIGINAL ARRAY INDEX OF EVEN NUMBERS
87
+
}else{
88
+
oddNumberedArray.push(arrayNumbers[i]);//WHERE TO GO + WHAT TO PUSH
0 commit comments