Skip to content

Commit 49c62c5

Browse files
committed
Fixed and debugged prewritten code while adding
own in Part 2 of assignment.
1 parent ce92942 commit 49c62c5

File tree

1 file changed

+27
-4
lines changed

1 file changed

+27
-4
lines changed

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

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,37 @@ 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; //this looked correct to me and displayed how I expected. I added Number() to be more clear
2323
console.log("The result is: " + result);
2424

25-
let isValid = Boolean("false");
25+
let isValid = Boolean("false" == false);//I added '==' so that the string 'false' will be equal to the boolean false.
2626
if (isValid) {
2727
console.log("This is valid!");
28-
}
28+
}
29+
2930

3031
let age = "25";
31-
let totalAge = age + 5;
32+
33+
let totalAge = Number(age) + 5; //turned the variable 'age' from string to number so the equation can work
3234
console.log("Total Age: " + totalAge);
35+
36+
37+
//Part Two
38+
39+
//Implicit type conversion
40+
let numofItems = 38;
41+
console.log(`You have ${numofItems} items in your cart.`); //Template literal
42+
43+
//Explicit Type Conversion
44+
let str = "72";
45+
let num = Number(str);
46+
47+
console.log(num); // converting to a number from string
48+
49+
//One edge case
50+
51+
let slicesofPizza = Boolean(null); //used null to for Boolean value and 'if()' to create output if returned true. If false, code shows the expected output.
52+
if (slicesofPizza) {
53+
console.log("There is currently no pizza!");
54+
}
55+
console.log(slicesofPizza);

0 commit comments

Comments
 (0)