File tree 1 file changed +18
-3
lines changed 1 file changed +18
-3
lines changed Original file line number Diff line number Diff line change @@ -19,14 +19,29 @@ 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 ; //added explicit Number conversion
23
23
console . log ( "The result is: " + result ) ;
24
24
25
- let isValid = Boolean ( " false" ) ;
25
+ let isValid = Boolean ( false ) ; //removed quotes so that it will be seen as a false statement.
26
26
if ( isValid ) {
27
27
console . log ( "This is valid!" ) ;
28
+ } else { //added else statement so it can still run
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 ; // was outputting 52 due to implicit conversion. Add Number conversion to fix output.
32
34
console . log ( "Total Age: " + totalAge ) ;
35
+
36
+ //my own examples
37
+
38
+ //explicit type conversion:
39
+ let numberOfApples = Number ( "10" ) ; //I used the Number conversion to explicitly make this a number despite it being in quotes making it a string
40
+ let numberOfOranges = 20 ;
41
+ let sumFruit = numberOfApples + numberOfOranges ;
42
+ console . log ( "The fantastical fruit total is " + sumFruit ) ;
43
+
44
+ //implicit type conversion:
45
+
46
+ let sum = 30 + "2" ;
47
+ console . log ( sum ) ; //returns 302 because 30 was implicitly converted to a string and added to the string "2".
You can’t perform that action at this time.
0 commit comments