Skip to content

Commit

Permalink
String Method practice complete.
Browse files Browse the repository at this point in the history
  • Loading branch information
craigb28 committed Jan 16, 2024
1 parent 9813cdf commit 9b8ef48
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 7 deletions.
18 changes: 18 additions & 0 deletions arrays/exercises/part-one-arrays.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
//Create an array called practiceFile with the following entry: 273.15

let practiceFile = []

practiceFile.push(273.15)

console.log(practiceFile)

//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,"87")

console.log(practiceFile)
22 changes: 22 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,25 @@ let jsCreator = "Brendan Eich";

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

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

pet = 'cat'
console.log(pet + 's'); // this simply printed pet and included an s in this instance.
pet += 's'; // this added an "s" to the string that was stored as variable pet.
console.log(pet)

let catDog = "Rover "

console.log(catDog.indexOf("v"))
console.log(catDog.trim())
console.log(catDog.replace("ov", "yd"))
console.log(catDog.slice(2, 4))
console.log(catDog.slice(4).toUpperCase())

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

console.log(trimmed);

14 changes: 9 additions & 5 deletions stringing-characters-together/code-snippets/mad-libs.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
let pluralNoun = ;
let name = ;
let verb = ;
let adjective = ;
let color = ;
let pluralNoun = "cats";
let name = "John";
let verb = "kick";
let adjective = "cold";
let color = "green";

console.log("JavaScript provides a "+ color +" collection of tools — including " + adjective + " syntax and " + pluralNoun + " — that allows "+ name +" to "+ verb +" with strings.")

console.log( `Javascript provides a ${color} collection of tools -
including ${adjective} syntax and ${pluralNoun} -
that allows ${name} to ${verb} with strings.`)
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@
let word = 'JavaScript';

console.log(word.toUpperCase());

//Returns ``JAVASCRIPT``

//What does ``word.slice(4).toUpperCase()`` return?

console.log(word.slice(4).toUpperCase())

//Experiment with other combinations (chains) of string methods.

console.log(word.slice(2).toUpperCase().replace("v","X"))
25 changes: 25 additions & 0 deletions stringing-characters-together/exercises/part-one.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,35 @@
let num = 1001;
let numTwo= 123.45

//Returns 'undefined'.
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).
console.log(String(numTwo).replace(".", "").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.

if (String(numTwo).indexOf(".") >= 0) {
console.log(String(numTwo).replace(".", "").length)
} else {
console.log(String(num).length)
}

console.log("Launch\nCode")

let charles = ['coder', 'Tech', 47, 23, 350];
charles.sort();
console.log(charles);

const school = [
["science", "computer", "art"],
["Jones", "Willoughby", "Rhodes"]
];
school[0].push("dance")
console.log(school[0])

school[1].unshift("Holmes")
console.log(school[1])
15 changes: 15 additions & 0 deletions stringing-characters-together/exercises/part-three.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,30 @@
let language = 'JavaScript';

//1. Use string concatenation and two slice() methods to print 'JS' from 'JavaScript'
console.log(language.slice(0,4))
console.log(language.slice(4,10))

let initials = (language.slice(0,4))[0]+(language.slice(4,10))[0]

console.log(initials)

//2. Without using slice(), use method chaining to accomplish the same thing.

console.log(language[language.indexOf("J")]+language[language.indexOf("S")])

//3. Use bracket notation and a template literal to print, "The abbreviation for 'JavaScript' is 'JS'."

console.log(`The abbreviation for ${language} is ${language[language.indexOf("J")]+language[language.indexOf("S")]}`)

//4. Just for fun, try chaining 3 or more methods together, and then print the result.

console.log(language.slice(0,(language.length-3)).toUpperCase())
//Part Three section Two

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

let notTitleCase = 'title case';

let titleCase = notTitleCase.replace("t","T").replace("c","C")

console.log(titleCase)
24 changes: 22 additions & 2 deletions stringing-characters-together/exercises/part-two.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,22 @@ 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. */);
let trimmedDna = dna.trim()
console.log(trimmedDna);

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

console.log();
let trimmedDnaUpper = trimmedDna.toUpperCase();
console.log(trimmedDnaUpper);

//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
Expand All @@ -23,10 +28,25 @@ 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.

dnaTwo = dnaTwo.replace("GCT", "AGG")

console.log(dnaTwo)

//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(dnaTwo.slice(16,19)) /* 16 is the index for the first letter of the fifth codon, since there are dashes between codons. 19 is the index for the dash after that codon, thus the end of it.*/

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

console.log(`The DNA strand is ${dnaTwo.length} characters long.`)

//5) Just for fun, apply methods to ``dna`` and use another template literal to print, 'taco cat'.

console.log(`${dnaTwo.slice(4,7).toLowerCase()}o ${dnaTwo.slice(40,43).toLowerCase()}`)

0 comments on commit 9b8ef48

Please sign in to comment.