Skip to content

Commit 52b5d60

Browse files
Merge pull request LaunchCodeEducation#8 from LaunchCodeEducation/array-exercises-studios
Array exercises studios
2 parents abdd18f + fec3ae2 commit 52b5d60

13 files changed

+200
-0
lines changed

arrays/exercises/part-five-arrays.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
let str = 'In space, no one can hear you code.';
2+
let arr = ['B', 'n', 'n', 5];
3+
4+
//1) Use the split method on the string to identify the purpose of the parameter inside the ().
5+
6+
//2) Use the join method on the array to identify the purpose of the parameter inside the ().
7+
8+
//3) Do split or join change the original string/array?
9+
10+
//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.
11+
let cargoHold = "water,space suits,food,plasma sword,batteries";

arrays/exercises/part-four-arrays.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
let holdCabinet1 = ['duct tape', 'gum', 3.14, false, 6.022e23];
2+
let holdCabinet2 = ['orange drink', 'nerf toys', 'camera', 42, 'parsnip'];
3+
4+
//Explore the methods concat, slice, reverse, and sort to determine which ones alter the original array.
5+
6+
//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+
8+
//2) Print a slice of two elements from each array. Does slice alter the original arrays?
9+
10+
//3) reverse the first array, and sort the second. What is the difference between these two methods? Do the methods alter the original arrays?

arrays/exercises/part-one-arrays.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
//Create an array that can hold 4 items name practiceFile.
2+
3+
//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+
5+
//Use a SetValue to add the items "false", and "-4.6" to the array. Print the array to confirm the changes.

arrays/exercises/part-six-arrays.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//Arrays can hold different data types, even other arrays! A multi-dimensional array is one with entries that are themselves arrays.
2+
3+
//1) Define and initialize the arrays specified in the exercise to hold the name, chemical symbol and mass for different elements.
4+
5+
//2) Define the array 'table', and use 'push' to add each of the element arrays to it. Print 'table' to see its structure.
6+
7+
//3) Use bracket notation to examine the difference between printing 'table' with one index vs. two indices (table[][]).
8+
9+
//4) Using bracket notation and the table array, print the mass of element1, the name for element 2 and the symbol for element26.
10+
11+
//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: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
let cargoHold = [1138, 'space suits', 'parrot', 'instruction manual', 'meal packs', 'space tether', '20 meters'];
2+
3+
//Use splice to make the following changes to the cargoHold array. Be sure to print the array after each step to confirm your updates.
4+
5+
//1) Insert the string 'keys' at index 3 without replacing any other entries.
6+
7+
//2) Remove ‘instruction manual’ from the array. (Hint: indexOf is helpful to avoid manually counting an index).
8+
9+
//3) Replace the elements at indexes 2 - 4 with the items ‘cat’, ‘fob’, and ‘string cheese’.

arrays/exercises/part-two-arrays.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
let cargoHold = ['oxygen tanks', 'space suits', 'parrot', 'instruction manual', 'meal packs', 'slinky', 'security blanket'];
2+
3+
//1) Use bracket notation to replace ‘slinky’ with ‘space tether’. Print the array to confirm the change.
4+
5+
//2) Remove the last item from the array with pop. Print the element removed and the updated array.
6+
7+
//3) Remove the first item from the array with shift. Print the element removed and the updated array.
8+
9+
//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+
11+
//5) Use a template literal to print the final array and its length.
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
let protoArray1 = "3,6,9,12";
2+
let protoArray2 = "A;C;M;E";
3+
let protoArray3 = "space delimited string";
4+
let protoArray4 = "Comma-spaces, might, require, typing, caution";
5+
6+
strings = [protoArray1, protoArray2, protoArray3, protoArray4];
7+
8+
//2)
9+
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+
15+
//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 { }.
16+
return output;
17+
}
18+
19+
//3)
20+
function semiDash() {
21+
let check;
22+
let output;
23+
//TODO: write the code required for this step
24+
25+
26+
return output;
27+
}
28+
29+
//4)
30+
function reverseSpaces() {
31+
let check;
32+
let output;
33+
//TODO: write the code required for this step
34+
35+
return output;
36+
}
37+
38+
//5)
39+
function commaSpace() {
40+
let check;
41+
let output;
42+
//TODO: write the code required for this step
43+
44+
return output;
45+
}
46+
47+
// NOTE: Don't add or modify any code below this line or your program might not run as expected.
48+
module.exports = {
49+
strings : strings,
50+
reverseCommas : reverseCommas,
51+
semiDash: semiDash,
52+
reverseSpaces : reverseSpaces,
53+
commaSpace : commaSpace
54+
};
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const studio = require('./array-testing');
2+
3+
console.log(studio.reverseCommas());
4+
console.log(studio.semiDash());
5+
console.log(studio.reverseSpaces());
6+
console.log(studio.commaSpace());
7+
8+
//NOTE: open the array-testing.js file to begin coding
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "Array and String Conversion",
3+
"version": "1.0.0",
4+
"description": "intro to prof web dev studio: Arrays Keep Things in Order",
5+
"main": "grading.js",
6+
"scripts": {
7+
"test": "jest"
8+
},
9+
"author": "",
10+
"license": "ISC",
11+
"devDependencies": {
12+
"@testing-library/jest-dom": "^5.16.5",
13+
"jest": "^29.6.1",
14+
"jest-environment-jsdom": "^29.6.1"
15+
},
16+
"overrides": {
17+
"semver": "~7.5.2"
18+
}
19+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* @jest-environment node
3+
*/
4+
5+
//NOTE: Do NOT modify any of the code below.
6+
7+
//These are the tests. To run them and check your own status, type "npm test" into the console. Running tests is optional.
8+
const solution = require('../array-testing');
9+
10+
describe("Array Studio Solution", function() {
11+
12+
it("strings[0] is '12,9,6,3' after method chaining", function() {
13+
let testArray = solution.reverseCommas(strings[0]);
14+
expect(testArray).toBe("12,9,6,3");
15+
});
16+
17+
it("strings[1] is 'A-C-E-M' after method chaining", function() {
18+
let testArray = solution.semiDash(strings[1]);
19+
expect(testArray).toBe("A-C-E-M");
20+
});
21+
22+
it("strings[2] is 'string space deliminated' after method chaining", function() {
23+
let testArray = solution.reverseSpaces(strings[2]);
24+
expect(testArray).toBe("string space delimited");
25+
});
26+
27+
it("string[3] is 'caution,typing,require,might,Comma-spaces' after method chaining", function() {
28+
let testArray = solution.commaSpace(strings[3]);
29+
expect(testArray).toBe("caution,typing,require,might,Comma-spaces");
30+
});
31+
});
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
let food = "water bottles,meal packs,snacks,chocolate";
2+
let equipment = "space suits,jet packs,tool belts,thermal detonators";
3+
let pets = "parrots,cats,moose,alien eggs";
4+
let sleepAids = "blankets,pillows,eyepatches,alarm clocks";
5+
6+
//1) Use split to convert the strings into four cabinet arrays. Alphabetize the contents of each cabinet.
7+
8+
//2) Initialize a cargoHold array and add the cabinet arrays to it. Print cargoHold to verify its structure.
9+
10+
//3) Query the user to select a cabinet (0 - 3) in the cargoHold.
11+
12+
//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.
13+
14+
//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 ____.”

arrays/studio/package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"main": "index.js",
3+
"dependencies": {
4+
"readline-sync": "1.4.9"
5+
}
6+
}

arrays/studio/string-modification.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const input = require('readline-sync');
2+
let str = "LaunchCode";
3+
4+
//1) Use string methods to remove the first three characters from the string and add them to the end.
5+
//Hint - define another variable to hold the new string or reassign the new string to str.
6+
7+
//Use a template literal to print the original and modified string in a descriptive phrase.
8+
9+
//2) Modify your code to accept user input. Query the user to enter the number of letters that will be relocated.
10+
11+
//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.

0 commit comments

Comments
 (0)