@@ -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 ;
23
- console . log ( "The result is: " + result ) ;
24
-
25
- let isValid = Boolean ( "false" ) ;
22
+ let numStr = "5" ; // Add variable str to call out "5"
23
+ console . log ( typeof numStr ) ;
24
+ let num = Number ( numStr ) ; // Add variable num to convert "5" to number 5
25
+ console . log ( typeof num ) ;
26
+ let result = num - 2 ; // Use num to perform the math problem
27
+ console . log ( "The result is: " + result ) ; // This provides the same result but handles 5 as a number instead of a string
28
+
29
+ let falseStr = "false" ; // set a variable to equal the "false" string
30
+ console . log ( typeof falseStr ) ;
31
+ falseStr = ( falseStr === false ) ; // Set the falseStr to a Boolean stating if "false" equals false
32
+ console . log ( typeof falseStr ) ;
33
+ let isValid = Boolean ( falseStr ) ;
26
34
if ( isValid ) {
27
35
console . log ( "This is valid!" ) ;
28
- }
36
+ } // The code no longer outputs because the Boolean is actaully false rather than "false" (which is truthy)
29
37
30
- let age = "25" ;
31
- let totalAge = age + 5 ;
38
+ let ageInput = "25" ; // Call out ageInput as a string
39
+ console . log ( typeof ageInput ) ;
40
+ let ageNum = Number ( ageInput ) ; // Change ageInput to number
41
+ console . log ( typeof ageNum ) ;
42
+ let totalAge = ageNum + 5 ; // Use ageNum in the math problem
32
43
console . log ( "Total Age: " + totalAge ) ;
44
+
45
+ // Implicit Conversion Example
46
+ let testScore = 82 ; // Score is entered as a number
47
+ console . log ( typeof testScore ) ;
48
+ let grade = ( "Your grade is " + testScore + "/100" ) ; // Score is converted to string
49
+ console . log ( typeof grade ) ;
50
+ console . log ( grade ) ; // Grade is shown as a whole string
51
+
52
+ // Explicit Conversion Example
53
+ let extraCreditInput = 0 ; // Extra credit is entered as a number
54
+ console . log ( typeof extraCreditInput ) ;
55
+ let extraCreditBool = Boolean ( extraCreditInput ) ; // Extra credit is converted to a Boolean
56
+ console . log ( typeof extraCreditBool ) ;
57
+ if ( ! extraCreditBool ) {
58
+ console . log ( "You did not earn any extra credit." ) ;
59
+ }
0 commit comments