Skip to content

Commit 84db9dc

Browse files
committed
exercises finished
1 parent 68b3e8d commit 84db9dc

File tree

3 files changed

+76
-10
lines changed

3 files changed

+76
-10
lines changed

loops/exercises/for-Loop-Exercises.js

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,60 @@
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+
for (let i = 0; i < 21; i++){
8+
console.log(i)
9+
}
710

11+
for (let j = 3; j < 30; j = j+2){
12+
console.log(j);
13+
}
814

15+
for (let k = 12; k > -15; k = k-2){
16+
console.log(k)
17+
}
918

10-
/*Exercise #2:
11-
Initialize two variables to hold the string “LaunchCode” and the array [1, 5, ‘LC101’, ‘blue’, 42].
19+
for (let l = 50; l > 20; l--){
1220

21+
if (0 === (l % 3)){
22+
console.log(l)
23+
}
24+
}
1325

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. */
26+
//*Exercise #2:
27+
//Initialize two variables to hold the string “LaunchCode” and the array [1, 5, ‘LC101’, ‘blue’, 42].
1728

29+
let one = "LaunchCode";
30+
let two = [1, 5, 'LC101', 'blue', 42];
31+
console.log(one);
32+
console.log(two);
1833

34+
//Construct ``for`` loops to accomplish the following tasks:
35+
// a. Print each element of the array to a new line.
36+
//b. Print each character of the string - in reverse order - to a new line. */
37+
38+
for (let i = 0; i < two.length; i++){
39+
console.log(two[i]);
40+
}
41+
42+
43+
for (let j = 9; j >= 0; j--){
44+
console.log(one[j]);
45+
}
1946

2047

2148

2249
/*Exercise #3:Construct a for loop that sorts the array [2, 3, 13, 18, -5, 38, -10, 11, 0, 104] into two new arrays:
2350
a. One array contains the even numbers, and the other holds the odds.
24-
b. Print the arrays to confirm the results. */
51+
b. Print the arrays to confirm the results. */
52+
let even = []
53+
let odd = []
54+
let three = [2, 3, 13, 18, -5, 38, -10, 11, 0, 104]
55+
for (k = 0; k < three.length; k++){
56+
if (three[k] % 2 === 0){
57+
even.splice(0, 0, three[k])
58+
} else if (three[k] % 2 === 1) {
59+
odd.splice(0, 0, three[k])
60+
}
61+
}
62+
console.log(even);
63+
console.log(odd);

loops/exercises/package-lock.json

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,46 @@
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+
const input = require('readline.sync')
23

3-
4+
let startingFuelLevel = 20000
5+
let numberOfAstronauts = 6
6+
let totalAltitude = 0
47

58

69

710
/*Exercise #4: Construct while loops to do the following:
811
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. */
912

10-
13+
while (startingFuelLevel <= 5000 || startingFuelLevel >= 30000 || isNaN(startingFuelLevel)) {
14+
startingFuelLevel = input.question("What is the starting fuel level?")
15+
}
1116

1217

1318

1419
//b. Use a second loop to query the user for the number of astronauts (up to a maximum of 7). Validate the entry.
15-
20+
while (numberOfAstronauts >= 7 || numberOfAstronauts <= 1 || isNaN(numberOfAstronauts)) {
21+
numberOfAstronauts = input.question("How many astronauts are on board?")
22+
}
1623

1724

1825

1926
//c. Use a final loop to monitor the fuel status and the altitude of the shuttle. Each iteration, decrease the fuel level by 100 units for each astronaut aboard. Also, increase the altitude by 50 kilometers.
27+
while(startingFuelLevel-100*numberOfAstronauts >= 0){
28+
startingFuelLevel -= 100*numberOfAstronauts
29+
totalAltitude += 50
30+
return totalAltitude
31+
}
32+
33+
let output= `The shuttle gained an elevation of ${totalAltitude} km.`
2034

35+
if (totalAltitude >= 2000) {
36+
output += " Orbit achieved!"
37+
}
2138

39+
console.log(output)
2240

2341
/*Exercise #5: Output the result with the phrase, “The shuttle gained an altitude of ___ km.”
2442
25-
If the altitude is 2000 km or higher, add “Orbit achieved!” Otherwise add, “Failed to reach orbit.”*/
43+
If the altitude is 2000 km or higher, add “Orbit achieved!” Otherwise add, “Failed to reach orbit*/
44+
45+
46+

0 commit comments

Comments
 (0)