Skip to content

Commit ee62645

Browse files
committed
Completed string exercises
1 parent b3ce059 commit ee62645

3 files changed

Lines changed: 34 additions & 3 deletions

File tree

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
let num = 1001;
22

33
//Returns 'undefined'.
4-
console.log(num.length);
4+
console.log(String(num).length);
55

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

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

10+
let numDecimal = 123.45;
11+
console.log((String(numDecimal).length - 1));
12+
1013
//Experiment! What if num could be EITHER an integer or a decimal? Add an if/else statement so your code can handle both cases.
14+
if (String(num).includes(".")) {
15+
console.log((String(num).length - 1));
16+
} else {
17+
console.log(String(num).length);
18+
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,21 @@ let language = 'JavaScript';
44

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

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

11+
console.log(language[language.indexOf("J")] + language[language.indexOf("S")]);
12+
913
//3. Use bracket notation and a template literal to print, "The abbreviation for 'JavaScript' is 'JS'."
14+
console.log(`The abbreviation for '${language}' is '${language[language.indexOf("J")] + language[language.indexOf("S")]}'.`);
1015

1116
//4. Just for fun, try chaining 3 or more methods together, and then print the result.
17+
console.log(language.slice(3,6).replace("a","qx").toUpperCase());
1218

1319
//Part Three section Two
1420

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

1723
let notTitleCase = 'title case';
24+
console.log(notTitleCase.slice(0,1).toUpperCase() + notTitleCase.slice(1).replace("c", "C"));

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,20 @@ 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.
66

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

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

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

13-
console.log();
15+
console.log(dna.trim().toUpperCase());
1416

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

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

2023
//Part Two Section Two
@@ -23,10 +26,23 @@ 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

29+
console.log("dnaTwo:");
30+
console.log(dnaTwo.replace("GCT", "AGG"));
31+
2632
//2) Look for the gene "CAT" with ``indexOf()``. If found print, "CAT gene found", otherwise print, "CAT gene NOT found".
2733

34+
// console.log(dnaTwo.indexOf("CAT"));
35+
if (dnaTwo.indexOf("CAT") > -1) {
36+
console.log("CAT gene found")
37+
} else {
38+
console.log("CAT gene NOT found")
39+
}
40+
2841
//3) Use .slice() to print out the fifth gene (set of 3 characters) from the DNA strand.
42+
console.log(dnaTwo.slice(16, 19));
2943

3044
//4) Use a template literal to print, "The DNA strand is ___ characters long."
45+
console.log(`The DNA strand is ${dnaTwo.replaceAll("-", "").length} characters long.`);
3146

3247
//5) Just for fun, apply methods to ``dna`` and use another template literal to print, 'taco cat'.
48+
console.log(dna.slice(4, 9).replace("-G", "o ").toLowerCase() + dna.slice(40, 43).toLowerCase());

0 commit comments

Comments
 (0)