Skip to content

Commit 6a3a1ec

Browse files
committed
Fixed conversions and added examples
1 parent ce92942 commit 6a3a1ec

File tree

1 file changed

+30
-3
lines changed

1 file changed

+30
-3
lines changed

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

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,41 @@ Use console.log() to clearly show the before-and-after type conversions.
1919
*/
2020

2121

22-
let result = "5" - 2;
22+
let result = Number("5") - 2; // I changed "5" to a number, just so that it was easier to understand that with was a math equation
2323
console.log("The result is: " + result);
2424

25-
let isValid = Boolean("false");
25+
let isValid = Boolean(""); // the string "false" actually comes out to be true because it is a non-empty string. I ended up just making it an empy string.
2626
if (isValid) {
2727
console.log("This is valid!");
28+
} else { //I added this because I assumed the code was supposed to output as false and it needed a worded output.
29+
console.log("This is NOT valid");
2830
}
2931

3032
let age = "25";
31-
let totalAge = age + 5;
33+
let totalAge = Number(age) + 5; // I changed the age to a number
3234
console.log("Total Age: " + totalAge);
35+
36+
37+
//My own examples:
38+
39+
console.log("------Implicit Type Conversion------");
40+
41+
let catAge = "8";
42+
let years = 4;
43+
let combined = catAge + years;
44+
45+
console.log("Combinded (string + number): " + combined);
46+
47+
console.log("------Explicit Type Conversion------");
48+
49+
let bookPages = "436";
50+
let bookPageNum = Number(bookPages);
51+
52+
console.log("Page string to page number: " + bookPageNum);
53+
54+
console.log("------Edge Case------");
55+
56+
let mysteryValue;
57+
let mysteryResult = Number(mysteryValue);
58+
59+
console.log("Converting undefined to number: " + mysteryResult);

0 commit comments

Comments
 (0)