Skip to content

Commit 35439f3

Browse files
committed
finished exercises
1 parent a123253 commit 35439f3

3 files changed

Lines changed: 68 additions & 7 deletions

File tree

loops/exercises/for-Loop-Exercises.js

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,28 @@
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). */
66

7-
8-
7+
//a
8+
for (i = 0; i<21; i++) {
9+
console.log(i);
10+
}
11+
//b
12+
for (let i=3; i<30; i++) {
13+
if (i%2 !== 0) {
14+
console.log(i)
15+
}
16+
}
17+
//c
18+
for (let i=12; i>-15; i--) {
19+
if (i%2 == 0) {
20+
console.log(i)
21+
}
22+
}
23+
//d
24+
for (let i = 50; i>19; i--) {
25+
if (i%3 == 0) {
26+
console.log(i)
27+
}
28+
}
929

1030
/*Exercise #2:
1131
Initialize two variables to hold the string “LaunchCode” and the array [1, 5, ‘LC101’, ‘blue’, 42].
@@ -15,10 +35,30 @@ Construct ``for`` loops to accomplish the following tasks:
1535
a. Print each element of the array to a new line.
1636
b. Print each character of the string - in reverse order - to a new line. */
1737

18-
19-
20-
21-
38+
let str = "Launchcode"
39+
let arr = [1, 5, 'LC101', 'blue', 42]
40+
//a
41+
for (let i=0; i<4; i++) {
42+
console.log(arr[i])
43+
}
44+
//b
45+
for (let i=10; i>=0; i--) {
46+
console.log(str[i])
47+
}
2248
/*Exercise #3:Construct a for loop that sorts the array [2, 3, 13, 18, -5, 38, -10, 11, 0, 104] into two new arrays:
2349
a. One array contains the even numbers, and the other holds the odds.
24-
b. Print the arrays to confirm the results. */
50+
b. Print the arrays to confirm the results. */
51+
52+
let arr2 = [2, 3, 13, 18, -5, 38, -10, 11, 0, 104]
53+
let newArr1= []
54+
let newArr2 = []
55+
56+
for (i=0; i<10; i++) {
57+
if (i%2 == 0) {
58+
newArr1.push(i)
59+
}
60+
else {
61+
newArr2.push(i)
62+
}
63+
}
64+
console.log(newArr1, newArr2);

loops/exercises/index.js

Whitespace-only changes.

loops/exercises/while-Loop-Exercises.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,30 @@
11
//Define three variables for the LaunchCode shuttle - one for the starting fuel level, another for the number of astronauts aboard, and the third for the altitude the shuttle reaches.
2+
let fuelLevel = 0;
3+
let numOfAstronauts = 0;
4+
let shuttleAltitude = 0;
25

6+
const input = require('readline-sync');
37

8+
while (fuelLevel <= 5000 || fuelLevel > 30000) {
9+
fuelLevel = input.question("Enter starting fuel level:\n ");
410

11+
}
512

13+
while (numOfAstronauts < 1 || numOfAstronauts > 7) {
14+
numOfAstronauts = input.question("Enter number of taikonauts:\n")
15+
}
616

17+
while (fuelLevel - 100*numOfAstronauts >= 0) {
18+
shuttleAltitude += 50;
19+
fuelLevel -= 100*numOfAstronauts;
20+
}
21+
console.log(`The shuttle gained an altitude of ${shuttleAltitude}km.`);
22+
23+
if (shuttleAltitude >= 2000) {
24+
console.log("Orbit achieved!");
25+
} else {
26+
console.log("Failed to reach orbit.");
27+
}
728
/*Exercise #4: Construct while loops to do the following:
829
a. Query the user for the starting fuel level. Validate that the user enters a positive, integer value greater than 5000 but less than 30000. */
930

0 commit comments

Comments
 (0)