Skip to content

Commit 18fc274

Browse files
committed
exercises done
1 parent d5db1ef commit 18fc274

File tree

9 files changed

+122
-6
lines changed

9 files changed

+122
-6
lines changed

arrays/exercises/part-five-arrays.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,22 @@ 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+
console.log(str.split());
6+
console.log(str.split('e'));
7+
console.log(str.split(' '));
8+
console.log(str.split(''));
9+
510

611
//2) Use the join method on the array to identify the purpose of the parameter inside the ().
12+
console.log(arr.join());
13+
console.log(arr.join('a'));
14+
console.log(arr.join(' '));
15+
console.log(arr.join(''));
716

817
//3) Do split or join change the original string/array?
18+
console.log(str);
19+
console.log(arr);
20+
921

1022
//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.
1123
let cargoHold = "water,space suits,food,plasma sword,batteries";

arrays/exercises/part-four-arrays.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,23 @@ let holdCabinet2 = ['orange drink', 'nerf toys', 'camera', 42, 'parsnip'];
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.
77

8+
newArray = holdCabinet1.concat(holdCabinet2);
9+
console.log(newArray);
10+
console.log(holdCabinet1);
11+
812
//2) Print a slice of two elements from each array. Does slice alter the original arrays?
913

14+
holdCabinet1.slice(1,3);
15+
holdCabinet2.slice(0,2);
16+
17+
console.log(holdCabinet1);
18+
console.log(holdCabinet2);
19+
1020
//3) reverse the first array, and sort the second. What is the difference between these two methods? Do the methods alter the original arrays?
21+
22+
holdCabinet1.reverse();
23+
holdCabinet2.sort();
24+
25+
console.log(holdCabinet1);
26+
console.log(holdCabinet2);
27+

arrays/exercises/part-one-arrays.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
//Create an array called practiceFile with the following entry: 273.15
2+
let practiceFile = [273.15];
23

34
//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.
45

6+
practiceFile.splice(0,0,"42");
7+
console.log(practiceFile);
8+
9+
practiceFile.splice(3,0,"hello");
10+
console.log(practiceFile);
11+
512
//Use a single .push() to add the following items: false, -4.6, and "87". Print the array to confirm the changes.
13+
14+
practiceFile.push(false,-4.6,"87");
15+
console.log(practiceFile);

arrays/exercises/part-six-arrays.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,23 @@
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+
element1 = ['hydrogen', 'H', 1.008];
5+
element2 = ['helium', 'He', 4.003];
6+
element26 = ['iron', 'Fe', 55.85];
7+
48

59
//2) Define the array 'table', and use 'push' to add each of the element arrays to it. Print 'table' to see its structure.
10+
table = []
11+
table.push(element1,element2,element26);
12+
13+
console.log(table);
614

715
//3) Use bracket notation to examine the difference between printing 'table' with one index vs. two indices (table[][]).
16+
console.log(table[1]);
17+
console.log(table[1][1]);
818

919
//4) Using bracket notation and the table array, print the mass of element1, the name for element 2 and the symbol for element26.
20+
console.log(table[0][2], table[1][0], table[2][1]);
1021

1122
//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.
23+

arrays/exercises/part-three-arrays.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,16 @@ 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+
cargoHold.splice(3,0,"keys");
7+
console.log(cargoHold);
68

79
//2) Remove ‘instruction manual’ from the array. (Hint: indexOf is helpful to avoid manually counting an index).
10+
console.log(cargoHold.indexOf("instruction manual"));
11+
cargoHold.splice(4,1);
12+
console.log(cargoHold);
13+
814

915
//3) Replace the elements at indexes 2 - 4 with the items ‘cat’, ‘fob’, and ‘string cheese’.
16+
17+
cargoHold.splice(2,3,"cat","fob","string cheese");
18+
console.log(cargoHold);

arrays/exercises/part-two-arrays.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,27 @@ let cargoHold = ['oxygen tanks', 'space suits', 'parrot', 'instruction manual',
22

33
//1) Use bracket notation to replace ‘slinky’ with ‘space tether’. Print the array to confirm the change.
44

5+
cargoHold.splice(5,1,"space tether");
6+
console.log(cargoHold);
7+
58
//2) Remove the last item from the array with pop. Print the element removed and the updated array.
69

10+
console.log(cargoHold.pop());
11+
console.log(cargoHold);
12+
13+
714
//3) Remove the first item from the array with shift. Print the element removed and the updated array.
815

16+
console.log(cargoHold.shift());
17+
console.log(cargoHold);
18+
919
//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.
20+
cargoHold.push("20 meters");
21+
cargoHold.unshift(1138);
22+
23+
console.log(cargoHold);
24+
1025

1126
//5) Use a template literal to print the final array and its length.
27+
28+
console.log(`The final array is ${cargoHold} with length of ${cargoHold.length}.`);
Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,15 @@
1-
let jsCreator = "Brendan Eich";
1+
// let jsCreator = "Brendan Eich";
22

3-
console.log(jsCreator[-1]);
4-
console.log(jsCreator[42]);
3+
// console.log(jsCreator[-1]);
4+
// console.log(jsCreator[42]);
5+
6+
7+
// let org = " The LaunchCode Foundation ";
8+
// let trimmed = org.trim();
9+
10+
// console.log(trimmed);
11+
12+
let name = "Jack";
13+
let currentAge = 9;
14+
15+
console.log(`Next year, ${name} will be ${currentAge + 1}.`);

stringing-characters-together/exercises/part-one.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,17 @@ console.log(num.length);
55

66
//Use type conversion to print the length (number of digits) of an integer.
77

8+
// console.log(String(num));
9+
// num = String(num);
10+
// console.log(num.length);
11+
812
//Follow up: Print the number of digits in a DECIMAL value (e.g. num = 123.45 has 5 digits but a length of 6).
913

14+
num /= 100;
15+
console.log(num)
16+
num = String(num)
17+
console.log(typeof num);
18+
console.log(num.replace('.', ''));
19+
20+
1021
//Experiment! What if num could be EITHER an integer or a decimal? Add an if/else statement so your code can handle both cases.

stringing-characters-together/exercises/part-two.js

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,21 @@
33
let dna = " TCG-TAC-gaC-TAC-CGT-CAG-ACT-TAa-CcA-GTC-cAt-AGA-GCT ";
44

55
// First, print out the dna strand in it's current state.
6+
console.log(dna);
67

78
//1) Use the .trim() method to remove the leading and trailing whitespace, then print the result.
89

9-
console.log(/* Your code here. */);
10+
console.log(dna.trim());
1011

1112
//2) Change all of the letters in the dna string to UPPERCASE, then print the result.
1213

13-
console.log();
14+
console.log(dna.toUpperCase());
1415

1516
//3) Note that after applying the methods above, the original, flawed string is still stored in dna. To fix this, we need to reassign the changes to back to dna.
1617
//Apply these fixes to your code so that console.log(dna) prints the DNA strand in UPPERCASE with no whitespace.
1718

19+
console.log(dna);
20+
dna = dna.trim().toUpperCase();
1821
console.log(dna);
1922

2023
//Part Two Section Two
@@ -23,10 +26,24 @@ let dnaTwo = "TCG-TAC-GAC-TAC-CGT-CAG-ACT-TAA-CCA-GTC-CAT-AGA-GCT";
2326

2427
//1) Replace the gene "GCT" with "AGG", and then print the altered strand.
2528

26-
//2) Look for the gene "CAT" with ``indexOf()``. If found print, "CAT gene found", otherwise print, "CAT gene NOT found".
29+
console.log(dnaTwo.replace('GCT', 'AGG'));
2730

31+
32+
//2) Look for the gene "CAT" with ``indexOf()``. If found print, "CAT gene found", otherwise print, "CAT gene NOT found"
33+
if (dna.indexOf("CAT")){
34+
console.log("CAT gene found");
35+
} else {
36+
console.log("CAT gene NOT found");
37+
}
2838
//3) Use .slice() to print out the fifth gene (set of 3 characters) from the DNA strand.
39+
dna = dna.replace('GCT','AGG');
40+
console.log(dna);
2941

3042
//4) Use a template literal to print, "The DNA strand is ___ characters long."
3143

44+
dnaLength = dna.length;
45+
46+
console.log(`"The DNA strand is ${dnaLength} characters long."`);
47+
48+
3249
//5) Just for fun, apply methods to ``dna`` and use another template literal to print, 'taco cat'.

0 commit comments

Comments
 (0)