Skip to content

Commit 9b34fa5

Browse files
authored
completed part 1
1 parent 4e70d2e commit 9b34fa5

File tree

1 file changed

+15
-5
lines changed

1 file changed

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

3-
//Returns 'undefined'.
4-
console.log(num.length);
3+
console.log(num.length); // Prints undefined because num is an integer and does not have a length property.
54

6-
//Use type conversion to print the length (number of digits) of an integer.
5+
if (String(num).includes('.')) {
6+
console.log(String(num).replace('.', '').length); // If num were a decimal, this would print the number of digits excluding the decimal point.
7+
} else {
8+
console.log(String(num).length); // Prints the number of digits in the integer num.
9+
}
710

8-
//Follow up: Print the number of digits in a DECIMAL value (e.g. num = 123.45 has 5 digits but a length of 6).
11+
let decimalNum = 123.45;
12+
console.log(String(decimalNum).replace('.', '').length); // Prints the number of digits in the decimal number excluding the decimal point.
913

10-
//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+
let num2 = 123.45;
15+
16+
if (String(num2).includes('.')) {
17+
console.log(String(num2).replace('.', '').length); // Prints the number of digits in the decimal number excluding the decimal point.
18+
} else {
19+
console.log(String(num2).length); // Prints the number of digits in the integer num2.
20+
}

0 commit comments

Comments
 (0)