Skip to content

Commit

Permalink
String and Array - Excersise completed
Browse files Browse the repository at this point in the history
  • Loading branch information
KayalvizhiRajendran committed May 9, 2024
1 parent af86ac4 commit 30098f3
Show file tree
Hide file tree
Showing 12 changed files with 148 additions and 29 deletions.
14 changes: 11 additions & 3 deletions arrays/exercises/part-five-arrays.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,18 @@ let str = 'In space, no one can hear you code.';
let arr = ['B', 'n', 'n', 5];

//1) Use the split method on the string to identify the purpose of the parameter inside the ().

console.log(str.split());
console.log(str.split('e'));
console.log( str.split(' '));
console.log(str.split(''));
console.log(str);
//2) Use the join method on the array to identify the purpose of the parameter inside the ().

console.log(arr.join(""));
//3) Do split or join change the original string/array?

console.log(str);
console.log(arr);
//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.
let cargoHold = "water,space suits,food,plasma sword,batteries";
let tempArr = cargoHold.split(",");
cargoHold = tempArr.sort().join(",");
console.log(cargoHold);
11 changes: 9 additions & 2 deletions arrays/exercises/part-four-arrays.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,16 @@ let holdCabinet1 = ['duct tape', 'gum', 3.14, false, 6.022e23];
let holdCabinet2 = ['orange drink', 'nerf toys', 'camera', 42, 'parsnip'];

//Explore the methods concat, slice, reverse, and sort to determine which ones alter the original array.
let concatArray = holdCabinet1.concat(holdCabinet2);

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

console.log(holdCabinet1);
console.log(concatArray);
//2) Print a slice of two elements from each array. Does slice alter the original arrays?

console.log(holdCabinet1.slice(1,3));
console.log(holdCabinet1);
//3) reverse the first array, and sort the second. What is the difference between these two methods? Do the methods alter the original arrays?
holdCabinet1.reverse();
holdCabinet2.sort();
console.log(holdCabinet1);
console.log(holdCabinet2);
9 changes: 7 additions & 2 deletions arrays/exercises/part-one-arrays.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
//Create an array called practiceFile with the following entry: 273.15

let practiceFile =[273.15];
//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.

practiceFile.push(42);
console.log(practiceFile);
practiceFile.push("hello");
console.log(practiceFile);
//Use a single .push() to add the following items: false, -4.6, and "87". Print the array to confirm the changes.
practiceFile.push(false,-4.6, "87");
console.log(practiceFile);
16 changes: 12 additions & 4 deletions arrays/exercises/part-six-arrays.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
//Arrays can hold different data types, even other arrays! A multi-dimensional array is one with entries that are themselves arrays.

//1) Define and initialize the arrays specified in the exercise to hold the name, chemical symbol and mass for different elements.

let element1 = ['hydrogen', 'H', 1.008];
let element2 = ['helium', 'He', 4.003];
let element26 = ['iron', 'Fe', 55.85];
//2) Define the array 'table', and use 'push' to add each of the element arrays to it. Print 'table' to see its structure.

let table = [];
table.push(element1,element2,element26);
console.log(table);
//3) Use bracket notation to examine the difference between printing 'table' with one index vs. two indices (table[][]).

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

console.log(`The mass of element1 is ${table[0][2]}, The name of element2 is ${table[1][0]}, The symbol of elemnt26 is ${table[2][1]}`);
//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.
let tablesOftable=[];
tablesOftable.push(table);
console.log(tablesOftable[0][1][0]);
8 changes: 6 additions & 2 deletions arrays/exercises/part-three-arrays.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ let cargoHold = [1138, 'space suits', 'parrot', 'instruction manual', 'meal pack
//Use splice to make the following changes to the cargoHold array. Be sure to print the array after each step to confirm your updates.

//1) Insert the string 'keys' at index 3 without replacing any other entries.

cargoHold.splice(3,0, "key");
console.log(cargoHold);
//2) Remove ‘instruction manual’ from the array. (Hint: indexOf is helpful to avoid manually counting an index).

cargoHold.splice(cargoHold.indexOf("instruction manual"),1);
console.log(cargoHold);
//3) Replace the elements at indexes 2 - 4 with the items ‘cat’, ‘fob’, and ‘string cheese’.
cargoHold.splice(2,3,'cat','fob','string cheese');
console.log(cargoHold);
13 changes: 10 additions & 3 deletions arrays/exercises/part-two-arrays.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
let cargoHold = ['oxygen tanks', 'space suits', 'parrot', 'instruction manual', 'meal packs', 'slinky', 'security blanket'];

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

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

console.log(cargoHold.pop());
console.log(cargoHold);
//3) Remove the first item from the array with shift. Print the element removed and the updated array.

console.log(cargoHold.shift());
console.log(cargoHold);
//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.
console.log(cargoHold.unshift(1138));
console.log(cargoHold.push("20 meters"));
console.log(cargoHold);

//5) Use a template literal to print the final array and its length.
console.log(`The array ${cargoHold} has a length of ${cargoHold.length}.`);
4 changes: 2 additions & 2 deletions errors-and-debugging/chapter-examples/SyntaxErrors.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
let day = Wednesday;
console.log(day;
let day = "Wednesday";
console.log(day);
35 changes: 35 additions & 0 deletions stringing-characters-together/code-snippets/bracket-notation.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,38 @@ let jsCreator = "Brendan Eich";

console.log(jsCreator[-1]);
console.log(jsCreator[42]);

let phrase = "JavaScript rocks!";
console.log(phrase[phrase.length - 8]);

myStr = 'Index';
console.log(myStr[2] === 'n');
console.log(myStr[4] === 'x');
console.log(myStr[6] === ' ');
console.log(myStr[0] === 'I');

phrase = 'Code for fun';
console.log(phrase[2]);

cityName = "Vienna";
stateName = "Virginia";
location = cityName + ", " + stateName;

console.log(location.length);

let pet = 'cat';
console.log(pet +"s");
pet += "s";
console.log(pet);

let language = "JavaScript";
console.log(language.replace('J', 'Q'));
console.log(language.slice(0,5));
console.log(language);

console.log( language.slice(1,6));

let org = " The LaunchCode Foundation ";
let trimmed = org.trim();

console.log(trimmed);
28 changes: 28 additions & 0 deletions stringing-characters-together/code-snippets/method-chaining.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,34 @@
let word = 'JavaScript';

console.log(word.toUpperCase());

let codes = [76, 97, 117, 110, 99, 104, 67, 111, 100, 101];

let characters = String.fromCharCode(codes[0]) + String.fromCharCode(codes[1])
+ String.fromCharCode(codes[2]) + String.fromCharCode(codes[3])
+ String.fromCharCode(codes[4]) + String.fromCharCode(codes[5])
+ String.fromCharCode(codes[6]) + String.fromCharCode(codes[7])
+ String.fromCharCode(codes[8]) + String.fromCharCode(codes[9]);

console.log(characters);

console.log('Launch\nCode');
console.log('Launch/nCode');
console.log('Launch', 'Code');
console.log('Launch\tCode');
console.log('Launch/tCode');

let pluralNoun = "structures";
let name = "Tom";
let verb = "code";
let adjective = "beautiful";
let color = "red";

console.log(`JavaScript provides a ${color} collection of tools — including ${adjective} syntax and ${pluralNoun} — that allows ${name} to ${verb} with strings.`);
name = "Jack";
let currentAge = 9;

console.log(`Next year, ${name} will be ${currentAge + 1}.`);
//Returns ``JAVASCRIPT``

//What does ``word.slice(4).toUpperCase()`` return?
Expand Down
8 changes: 7 additions & 1 deletion stringing-characters-together/exercises/part-one.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ let num = 1001;
console.log(num.length);

//Use type conversion to print the length (number of digits) of an integer.
console.log(String(num).length);

//Follow up: Print the number of digits in a DECIMAL value (e.g. num = 123.45 has 5 digits but a length of 6).

if(String(num).includes(".")){
let str = String(num).replace(".","");
console.log(str.length);
}else{
console.log(String(num).length);
}
//Experiment! What if num could be EITHER an integer or a decimal? Add an if/else statement so your code can handle both cases.
8 changes: 6 additions & 2 deletions stringing-characters-together/exercises/part-three.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@ let language = 'JavaScript';

//1. Use string concatenation and two slice() methods to print 'JS' from 'JavaScript'

console.log(language.slice(0,1)+language.slice(4,5));
//2. Without using slice(), use method chaining to accomplish the same thing.

let initials =language.replace("ava","").replace("cript","")
console.log(initials);
//3. Use bracket notation and a template literal to print, "The abbreviation for 'JavaScript' is 'JS'."

console.log(`The abbreviation for ${language} is ${initials}.`);
//4. Just for fun, try chaining 3 or more methods together, and then print the result.

//Part Three section Two

//1. Use the string methods you know to print 'Title Case' from the string 'title case'.

let notTitleCase = 'title case';
console.log(notTitleCase.replace("ti","Ti").replace("c","C"));
//console.log(notTitleCase.charAt(0).toUpperCase());
23 changes: 15 additions & 8 deletions stringing-characters-together/exercises/part-two.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,37 @@
let dna = " TCG-TAC-gaC-TAC-CGT-CAG-ACT-TAa-CcA-GTC-cAt-AGA-GCT ";

// First, print out the dna strand in it's current state.

console.log(dna);
//1) Use the .trim() method to remove the leading and trailing whitespace, then print the result.

console.log(/* Your code here. */);
console.log(dna.trim());

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

console.log();
console.log(dna.toUpperCase());

//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.
//Apply these fixes to your code so that console.log(dna) prints the DNA strand in UPPERCASE with no whitespace.

dna = dna.trim().toUpperCase();
console.log(dna);

//Part Two Section Two

let dnaTwo = "TCG-TAC-GAC-TAC-CGT-CAG-ACT-TAA-CCA-GTC-CAT-AGA-GCT";

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

dna = dna.replace("GCT","AGG")
console.log(dna);
//2) Look for the gene "CAT" with ``indexOf()``. If found print, "CAT gene found", otherwise print, "CAT gene NOT found".

if(dnaTwo.indexOf("CAT") != -1){
console.log("CAT found");
}else{
console.log("CAT not found");
}
//3) Use .slice() to print out the fifth gene (set of 3 characters) from the DNA strand.

console.log(dna.slice(16,19));
//4) Use a template literal to print, "The DNA strand is ___ characters long."

console.log(`The DNA strand is ${dna.length} characters long.`);
//5) Just for fun, apply methods to ``dna`` and use another template literal to print, 'taco cat'.
let tempStr = dna.slice(12,15).toLowerCase()+ "o";
console.log(`${tempStr} ${dna.slice(40,43).toLowerCase()}`);

0 comments on commit 30098f3

Please sign in to comment.