diff --git a/explicit-and-implicit-conversion-in-javascript.js b/explicit-and-implicit-conversion-in-javascript.js index ede0ccd..60403bb 100644 --- a/explicit-and-implicit-conversion-in-javascript.js +++ b/explicit-and-implicit-conversion-in-javascript.js @@ -22,11 +22,35 @@ Use console.log() to clearly show the before-and-after type conversions. let result = "5" - 2; console.log("The result is: " + result); -let isValid = Boolean("false"); +let isValid = Boolean(""); //assuming "false" is desired, removing the word "false" and leaving the string empty makes it falsy if (isValid) { console.log("This is valid!"); } let age = "25"; +age = Number(age); //this step converts the string "25" to a number let totalAge = age + 5; console.log("Total Age: " + totalAge); + + +let width; //undefined +console.log("width is a " + typeof width); + +width = 0.5; //initializes width and implicitly converts to a Number (float) +console.log("width is a " + typeof width); + +let length = 3; +let depth = "0.75"; +console.log("depth is a " + typeof depth); + +let feet = "true"; +console.log("feet is a " + typeof feet); + +feet = Boolean(feet); //explicit conversion of feet +console.log("feet is " + typeof feet + "and is " + feet); + +if (feet) { + console.log("Total board feet is ", (width * length * depth) / 12); //implicit conversion of depth +} else { + console.log("Total board feet is ", (width * length * depth) / 144); //implicit conversion of depth +}