Skip to content

Commit e25edc4

Browse files
committed
For Loop V1
1 parent b5c397d commit e25edc4

File tree

1 file changed

+87
-4
lines changed

1 file changed

+87
-4
lines changed

loops/exercises/for-Loop-Exercises.js

Lines changed: 87 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,107 @@
11
/*Exercise #1: Construct for loops that accomplish the following tasks:
22
a. Print the numbers 0 - 20, one number per line.
3+
for (let i = 0; i <= 20; i++) {
4+
console.log(i);
5+
}
36
b. Print only the ODD values from 3 - 29, one number per line.
47
c. Print the EVEN numbers 12 to -14 in descending order, one number per line.
58
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). */
69

10+
for (let i = 0; i <= 20; i++) {//<= includes the number
11+
console.log(i);
12+
}
713

14+
console.log();
15+
16+
for (let i = 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 (let i = 12; i >= -14; i -=2) {//-= is descending and icnriment of 2
25+
console.log(i);
26+
}
27+
28+
for (let i = 50; i >=20; i --){//-- is DESCENDDING
29+
if (i % 3 === 0) {
30+
console.log(i);
31+
}
32+
}
33+
34+
console.log();
835

936

1037
/*Exercise #2:
1138
Initialize two variables to hold the string “LaunchCode” and the array [1, 5, ‘LC101’, ‘blue’, 42].
39+
*/
40+
41+
let varOne = "LaunchCode";
1242

1343

14-
Construct ``for`` loops to accomplish the following tasks:
44+
let cuteArray = [1, 5, "LC101", "blue", 42];
45+
46+
/*Construct ``for`` loops to accomplish the following tasks:
1547
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 (let i = 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+
}
1756

57+
console.log();
58+
1859

60+
1961

62+
for (let i = varOne.length-1; i >= 0; i--) {//read as undefined so use -1 because was donig 1 extra
63+
console.log(varOne[i]);
64+
}
2065

66+
console.log();
2167

2268
/*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+
let arrayNumbers = [2, 3, 13, 18, -5, 38, -10, 11, 0, 104];
76+
77+
let evenNumberedArray = [];
78+
79+
let oddNumberedArray = [];
80+
81+
for (let i = 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
89+
}
90+
}
91+
console.log(evenNumberedArray);
92+
console.log("=============================");
93+
console.log(oddNumberedArray);
94+
95+
96+
97+
98+
99+
/*
100+
)
101+
102+
103+
//for (let i = 0; i = ""; i++) {
104+
//if (numbArray % 2 === 0) {
105+
//console.log(numbArray);
106+
107+
*/

0 commit comments

Comments
 (0)