Skip to content

Commit c018fec

Browse files
committed
strings exercises
1 parent 6ad18fe commit c018fec

3 files changed

Lines changed: 49 additions & 2 deletions

File tree

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,22 @@ console.log(num.length);
55

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

8+
console.log(String(num).length);
9+
810
//Follow up: Print the number of digits in a DECIMAL value (e.g. num = 123.45 has 5 digits but a length of 6).
911

12+
if (String(num).includes('.'))
13+
{
14+
console.log(String(num).length - 1);
15+
}
16+
1017
//Experiment! What if num could be EITHER an integer or a decimal? Add an if/else statement so your code can handle both cases.
18+
19+
if (String(num).includes('.'))
20+
{
21+
console.log(String(num).length - 1);
22+
}
23+
else
24+
{
25+
console.log(String(num).length);
26+
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,26 @@ 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.charAt(0) + language.charAt(4));
12+
913
//3. Use bracket notation and a template literal to print, "The abbreviation for 'JavaScript' is 'JS'."
1014

15+
console.log(`The abbreviation for '${language}' is '${language.charAt(0) + language.charAt(4)}'.`)
16+
1117
//4. Just for fun, try chaining 3 or more methods together, and then print the result.
1218

19+
console.log( language.trim().toUpperCase().toLowerCase().slice(0,8));
20+
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+
notTitleCase = notTitleCase.replace('t', 'T');
27+
notTitleCase = notTitleCase.replace('c','C');
28+
29+
console.log(notTitleCase);

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

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,21 @@ 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());
12+
1013

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

13-
console.log();
16+
console.log(dna.toUpperCase());
1417

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

21+
dna = dna.trim().toUpperCase();
1822
console.log(dna);
1923

2024
//Part Two Section Two
@@ -23,10 +27,25 @@ let dnaTwo = "TCG-TAC-GAC-TAC-CGT-CAG-ACT-TAA-CCA-GTC-CAT-AGA-GCT";
2327

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

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

35+
if(dna.indexOf('CAT'))
36+
console.log("CAT found");
37+
38+
else
39+
console.log("CAT not found");
40+
2841
//3) Use .slice() to print out the fifth gene (set of 3 characters) from the DNA strand.
2942

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

47+
console.log(`The DNA strand is ${dna.length} characters long.`);
48+
3249
//5) Just for fun, apply methods to ``dna`` and use another template literal to print, 'taco cat'.
50+
51+
console.log(`${dna.slice(4,7).toLowerCase()}o ${dna.slice(dna.indexOf('CAT'),dna.indexOf('CAT')+3).toLowerCase()}`);

0 commit comments

Comments
 (0)