From 6a3a1ec4dee171930eb702e9e56fe297678a4040 Mon Sep 17 00:00:00 2001 From: ItsAddi Date: Sat, 1 Mar 2025 16:53:11 -0600 Subject: [PATCH] Fixed conversions and added examples --- ...t-and-implicit-conversion-in-javascript.js | 33 +++++++++++++++++-- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/explicit-and-implicit-conversion-in-javascript.js b/explicit-and-implicit-conversion-in-javascript.js index ede0ccd..97d8294 100644 --- a/explicit-and-implicit-conversion-in-javascript.js +++ b/explicit-and-implicit-conversion-in-javascript.js @@ -19,14 +19,41 @@ Use console.log() to clearly show the before-and-after type conversions. */ -let result = "5" - 2; +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 console.log("The result is: " + result); -let isValid = Boolean("false"); +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. if (isValid) { console.log("This is valid!"); +} else { //I added this because I assumed the code was supposed to output as false and it needed a worded output. + console.log("This is NOT valid"); } let age = "25"; -let totalAge = age + 5; +let totalAge = Number(age) + 5; // I changed the age to a number console.log("Total Age: " + totalAge); + + +//My own examples: + +console.log("------Implicit Type Conversion------"); + +let catAge = "8"; +let years = 4; +let combined = catAge + years; + +console.log("Combinded (string + number): " + combined); + +console.log("------Explicit Type Conversion------"); + +let bookPages = "436"; +let bookPageNum = Number(bookPages); + +console.log("Page string to page number: " + bookPageNum); + +console.log("------Edge Case------"); + +let mysteryValue; +let mysteryResult = Number(mysteryValue); + +console.log("Converting undefined to number: " + mysteryResult); \ No newline at end of file