From 30098f3e6971593a75ab511c2cae6bfe8a1f590f Mon Sep 17 00:00:00 2001 From: Kayalvizhi Rajendran Date: Thu, 9 May 2024 14:23:27 -0500 Subject: [PATCH] String and Array - Excersise completed --- arrays/exercises/part-five-arrays.js | 14 ++++++-- arrays/exercises/part-four-arrays.js | 11 ++++-- arrays/exercises/part-one-arrays.js | 9 +++-- arrays/exercises/part-six-arrays.js | 16 ++++++--- arrays/exercises/part-three-arrays.js | 8 +++-- arrays/exercises/part-two-arrays.js | 13 +++++-- .../chapter-examples/SyntaxErrors.js | 4 +-- .../code-snippets/bracket-notation.js | 35 +++++++++++++++++++ .../code-snippets/method-chaining.js | 28 +++++++++++++++ .../exercises/part-one.js | 8 ++++- .../exercises/part-three.js | 8 +++-- .../exercises/part-two.js | 23 +++++++----- 12 files changed, 148 insertions(+), 29 deletions(-) diff --git a/arrays/exercises/part-five-arrays.js b/arrays/exercises/part-five-arrays.js index 4cdf1bba41..8cfffe86bb 100644 --- a/arrays/exercises/part-five-arrays.js +++ b/arrays/exercises/part-five-arrays.js @@ -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); diff --git a/arrays/exercises/part-four-arrays.js b/arrays/exercises/part-four-arrays.js index 498149702e..8a126c546b 100644 --- a/arrays/exercises/part-four-arrays.js +++ b/arrays/exercises/part-four-arrays.js @@ -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); \ No newline at end of file diff --git a/arrays/exercises/part-one-arrays.js b/arrays/exercises/part-one-arrays.js index 92f4e45170..fc4239c15a 100644 --- a/arrays/exercises/part-one-arrays.js +++ b/arrays/exercises/part-one-arrays.js @@ -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); \ No newline at end of file diff --git a/arrays/exercises/part-six-arrays.js b/arrays/exercises/part-six-arrays.js index d0a28bed56..9104aa887f 100644 --- a/arrays/exercises/part-six-arrays.js +++ b/arrays/exercises/part-six-arrays.js @@ -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]); \ No newline at end of file diff --git a/arrays/exercises/part-three-arrays.js b/arrays/exercises/part-three-arrays.js index d43918a702..e3001783bf 100644 --- a/arrays/exercises/part-three-arrays.js +++ b/arrays/exercises/part-three-arrays.js @@ -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); \ No newline at end of file diff --git a/arrays/exercises/part-two-arrays.js b/arrays/exercises/part-two-arrays.js index a940b1d0ff..c48ee1ffe4 100644 --- a/arrays/exercises/part-two-arrays.js +++ b/arrays/exercises/part-two-arrays.js @@ -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}.`); \ No newline at end of file diff --git a/errors-and-debugging/chapter-examples/SyntaxErrors.js b/errors-and-debugging/chapter-examples/SyntaxErrors.js index 5597f22de5..4ad8b9b125 100644 --- a/errors-and-debugging/chapter-examples/SyntaxErrors.js +++ b/errors-and-debugging/chapter-examples/SyntaxErrors.js @@ -1,2 +1,2 @@ -let day = Wednesday; -console.log(day; \ No newline at end of file +let day = "Wednesday"; +console.log(day); \ No newline at end of file diff --git a/stringing-characters-together/code-snippets/bracket-notation.js b/stringing-characters-together/code-snippets/bracket-notation.js index 5b07c358ad..ea638005b7 100644 --- a/stringing-characters-together/code-snippets/bracket-notation.js +++ b/stringing-characters-together/code-snippets/bracket-notation.js @@ -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); \ No newline at end of file diff --git a/stringing-characters-together/code-snippets/method-chaining.js b/stringing-characters-together/code-snippets/method-chaining.js index 43fcd55bc7..b8348a89a9 100644 --- a/stringing-characters-together/code-snippets/method-chaining.js +++ b/stringing-characters-together/code-snippets/method-chaining.js @@ -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? diff --git a/stringing-characters-together/exercises/part-one.js b/stringing-characters-together/exercises/part-one.js index 9295e4dd9f..fbf58b3d06 100644 --- a/stringing-characters-together/exercises/part-one.js +++ b/stringing-characters-together/exercises/part-one.js @@ -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. diff --git a/stringing-characters-together/exercises/part-three.js b/stringing-characters-together/exercises/part-three.js index 8c310f1445..9102aa00f2 100644 --- a/stringing-characters-together/exercises/part-three.js +++ b/stringing-characters-together/exercises/part-three.js @@ -4,10 +4,12 @@ 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 @@ -15,3 +17,5 @@ let language = 'JavaScript'; //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()); diff --git a/stringing-characters-together/exercises/part-two.js b/stringing-characters-together/exercises/part-two.js index a06e9094dc..5b2d49356e 100644 --- a/stringing-characters-together/exercises/part-two.js +++ b/stringing-characters-together/exercises/part-two.js @@ -3,18 +3,18 @@ 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 @@ -22,11 +22,18 @@ console.log(dna); 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()}`); \ No newline at end of file