File tree Expand file tree Collapse file tree 1 file changed +15
-5
lines changed
stringing-characters-together/exercises Expand file tree Collapse file tree 1 file changed +15
-5
lines changed Original file line number Diff line number Diff line change 1
1
let num = 1001 ;
2
2
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.
5
4
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
+ }
7
10
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.
9
13
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
+ }
You can’t perform that action at this time.
0 commit comments