File tree Expand file tree Collapse file tree 1 file changed +43
-4
lines changed Expand file tree Collapse file tree 1 file changed +43
-4
lines changed Original file line number Diff line number Diff line change @@ -18,15 +18,54 @@ Use console.log() to clearly show the before-and-after type conversions.
18
18
19
19
*/
20
20
21
-
22
- let result = "5" - 2 ;
21
+ // broke down the variable subtraction into more steps, making the code
22
+ // easier to follow. can convert x value with Number(), though the interpreter
23
+ // makes the decision to convert data type automatically to a number
24
+ let x = "5" ;
25
+ let y = 2 ;
26
+ let result = x - y ;
23
27
console . log ( "The result is: " + result ) ;
24
28
25
- let isValid = Boolean ( "false" ) ;
29
+ // Boolean() wrapper was unnecessary and caused confusion, because it wrapped a non-empty string
30
+ // which evaluated the code to boolean true. so I just initialized the false value as a bool
31
+ let isValid = false ;
26
32
if ( isValid ) {
27
33
console . log ( "This is valid!" ) ;
28
34
}
29
35
36
+ // converting age to a primitive Number makes the addition work fine
30
37
let age = "25" ;
31
- let totalAge = age + 5 ;
38
+ let totalAge = Number ( age ) + 5 ;
32
39
console . log ( "Total Age: " + totalAge ) ;
40
+
41
+
42
+ // MY EXAMPLES
43
+
44
+ // EXPLICIT CONVERSION
45
+ let someInput = '5' ;
46
+ let anotherInput = null ;
47
+
48
+ let sumInputs ;
49
+
50
+ if ( someInput === null && anotherInput == null ) {
51
+ sumInputs = 0 ;
52
+ } else if ( someInput === null ) {
53
+ sumInputs = Number ( anotherInput ) ;
54
+ } else if ( anotherInput === null ) {
55
+ sumInputs = Number ( someInput ) ;
56
+ } else {
57
+ sumInputs = Number ( someInput ) + Number ( anotherInput ) ;
58
+ }
59
+
60
+ console . log ( sumInputs ) ;
61
+
62
+
63
+ // IMPLICIT CONVERSION
64
+
65
+ let numX = 3 ;
66
+ let numY = '8' ;
67
+ let product = numX * numY ;
68
+
69
+ console . log ( product ) ;
70
+
71
+
You can’t perform that action at this time.
0 commit comments