Skip to content

Commit 64449c1

Browse files
committed
for loops and while loops completed
1 parent 568dbf8 commit 64449c1

File tree

3 files changed

+67
-9
lines changed

3 files changed

+67
-9
lines changed

loops/exercises/for-Loop-Exercises.js

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,23 @@
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 i = 3; i < 30; i = i + 2) {
12+
console.log(i)
13+
}
814

15+
for (let i = 12; i >= -14; i = i - 2){
16+
console.log(i)
17+
}
18+
19+
for (let i = 50; i >= 20; i--) {
20+
if (i % 3 == 0) {
21+
console.log(i)
22+
}
23+
}
924

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

33+
let exerStr = 'LaunchCode'
34+
let exerArr = [1, 5, 'LC101', 'blue', 42]
35+
36+
for (let i = 0; i < exerArr.length; i++) {
37+
console.log(exerArr[i])
38+
}
1839

40+
for (let i = 9; i >= 0; i--) {
41+
console.log(exerStr[i])
42+
}
1943

2044

2145

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

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: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,39 @@
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.
22

3+
const input = require('readline-sync');
34

4-
5-
5+
let startFuel = 0
6+
let astronautsAboard = 0
7+
let altitude = 0
68

79
/*Exercise #4: Construct while loops to do the following:
810
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. */
911

10-
11-
12-
12+
while (startFuel <=5000 || startFuel > 30000 || isNaN(startFuel)) {
13+
startFuel = input.question("Enter a starting fuel level: ")
14+
}
1315

1416
//b. Use a second loop to query the user for the number of astronauts (up to a maximum of 7). Validate the entry.
15-
16-
17-
17+
18+
while (astronautsAboard < 1 || astronautsAboard > 7) {
19+
astronautsAboard = input.question("How many astronauts are aboard the shuttle? ")
20+
}
1821

1922
//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.
2023

24+
while (startFuel > 100 * astronautsAboard) {
25+
startFuel -= (100 * astronautsAboard);
26+
altitude += 50;
27+
}
2128

29+
console.log(`The shuttle gained and altitude of ${altitude} km.`)
2230

2331
/*Exercise #5: Output the result with the phrase, “The shuttle gained an altitude of ___ km.”
2432
2533
If the altitude is 2000 km or higher, add “Orbit achieved!” Otherwise add, “Failed to reach orbit.”*/
34+
35+
if (altitude >= 2000) {
36+
console.log('Orbit achieved!')
37+
} else {
38+
console.log('Failed to reach orbit')
39+
}

0 commit comments

Comments
 (0)