Skip to content

Commit abed669

Browse files
Practice 1-Values,Data Types, and Operations
1 parent ce92942 commit abed669

File tree

1 file changed

+30
-5
lines changed

1 file changed

+30
-5
lines changed

explicit-and-implicit-conversion-in-javascript.js

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,40 @@ Use console.log() to clearly show the before-and-after type conversions.
1818
1919
*/
2020

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.
2324
console.log("The result is: " + result);
2425

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.
2630
if (isValid) {
2731
console.log("This is valid!");
32+
}else{ // Added else function to clarify what happens when isValid is false
33+
console.log("This is invalid!");
2834
}
2935

30-
let age = "25";
36+
let age = Number("25");
3137
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

Comments
 (0)