Skip to content

Commit ee72b9a

Browse files
committed
Strings Done
1 parent 611030c commit ee72b9a

File tree

3 files changed

+39
-12
lines changed

3 files changed

+39
-12
lines changed
Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,28 @@
11
let num = 1001;
22

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

66
//Use type conversion to print the length (number of digits) of an integer.
7-
7+
if (typeof num === 'number') {
8+
let numStr = num.toString();
9+
console.log(numStr.length);
10+
}
811
//Follow up: Print the number of digits in a DECIMAL value (e.g. num = 123.45 has 5 digits but a length of 6).
12+
let num2 = 'gfd';
13+
if (typeof num2 === 'number'){
14+
let numStr2 = num2.toString();
15+
if (numStr2.includes('.')){
16+
let removeDecimal = numStr2.replace('.', '');
17+
console.log(removeDecimal.length);
18+
19+
} else{
20+
console.log(numStr2.length)
21+
}
22+
} else{
23+
console.log("Please input a number");
24+
}
925

1026
//Experiment! What if num could be EITHER an integer or a decimal? Add an if/else statement so your code can handle both cases.
27+
28+

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

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

55
//1. Use string concatenation and two slice() methods to print 'JS' from 'JavaScript'
6-
6+
console.log(language.slice(0, 1)+language.slice(4, 5));
77
//2. Without using slice(), use method chaining to accomplish the same thing.
8-
8+
console.log(language.charAt(0) + language.charAt(4));
99
//3. Use bracket notation and a template literal to print, "The abbreviation for 'JavaScript' is 'JS'."
10-
10+
console.log(`The abbreviation for ${language} is ${language.slice(0,1)}${language.slice(4,5)}.`)
1111
//4. Just for fun, try chaining 3 or more methods together, and then print the result.
12-
12+
console.log(`The abbreviation for ${language} is ${language.slice(0,1)}${language.charAt(4)}.`)
1313
//Part Three section Two
1414

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

1717
let notTitleCase = 'title case';
18+
console.log(notTitleCase.replace('t','T').slice(0,5)+notTitleCase.replace('c','C').slice(5,10))

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

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,38 @@
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-
6+
console.log(dna);
77
//1) Use the .trim() method to remove the leading and trailing whitespace, then print the result.
88

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

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

13-
console.log();
13+
console.log(dna.toUpperCase());
1414

1515
//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.
1616
//Apply these fixes to your code so that console.log(dna) prints the DNA strand in UPPERCASE with no whitespace.
17-
17+
dna = dna.trim().toUpperCase();
1818
console.log(dna);
1919

2020
//Part Two Section Two
2121

2222
let dnaTwo = "TCG-TAC-GAC-TAC-CGT-CAG-ACT-TAA-CCA-GTC-CAT-AGA-GCT";
2323

2424
//1) Replace the gene "GCT" with "AGG", and then print the altered strand.
25+
dnaTwo = dnaTwo.replace('GCT','AGG');
26+
console.log(dnaTwo);
2527

2628
//2) Look for the gene "CAT" with ``indexOf()``. If found print, "CAT gene found", otherwise print, "CAT gene NOT found".
27-
29+
if (dnaTwo.indexOf('CAT')){
30+
console.log("CAT gene found");
31+
} else{
32+
console.log('CAT gene not found');
33+
}
2834
//3) Use .slice() to print out the fifth gene (set of 3 characters) from the DNA strand.
35+
console.log(dnaTwo.slice(16,19));
2936

3037
//4) Use a template literal to print, "The DNA strand is ___ characters long."
31-
38+
console.log(`The DNA strand is ${dnaTwo.length} characters long.`)
3239
//5) Just for fun, apply methods to ``dna`` and use another template literal to print, 'taco cat'.
40+
console.log(`${dnaTwo.slice(4,7).toLowerCase()}o ${dnaTwo.slice(40,43).toLowerCase()}`)

0 commit comments

Comments
 (0)