File tree 1 file changed +30
-3
lines changed 1 file changed +30
-3
lines changed Original file line number Diff line number Diff line change @@ -19,14 +19,41 @@ Use console.log() to clearly show the before-and-after type conversions.
19
19
*/
20
20
21
21
22
- let result = "5" - 2 ;
22
+ let result = Number ( "5" ) - 2 ; // I changed "5" to a number, just so that it was easier to understand that with was a math equation
23
23
console . log ( "The result is: " + result ) ;
24
24
25
- let isValid = Boolean ( "false " ) ;
25
+ let isValid = Boolean ( "" ) ; // the string "false" actually comes out to be true because it is a non-empty string. I ended up just making it an empy string.
26
26
if ( isValid ) {
27
27
console . log ( "This is valid!" ) ;
28
+ } else { //I added this because I assumed the code was supposed to output as false and it needed a worded output.
29
+ console . log ( "This is NOT valid" ) ;
28
30
}
29
31
30
32
let age = "25" ;
31
- let totalAge = age + 5 ;
33
+ let totalAge = Number ( age ) + 5 ; // I changed the age to a number
32
34
console . log ( "Total Age: " + totalAge ) ;
35
+
36
+
37
+ //My own examples:
38
+
39
+ console . log ( "------Implicit Type Conversion------" ) ;
40
+
41
+ let catAge = "8" ;
42
+ let years = 4 ;
43
+ let combined = catAge + years ;
44
+
45
+ console . log ( "Combinded (string + number): " + combined ) ;
46
+
47
+ console . log ( "------Explicit Type Conversion------" ) ;
48
+
49
+ let bookPages = "436" ;
50
+ let bookPageNum = Number ( bookPages ) ;
51
+
52
+ console . log ( "Page string to page number: " + bookPageNum ) ;
53
+
54
+ console . log ( "------Edge Case------" ) ;
55
+
56
+ let mysteryValue ;
57
+ let mysteryResult = Number ( mysteryValue ) ;
58
+
59
+ console . log ( "Converting undefined to number: " + mysteryResult ) ;
You can’t perform that action at this time.
0 commit comments