Skip to content

Commit 082e655

Browse files
committed
Chapter 9 Studio
1 parent c4d67df commit 082e655

18 files changed

+178
-44
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
.DS_Store
1+
.DS_Store
2+
3+
node_modules

arrays/exercises/part-five-arrays.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ let str = 'In space, no one can hear you code.';
22
let arr = ['B', 'n', 'n', 5];
33

44
//1) Use the split method on the string to identify the purpose of the parameter inside the ().
5-
5+
console.log(str.split("."))
66
//2) Use the join method on the array to identify the purpose of the parameter inside the ().
7-
7+
console.log(arr.join("."))
88
//3) Do split or join change the original string/array?
9-
9+
// Yup they do
1010
//4) We can take a comma-separated string and convert it into a modifiable array. Try it! Alphabetize the cargoHold string, and then combine the contents into a new string.
1111
let cargoHold = "water,space suits,food,plasma sword,batteries";
12+
13+
console.log(cargoHold.split(",").sort().join(", "))

arrays/exercises/part-four-arrays.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@ let holdCabinet2 = ['orange drink', 'nerf toys', 'camera', 42, 'parsnip'];
44
//Explore the methods concat, slice, reverse, and sort to determine which ones alter the original array.
55

66
//1) Print the result of using concat on the two arrays. Does concat alter the original arrays? Verify this by printing holdCabinet1 after using the method.
7+
console.log(holdCabinet1.concat(holdCabinet2))
78

89
//2) Print a slice of two elements from each array. Does slice alter the original arrays?
9-
10+
console.log(holdCabinet1.splice(2,1))
11+
console.log(holdCabinet2.splice(2,1))
1012
//3) reverse the first array, and sort the second. What is the difference between these two methods? Do the methods alter the original arrays?
13+
holdCabinet1.reverse()
14+
console.log(holdCabinet1)
15+
holdCabinet2.sort()
16+
console.log(holdCabinet2)

arrays/exercises/part-one-arrays.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
//Create an array called practiceFile with the following entry: 273.15
2-
2+
let practiceFile = [273.15]
33
//Use the bracket notation method to add "42" and "hello" to the array. Add these new items one at a time. Print the array after each step to confirm the changes.
4-
4+
practiceFile.push(42)
5+
console.log(practiceFile)
6+
practiceFile.push("hello")
7+
console.log(practiceFile)
58
//Use a single .push() to add the following items: false, -4.6, and "87". Print the array to confirm the changes.
9+
practiceFile.push(false, -4.6, "87")
10+
console.log(practiceFile)

arrays/exercises/part-six-arrays.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
//Arrays can hold different data types, even other arrays! A multi-dimensional array is one with entries that are themselves arrays.
22

33
//1) Define and initialize the arrays specified in the exercise to hold the name, chemical symbol and mass for different elements.
4-
4+
let element1 = ['hydrogen', 'H', 1.008]
5+
let element2 = ['helium', 'He', 4.003]
6+
let element26 = ['iron', 'Fe', 55.85]
57
//2) Define the array 'table', and use 'push' to add each of the element arrays to it. Print 'table' to see its structure.
6-
8+
let table = element1.push[element2,element26]
9+
console.log(table[][])
710
//3) Use bracket notation to examine the difference between printing 'table' with one index vs. two indices (table[][]).
811

912
//4) Using bracket notation and the table array, print the mass of element1, the name for element 2 and the symbol for element26.
10-
13+
console.log(element1[0,2], element2 [1,2], element26[2,1])
1114
//5) 'table' is an example of a 2-dimensional array. The first “level” contains the element arrays, and the second level holds the name/symbol/mass values. Experiment! Create a 3-dimensional array and print out one entry from each level in the array.

arrays/exercises/part-three-arrays.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ let cargoHold = [1138, 'space suits', 'parrot', 'instruction manual', 'meal pack
33
//Use splice to make the following changes to the cargoHold array. Be sure to print the array after each step to confirm your updates.
44

55
//1) Insert the string 'keys' at index 3 without replacing any other entries.
6-
6+
cargoHold.splice(3,0,"keys")
7+
console.log(cargoHold)
78
//2) Remove ‘instruction manual’ from the array. (Hint: indexOf is helpful to avoid manually counting an index).
8-
9+
cargoHold.splice(4,1)
10+
console.log(cargoHold)
911
//3) Replace the elements at indexes 2 - 4 with the items ‘cat’, ‘fob’, and ‘string cheese’.
12+
cargoHold.splice(2,3, "cat","fob","string cheese")
13+
console.log(cargoHold)

arrays/exercises/part-two-arrays.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
let cargoHold = ['oxygen tanks', 'space suits', 'parrot', 'instruction manual', 'meal packs', 'slinky', 'security blanket'];
22

33
//1) Use bracket notation to replace ‘slinky’ with ‘space tether’. Print the array to confirm the change.
4-
4+
cargoHold[5] = "space tether"
5+
console.log("PART ONE\n\n" + cargoHold)
56
//2) Remove the last item from the array with pop. Print the element removed and the updated array.
6-
7+
console.log("PART TWO\n\n" + cargoHold.pop())
8+
console.log(cargoHold)
79
//3) Remove the first item from the array with shift. Print the element removed and the updated array.
8-
10+
console.log("PART THREE\n\n" + cargoHold.shift())
11+
console.log(cargoHold)
912
//4) Unlike pop and shift, push and unshift require arguments inside the (). Add the items 1138 and ‘20 meters’ to the the array - the number at the start and the string at the end. Print the updated array to confirm the changes.
10-
13+
cargoHold.unshift(1138)
14+
cargoHold.push("20 meters")
15+
console.log(cargoHold)
1116
//5) Use a template literal to print the final array and its length.
17+
console.log("PART THREE\n\n" + cargoHold + " has a length of " + cargoHold.length +" . ")

arrays/studio/array-string-conversion/array-testing.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,23 @@ let protoArray2 = "A;C;M;E";
33
let protoArray3 = "space delimited string";
44
let protoArray4 = "Comma-spaces, might, require, typing, caution";
55

6-
strings = [protoArray1, protoArray2, protoArray3, protoArray4];
6+
let strings = [protoArray1, protoArray2, protoArray3, protoArray4];
77

88
//2)
99
function reverseCommas() {
10-
//TODO: 1. create and instantiate your variables.
11-
let check;
12-
let output;
13-
//TODO: 2. write the code required for this step
14-
10+
//TODO: 1. create and instantiate your variables
11+
let check =
12+
let output =
13+
}
14+
//TODO: 2. write the code required for this step
15+
console.log(reverseCommas())
1516
//NOTE: For the code to run properly, you must return your output. this needs to be the final line of code within the function's { }.
1617
return output;
17-
}
18+
1819

1920
//3)
2021
function semiDash() {
21-
let check;
22+
let check;
2223
let output;
2324
//TODO: write the code required for this step
2425

arrays/studio/multi-dimensional-arrays.js

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,46 @@ let food = "water bottles,meal packs,snacks,chocolate";
22
let equipment = "space suits,jet packs,tool belts,thermal detonators";
33
let pets = "parrots,cats,moose,alien eggs";
44
let sleepAids = "blankets,pillows,eyepatches,alarm clocks";
5+
let cargoHold = [[food], [equipment], [pets], [sleepAids]]
6+
let input = require('readline-sync');
7+
let selectCabinet = input.question("Select a cabinet.")
8+
let itemCabinet = input.question("Check cabinet for item");
59

610
//1) Use split to convert the strings into four cabinet arrays. Alphabetize the contents of each cabinet.
711

12+
813
//2) Initialize a cargoHold array and add the cabinet arrays to it. Print cargoHold to verify its structure.
14+
cargoHold.concat[food, equipment, pets, sleepAids]
915

1016
//3) Query the user to select a cabinet (0 - 3) in the cargoHold.
11-
17+
console.log(`${selectCabinet}${input}`)
1218
//4) Use bracket notation and a template literal to display the contents of the selected cabinet. If the user entered an invalid number, print an error message.
19+
if (input === "0") {
20+
console.log(`Here you go\n${cargoHold[0]}`)
21+
} if (input === "1") {
22+
console.log(`Here you go\n${cargoHold[1]}`)
23+
} if (input === "2") {
24+
console.log(`Here you go\n${cargoHold[2]}`)
25+
} if (input === "3") {
26+
console.log(`Here you go\n${cargoHold[3]}`)
27+
} else {
28+
console.log(`Get out.`)
29+
}
1330

1431
//5) Modify the code to query the user for BOTH a cabinet in cargoHold AND a particular item. Use the 'includes' method to check if the cabinet contains the selected item, then print “Cabinet ____ DOES/DOES NOT contain ____.”
32+
33+
//select for a cabinet and item
34+
console.log(`${itemCabinet}${input}`)
35+
36+
//state it does or does not have item in cabinet
37+
if (input === "0") {
38+
console.log(`Here you go\n${cargoHold[0]}`)
39+
} if (input === "1") {
40+
console.log(`Here you go\n${cargoHold[1]}`)
41+
} if (input === "2") {
42+
console.log(`Here you go\n${cargoHold[2]}`)
43+
} if (input === "3") {
44+
console.log(`Here you go\n${cargoHold[3]}`)
45+
} else {
46+
console.log(`Get out.`)
47+
}

arrays/studio/package-lock.json

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

arrays/studio/string-modification.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
1-
const input = require('readline-sync');
1+
let input = require('readline-sync');
22
let str = "LaunchCode";
3-
3+
let strNew = (str.split("").splice(3,12).join(""))
4+
let question = input.question("How many letters will be relocated?")
45
//1) Use string methods to remove the first three characters from the string and add them to the end.
56
//Hint - define another variable to hold the new string or reassign the new string to str.
67

78
//Use a template literal to print the original and modified string in a descriptive phrase.
89

910
//2) Modify your code to accept user input. Query the user to enter the number of letters that will be relocated.
10-
11+
console.log(`I am going to print two strings, watch: ${str} and ${strNew}. ${question}`)
1112
//3) Add validation to your code to deal with user inputs that are longer than the word. In such cases, default to moving 3 characters. Also, the template literal should note the error.
13+
if (question < str.length) {
14+
strNew = (str.split("").splice(question).join(""))
15+
console.log(`Look at what you did. ${strNew}.`)
16+
} else {
17+
(console.log(`Wrong`))
18+
}
19+
20+
21+
22+

errors-and-debugging/exercises/Debugging1stSyntaxError.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
let launchReady = false;
55
let fuelLevel = 17000;
66

7-
if (fuelLevel >= 20000 {
7+
if (fuelLevel >= 20000) {
88
console.log('Fuel level cleared.');
99
launchReady = true;
1010
} else {

errors-and-debugging/exercises/DebuggingLogicErrors1.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Run this sample code as-is and examine the output.
22
// Should the shuttle have launched?
33
// Did it?
4+
// I don't think so
45
// Do not worry about fixing the code yet, we will do that in the next series of exercises.
56

67
let launchReady = false;

errors-and-debugging/exercises/DebuggingLogicErrors2.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// Add console.log(launchReady) after this block, then run the program.
44

55
//Given the fuelLevel value, should launchReady be true or false after the check? Is the program behaving as expected?
6-
6+
// Yes the fuel level is lower than the required level in the if/else statement
77
let launchReady = false;
88
let fuelLevel = 17000;
99
// let crewStatus = true;
@@ -17,6 +17,8 @@ if (fuelLevel >= 20000) {
1717
launchReady = false;
1818
}
1919

20+
console.log(launchReady)
21+
2022
// if (crewStatus && computerStatus === 'green'){
2123
// console.log('Crew & computer cleared.');
2224
// launchReady = true;

errors-and-debugging/exercises/DebuggingLogicErrors3.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
// Given the values for crewStatus and computerStatus, should launchReady be true or false after the check?
66
// Is the program behaving as expected?
7-
7+
// No because the crew status isn't acknowledged in the if/else, only the computer status is
88
let launchReady = false;
99
// let fuelLevel = 17000;
1010
let crewStatus = true;
@@ -26,6 +26,8 @@ if (crewStatus && computerStatus === 'green'){
2626
launchReady = false;
2727
}
2828

29+
console.log(launchReady)
30+
2931
// if (launchReady) {
3032
// console.log('10, 9, 8, 7, 6, 5, 4, 3, 2, 1...');
3133
// console.log('Liftoff!');

loops/exercises/for-Loop-Exercises.js

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,52 @@
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+
}
14+
15+
for (let i = 12; i > -15; i = i - 2) {
16+
console.log(i);
17+
}
18+
19+
for (let i = 48; i > 19; i = i - 3) {
20+
console.log(i)
21+
}
822

923

1024
/*Exercise #2:
11-
Initialize two variables to hold the string “LaunchCode” and the array [1, 5, ‘LC101’, ‘blue’, 42].
25+
Initialize two variables to hold the string “LaunchCode” and the array [1, 5, ‘LC101’, ‘blue’, 42].*/
1226

27+
let arr = [1, 5, 'LC101', 'blue', 42]
28+
let lc = "LaunchCode";
1329

14-
Construct ``for`` loops to accomplish the following tasks:
30+
/*Construct ``for`` loops to accomplish the following tasks:
1531
a. Print each element of the array to a new line.
1632
b. Print each character of the string - in reverse order - to a new line. */
1733

34+
for(i = 0; i < arr.length; i++) {
35+
console.log(arr[i])
36+
}
1837

19-
38+
for(i = 0; i < arr.length; i++) {
39+
console.log(arr.join(""))
40+
}
2041

2142

2243
/*Exercise #3:Construct a for loop that sorts the array [2, 3, 13, 18, -5, 38, -10, 11, 0, 104] into two new arrays:
2344
a. One array contains the even numbers, and the other holds the odds.
24-
b. Print the arrays to confirm the results. */
45+
b. Print the arrays to confirm the results. */
46+
47+
let otherArr = [2, 3, 13, 18, -5, 38, -10, 11, 0, 104]
48+
let evens = []
49+
let odds = []
50+
51+
for(i = 0; i < [10]; i++) {
52+
evens = (otherArr[1 + 2])
53+
odds = (otherArr[2 + 2])
54+
console.log(otherArr)
55+
}

loops/exercises/while-Loop-Exercises.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,28 @@
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-
4-
5-
3+
const input = require('readline-sync')
4+
let fuelLevel = 0
5+
let astronautsAboard = 0
6+
let altitudeKm = 0
67

78
/*Exercise #4: Construct while loops to do the following:
89
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-
10+
11+
while (fuelLevel <= 5000 || fuelLevel > 30000 || isNaN(fuelLevel)) {
12+
fuelLevel = input.question("Enter the starting fuel level: ");
13+
}
1314

1415
//b. Use a second loop to query the user for the number of astronauts (up to a maximum of 7). Validate the entry.
1516

16-
17+
while (astronautsAboard <= 1 || astronautsAboard > 7 || isNaN(astronautsAboard)) {
18+
astronautsAboard = input.question("Enter the starting fuel level: ");
19+
}
20+
1721

1822

1923
//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.
2024

25+
while ()
2126

2227

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

loops/studio/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
const shuttleManagement = require('./solution.js');
22

3-
shuttleManagement.runProgram();
3+
shuttleManagement.runProgram()

0 commit comments

Comments
 (0)