Skip to content

Commit 8fd920e

Browse files
author
Edward King
committed
Loop Exercises Update
1 parent c24f456 commit 8fd920e

File tree

2 files changed

+49
-12
lines changed

2 files changed

+49
-12
lines changed

loops/exercises/for-Loop-Exercises.js

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,49 @@
1919
}
2020
}*/
2121

22+
/*for (let i = 50; i > 19; i --){
23+
if (i % 3 === 0){
24+
console.log(i);
25+
}
26+
}*/
2227

2328
/*Exercise #2:
24-
Initialize two variables to hold the string “LaunchCode” and the array [1, 5, ‘LC101’, ‘blue’, 42].
29+
Initialize two variables to hold the string “LaunchCode” and the array [1, 5, ‘LC101’, ‘blue’, 42].*/
30+
str = "LaunchCode";
2531

32+
arr = [1 ,5, "LC101", "blue", 42];
2633

27-
Construct ``for`` loops to accomplish the following tasks:
34+
/*Construct ``for`` loops to accomplish the following tasks:
2835
a. Print each element of the array to a new line.
29-
b. Print each character of the string - in reverse order - to a new line. */
30-
36+
b. Print each character of the string - in reverse order - to a new line. */
3137

38+
/*for (i = 0; i < arr.length; i++){
39+
console.log(arr[i]);
40+
}*/
3241

42+
for (i = str.length-1; i >= 0; i -- ){
43+
console.log(str[i]);
44+
}
3345

3446

3547
/*Exercise #3:Construct a for loop that sorts the array [2, 3, 13, 18, -5, 38, -10, 11, 0, 104] into two new arrays:
3648
a. One array contains the even numbers, and the other holds the odds.
37-
b. Print the arrays to confirm the results. */
49+
b. Print the arrays to confirm the results. */
50+
51+
ogArr = [2, 3, 13, 18, -5, 38, -10, 11, 0, 104];
52+
evenArr = [];
53+
oddArr = [];
54+
55+
for (i = 0; i < ogArr.length; i++){
56+
if (ogArr[i] % 2 === 0){
57+
evenArr.push(ogArr[i]);
58+
}
59+
}
60+
console.log(evenArr);
61+
62+
for (i = 0; i < ogArr.length; i++){
63+
if (ogArr[i] % 2 !== 0){
64+
oddArr.push(ogArr[i]);
65+
}
66+
}
67+
console.log(oddArr);

loops/exercises/while-Loop-Exercises.js

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,31 @@
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-
2+
let startFuel = "";
3+
let numOfAstros = "";
4+
let shuttleAlt = "";
35

46

57

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. */
11+
const input = require('readline - sync');
912

13+
startFuel = Number(input.question("Whats the starting fuel level? "));
1014

11-
12-
15+
while (startFuel < 5000 || startFuel > 3000){
16+
startFuel = Number(input.question("Please try again...Whats the starting fuel level? "));
17+
}
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.
1520

16-
17-
18-
21+
numOfAstros = Number(input.question("How large is the crew? "));
22+
while (numOfAstros < 0 || numOfAstros > 7){
23+
Number(input.question("Please try again...How large is the crew? "));
24+
}
25+
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.
2027

21-
28+
while ()
2229

2330
/*Exercise #5: Output the result with the phrase, “The shuttle gained an altitude of ___ km.”
2431

0 commit comments

Comments
 (0)