Skip to content

Commit ed9cf58

Browse files
Adding exercises and studio
1 parent 3835f13 commit ed9cf58

6 files changed

Lines changed: 230 additions & 0 deletions

File tree

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*Exercise #1: Construct for loops that accomplish the following tasks:
2+
a. Print the numbers 0 - 20, one number per line.
3+
b. Print only the ODD values from 3 - 29, one number per line.
4+
c. Print the EVEN numbers 12 to -14 in descending order, one number per line.
5+
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). */
6+
7+
8+
9+
10+
/*Exercise #2:
11+
Initialize two variables to hold the string “LaunchCode” and the array [1, 5, ‘LC101’, ‘blue’, 42].
12+
13+
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. */
17+
18+
19+
20+
21+
22+
/*Exercise #3:Construct a for loop that sorts the array [2, 3, 13, 18, -5, 38, -10, 11, 0, 104] into two new arrays:
23+
a. One array contains the even numbers, and the other holds the odds.
24+
b. Print the arrays to confirm the results. */
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
5+
6+
7+
/*Exercise #4: Construct while loops to do the following:
8+
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+
13+
14+
//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+
21+
22+
23+
/*Exercise #5: Output the result with the phrase, “The shuttle gained an altitude of ___ km.”
24+
25+
If the altitude is 2000 km or higher, add “Orbit achieved!” Otherwise add, “Failed to reach orbit.”*/

loops/studio/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const shuttleManagement = require('./solution.js');
2+
3+
shuttleManagement.runProgram();

loops/studio/package.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "loops",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "solution.js",
6+
"scripts": {
7+
"test": "jest"
8+
},
9+
"author": "",
10+
"license": "ISC",
11+
"dependencies": {
12+
"readline-sync": "^1.4.10"
13+
},
14+
"devDependencies": {
15+
"jest": "^29.6.1"
16+
}
17+
}

loops/studio/solution.js

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
const input = require('readline-sync');
2+
3+
// Part A: #1 Populate these arrays
4+
5+
let protein = [];
6+
let grains = [];
7+
let veggies = [];
8+
let beverages = [];
9+
let desserts = [];
10+
11+
12+
function mealAssembly(protein, grains, veggies, beverages, desserts, numMeals) {
13+
let pantry = [protein, grains, veggies, beverages, desserts];
14+
let meals = [];
15+
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+
20+
return meals;
21+
}
22+
23+
24+
function askForNumber() {
25+
numMeals = input.question("How many meals would you like to make?");
26+
27+
/// CODE YOUR SOLUTION TO PART B here ///
28+
29+
return numMeals;
30+
}
31+
32+
33+
function generatePassword(string1, string2) {
34+
let code = '';
35+
36+
/// Code your Bonus Mission Solution here ///
37+
38+
return code;
39+
}
40+
41+
function runProgram() {
42+
43+
/// TEST PART A #2 HERE ///
44+
/// UNCOMMENT the two lines of code below that invoke the mealAssembly function (starting with 'let meals =') and print the result ///
45+
/// Change the final input variable (aka numMeals) here to ensure your solution makes the right number of meals ///
46+
/// We've started with the number 2 for now. Does your solution still work if you change this value? ///
47+
48+
// let meals = mealAssembly(protein, grains, veggies, beverages, desserts, 2);
49+
// console.log(meals)
50+
51+
52+
/// TEST PART B HERE ///
53+
/// UNCOMMENT the next two lines to test your ``askForNumber`` solution ///
54+
/// Tip - don't test this part until you're happy with your solution to part A #2 ///
55+
56+
// let mealsForX = mealAssembly(protein, grains, veggies, beverages, desserts, askForNumber());
57+
// console.log(mealsForX);
58+
59+
/// TEST PART C HERE ///
60+
/// UNCOMMENT the remaining commented lines and change the password1 and password2 strings to ensure your code is doing its job ///
61+
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)}`);
66+
}
67+
68+
module.exports = {
69+
protein: protein,
70+
grains: grains,
71+
veggies: veggies,
72+
beverages: beverages,
73+
desserts: desserts,
74+
mealAssembly: mealAssembly,
75+
askForNumber: askForNumber,
76+
generatePassword: generatePassword,
77+
runProgram: runProgram
78+
};

loops/studio/spec/solution.spec.js

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
const studentsolution = require('../solution.js');
2+
3+
describe ("Loops studio solution", function() {
4+
5+
let protein, grains, veggies, beverages, desserts;
6+
7+
beforeAll(async function () {
8+
protein = studentsolution.protein;
9+
grains = studentsolution.grains;
10+
veggies = studentsolution.veggies;
11+
beverages = studentsolution.beverages;
12+
desserts = studentsolution.desserts;
13+
});
14+
15+
16+
it("desserts is initialized", function() {
17+
expect(desserts).toContain("apple");
18+
expect(desserts).toContain("banana");
19+
expect(desserts).toContain("more kale");
20+
expect(desserts).toContain("ice cream");
21+
expect(desserts).toContain("chocolate");
22+
expect(desserts).toContain("kiwi");
23+
expect(desserts.length).toBe(6);
24+
});
25+
26+
it("beverages is initialized", function() {
27+
expect(beverages).toContain("juice");
28+
expect(beverages).toContain("milk");
29+
expect(beverages).toContain("water");
30+
expect(beverages).toContain("soy milk");
31+
expect(beverages).toContain("soda");
32+
expect(beverages).toContain("tea");
33+
expect(beverages.length).toBe(6);
34+
});
35+
36+
it("protein is initialized", function () {
37+
expect(protein).toContain("chicken");
38+
expect(protein).toContain("pork");
39+
expect(protein).toContain("tofu");
40+
expect(protein).toContain("beef");
41+
expect(protein).toContain("fish");
42+
expect(protein).toContain("beans");
43+
expect(protein.length).toBe(6);
44+
});
45+
46+
it("grains is initialized", function() {
47+
expect(grains).toContain("rice");
48+
expect(grains).toContain("pasta");
49+
expect(grains).toContain("corn");
50+
expect(grains).toContain("potato");
51+
expect(grains).toContain("quinoa");
52+
expect(grains).toContain("crackers");
53+
expect(grains.length).toBe(6);
54+
});
55+
56+
it("veggies is initialized", function() {
57+
expect(veggies).toContain("peas");
58+
expect(veggies).toContain("green beans");
59+
expect(veggies).toContain("kale");
60+
expect(veggies).toContain("edamame");
61+
expect(veggies).toContain("broccoli");
62+
expect(veggies).toContain("asparagus");
63+
expect(veggies.length).toBe(6);
64+
});
65+
66+
it("mealAssembly produces proper number and size of meals", function() {
67+
let testMeals1 = studentsolution.mealAssembly(protein, grains, veggies, beverages, desserts, 6);
68+
expect(testMeals1.length).toBe(6);
69+
expect(testMeals1[0].length).toBe(5);
70+
let testMeals2 = studentsolution.mealAssembly(protein, grains, veggies, beverages, desserts, 3);
71+
expect(testMeals2.length).toBe(3);
72+
expect(testMeals2[0].length).toBe(5);
73+
let testMeals3 = studentsolution.mealAssembly(protein, grains, veggies, beverages, desserts, 4);
74+
expect(testMeals3.length).toBe(4);
75+
expect(testMeals3[3].length).toBe(5);
76+
});
77+
78+
/// BONUS MISSION TEST ///
79+
80+
// it("generatePassword returns jumbled words", function() {
81+
// expect(studentsolution.generatePassword("LoOt", "oku!")).toEqual("LookOut!");
82+
// })
83+
});

0 commit comments

Comments
 (0)