Skip to content

Commit 50eaa49

Browse files
committed
Exercises: String
1 parent a1cc044 commit 50eaa49

File tree

3 files changed

+53
-2
lines changed

3 files changed

+53
-2
lines changed

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,25 @@ let num = 1001;
44
console.log(num.length);
55

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

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

11+
num = 123.45;
12+
let numString = String(num);
13+
1014
//Experiment! What if num could be EITHER an integer or a decimal? Add an if/else statement so your code can handle both cases.
15+
16+
let stringLength = numString.length;
17+
18+
//evaluates as truthy if there is a '.' at any index, but zero
19+
console.log('numString: ' + numString)
20+
console.log(numString.includes('.'));
21+
22+
if (numString.includes('.')) {
23+
24+
console.log(stringLength - 1);
25+
26+
} else {
27+
console.log(stringLength);
28+
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,27 @@
33
let language = 'JavaScript';
44

55
//1. Use string concatenation and two slice() methods to print 'JS' from 'JavaScript'
6+
let sliceJ = language.slice(0, 1);
7+
let sliceS = language.slice(4, 5);
8+
console.log(`${sliceJ}${sliceS}`);
69

710
//2. Without using slice(), use method chaining to accomplish the same thing.
11+
let indexJ = language.indexOf('J');
12+
let indexS = language.indexOf('S');
13+
console.log(language[indexJ] + language[indexS]);
814

915
//3. Use bracket notation and a template literal to print, "The abbreviation for 'JavaScript' is 'JS'."
16+
console.log(`The abbreviation for '${language}' is '${language[0]}${language[4]}'.`);
1017

1118
//4. Just for fun, try chaining 3 or more methods together, and then print the result.
19+
console.log(language.toUpperCase().slice(0, 4).indexOf('V'));
1220

1321
//Part Three section Two
1422

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

1725
let notTitleCase = 'title case';
26+
27+
let TitleCase = notTitleCase.replace('t', 'T').replace('c', 'C');
28+
console.log(TitleCase);
29+

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

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,19 @@
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.trim().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.
18+
dna = dna.trim().toUpperCase();
1719

1820
console.log(dna);
1921

@@ -23,10 +25,29 @@ let dnaTwo = "TCG-TAC-GAC-TAC-CGT-CAG-ACT-TAA-CCA-GTC-CAT-AGA-GCT";
2325

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

28+
dnaTwo = dnaTwo.replace('GCT', 'AGG');
29+
console.log('dnaTwo: ' + dnaTwo)
30+
2631
//2) Look for the gene "CAT" with ``indexOf()``. If found print, "CAT gene found", otherwise print, "CAT gene NOT found".
2732

33+
if (dnaTwo.indexOf('CAT')) {
34+
console.log('CAT gene found');
35+
36+
} else {
37+
console.log('CAT gene NOT found');
38+
}
2839
//3) Use .slice() to print out the fifth gene (set of 3 characters) from the DNA strand.
2940

41+
console.log(dnaTwo.slice(16, 19));
42+
3043
//4) Use a template literal to print, "The DNA strand is ___ characters long."
44+
console.log(`The DNA strand is ${dnaTwo.length} characters long.`);
3145

3246
//5) Just for fun, apply methods to ``dna`` and use another template literal to print, 'taco cat'.
47+
48+
console.log(dnaTwo.indexOf('A'));
49+
console.log(dnaTwo.toLowerCase());
50+
console.log(`----
51+
taco
52+
cat
53+
-----`);

0 commit comments

Comments
 (0)