@@ -18,15 +18,40 @@ 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
+ // This one seems to have the right results, but
22
+ let result = Number ( "5" ) - 2 ; // Assuming the intent is to make sure this 5 acts as an integer in this program,
23
+ // I will just turn the string into a number with the number() function.
23
24
console . log ( "The result is: " + result ) ;
24
25
25
- let isValid = Boolean ( "false" ) ;
26
+ let isValid = false ; // here the boolean is reading false as a string "false", making the program run it as a truthy statement
27
+ // It seems like they were looking for a "This is invalid' response
28
+ // I will take the quotes away from "false" to make it a boolean falsy statemtn
29
+ // I will also remove the boolean() function as it is not needed.
26
30
if ( isValid ) {
27
31
console . log ( "This is valid!" ) ;
32
+ } else { // Added else function to clarify what happens when isValid is false
33
+ console . log ( "This is invalid!" ) ;
28
34
}
29
35
30
- let age = "25" ;
36
+ let age = Number ( "25" ) ;
31
37
let totalAge = age + 5 ;
32
- console . log ( "Total Age: " + totalAge ) ;
38
+ console . log ( "Total Age: " + totalAge ) ; // Here the program is converting the number into a string because of the + sign
39
+ // I will fix this by turning the 25 into a number. number()
40
+ // This will make the numbers the same type, allowing them to be added together for a sum
41
+
42
+
43
+ let implicitExample = "10" - 2 ; // Implicit Example String turns into a number when - function is used
44
+ console . log (
45
+ "The Implicit conversion is " + implicitExample + ", which is a " + typeof implicitExample ) ;
46
+
47
+ let explicitExample = Number ( "42" ) ; // Explicit conversion of a string to a number
48
+ console . log (
49
+ "The explicit conversion is " + explicitExample + ", which is a " + typeof explicitExample
50
+ ) ;
51
+ let explicitExampleNan = NaN ; // NaN is explicitly checked as a falsy value
52
+
53
+ if ( explicitExampleNan ) {
54
+ console . log ( explicitExampleNan + " is truthy" ) ;
55
+ } else {
56
+ console . log ( explicitExampleNan + " is falsy" ) ;
57
+ }
0 commit comments