Skip to content

Commit 6b2568b

Browse files
committed
Completed 'EXERCISES: LOOPS, while-Loop-Exercises.js'
Completed 'STUDIO: LOOPS'
1 parent 4c7a45f commit 6b2568b

3 files changed

Lines changed: 93 additions & 37 deletions

File tree

loops/exercises/package.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "loops",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "while-Loop-Exercises.js",
6+
"author": "",
7+
"license": "ISC",
8+
"dependencies": {
9+
"readline-sync": "^1.4.10"
10+
}
11+
}

loops/exercises/while-Loop-Exercises.js

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,45 @@
1-
//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-
3-
4-
1+
const INPUT = require('readline-sync');
2+
const MIN_FUEL_LEVEL = 5001;
3+
const MAX_FUEL_LEVEL = 29999;
4+
const MAX_NUMBER_ASTRONAUTS = 7;
5+
const FUEL_USED_PER_ASTRONAUT = 100;
6+
const ALTITUDE_UPDATES_KM = 50;
7+
const ORBIT_ALTITUDE_KM = 2000;
58

9+
//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.
10+
let startingFuelLevel = 0;
11+
let numberOfAstronauts = -1;
12+
let altitudeShuttleReachesKM = 0;
13+
let userInput = '';
614

715
/*Exercise #4: Construct while loops to do the following:
816
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. */
9-
10-
11-
12-
17+
while (!(Number.isInteger(startingFuelLevel) && startingFuelLevel >= MIN_FUEL_LEVEL && startingFuelLevel <= MAX_FUEL_LEVEL)) {
18+
userInput = INPUT.question(`What is the starting fuel level (positive interger greater than ${MIN_FUEL_LEVEL} but less than ${MAX_FUEL_LEVEL}): `);
19+
startingFuelLevel = Number(userInput);
20+
}
21+
console.log(`Starting fuel level is ${startingFuelLevel}.`)
1322

1423
//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-
18-
19-
//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.
20-
24+
while (!(Number.isInteger(numberOfAstronauts) && numberOfAstronauts >= 1 && numberOfAstronauts <= MAX_NUMBER_ASTRONAUTS)) {
25+
userInput = INPUT.question(`What is the number of astronauts on board (up to ${MAX_NUMBER_ASTRONAUTS}): `);
26+
numberOfAstronauts = Number(userInput);
27+
}
28+
console.log(`Number of astronauts is ${numberOfAstronauts}`);
2129

30+
//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.
31+
let fuelLevel = startingFuelLevel;
32+
while (fuelLevel > 0 && altitudeShuttleReachesKM < ORBIT_ALTITUDE_KM) {
33+
fuelLevel -= FUEL_USED_PER_ASTRONAUT * numberOfAstronauts;
34+
altitudeShuttleReachesKM += ALTITUDE_UPDATES_KM;
35+
console.log(`The shuttle gained an altitude of ${altitudeShuttleReachesKM} km`);
36+
}
37+
if (altitudeShuttleReachesKM >= ORBIT_ALTITUDE_KM) {
38+
console.log("Orbit achieved!");
39+
}
40+
else {
41+
console.log("Failed to reach orbit.");
42+
}
2243

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

loops/studio/solution.js

Lines changed: 47 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,62 @@ const input = require('readline-sync');
22

33
// Part A: #1 Populate these arrays
44

5-
let protein = [];
6-
let grains = [];
7-
let veggies = [];
8-
let beverages = [];
9-
let desserts = [];
5+
let protein = ['chicken', 'pork', 'tofu', 'beef', 'fish', 'beans'];
6+
let grains = ['rice', 'pasta', 'corn', 'potato', 'quinoa', 'crackers'];
7+
let veggies = ['peas', 'green beans', 'kale', 'edamame', 'broccoli', 'asparagus'];
8+
let beverages = ['juice', 'milk', 'water', 'soy milk', 'soda', 'tea'];
9+
let desserts = ['apple', 'banana', 'more kale', 'ice cream', 'chocolate', 'kiwi'];
1010

1111

1212
function mealAssembly(protein, grains, veggies, beverages, desserts, numMeals) {
1313
let pantry = [protein, grains, veggies, beverages, desserts];
1414
let meals = [];
1515

16-
/// Part A #2: Write a ``for`` loop inside this function
17-
/// Code your solution for part A #2 below this comment (and above the return statement) ... ///
18-
19-
16+
let maxMeals = Math.max(protein.length, grains.length, veggies.length, beverages.length, desserts.length);
17+
let loopEnd = maxMeals < numMeals ? maxMeals : numMeals;
18+
// Make at most 'maxMeals'.
19+
for (let i = 0; i < loopEnd ; i++) {
20+
let meal = [];
21+
// Go through 'pantry' pulling an item from each food group and add it to the current meal.
22+
for (let j = 0; j < pantry.length; j++) {
23+
meal.push(pantry[j][i]);
24+
}
25+
// Add completed meal to meals array.
26+
meals.push(meal);
27+
}
2028
return meals;
2129
}
2230

2331

2432
function askForNumber() {
25-
numMeals = input.question("How many meals would you like to make?");
26-
27-
/// CODE YOUR SOLUTION TO PART B here ///
28-
33+
let numMeals = -1;
34+
// Keep asking for a number until we get an integer between 1 and 6.
35+
while (!Number.isInteger(numMeals) || numMeals < 1 || numMeals > 6) {
36+
numMeals = Number(input.question("How many meals would you like to make [1-6]? "));
37+
if (numMeals < 1 || numMeals > 6) {
38+
console.log("Please enter integer between 1 and 6.");
39+
}
40+
}
2941
return numMeals;
3042
}
3143

3244

3345
function generatePassword(string1, string2) {
3446
let code = '';
35-
36-
/// Code your Bonus Mission Solution here ///
47+
let numLoops = string2.length < string1.length ? string2.length : string1.length;
48+
let i = 0;
49+
// Add characters to 'code' alternately from 'string1' and 'string2'.
50+
for (i = 0; i < numLoops; i++) {
51+
code += string1[i] + string2[i];
52+
}
53+
// Add (any) leftover string1 characters to 'code'.
54+
if (string1.length > i) {
55+
code += string1.slice(i);
56+
}
57+
// Add (any) leftover string2 characters to 'code'.
58+
if (string2.length > i) {
59+
code += string2.slice(i);
60+
}
3761

3862
return code;
3963
}
@@ -45,24 +69,24 @@ function runProgram() {
4569
/// Change the final input variable (aka numMeals) here to ensure your solution makes the right number of meals ///
4670
/// We've started with the number 2 for now. Does your solution still work if you change this value? ///
4771

48-
// let meals = mealAssembly(protein, grains, veggies, beverages, desserts, 2);
49-
// console.log(meals)
72+
let meals = mealAssembly(protein, grains, veggies, beverages, desserts, 2);
73+
console.log(meals)
5074

5175

5276
/// TEST PART B HERE ///
5377
/// UNCOMMENT the next two lines to test your ``askForNumber`` solution ///
5478
/// Tip - don't test this part until you're happy with your solution to part A #2 ///
5579

56-
// let mealsForX = mealAssembly(protein, grains, veggies, beverages, desserts, askForNumber());
57-
// console.log(mealsForX);
80+
let mealsForX = mealAssembly(protein, grains, veggies, beverages, desserts, askForNumber());
81+
console.log(mealsForX);
5882

5983
/// TEST PART C HERE ///
6084
/// UNCOMMENT the remaining commented lines and change the password1 and password2 strings to ensure your code is doing its job ///
6185

62-
// let password1 = '';
63-
// let password2 = '';
64-
// console.log("Time to run the password generator so we can update the menu tomorrow.")
65-
// console.log(`The new password is: ${generatePassword(password1, password2)}`);
86+
let password1 = 'ABCDEFGH';
87+
let password2 = '1234567';
88+
console.log("Time to run the password generator so we can update the menu tomorrow.")
89+
console.log(`The new password is: ${generatePassword(password1, password2)}`);
6690
}
6791

6892
module.exports = {

0 commit comments

Comments
 (0)