From 262118776355ecd3e129ce64c0ba5e748087c2fe Mon Sep 17 00:00:00 2001 From: Benjo <130617538+benjo00@users.noreply.github.com> Date: Wed, 5 Mar 2025 18:57:13 -0600 Subject: [PATCH 1/2] Fixed bugs and added my own examples --- ...t-and-implicit-conversion-in-javascript.js | 21 ++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/explicit-and-implicit-conversion-in-javascript.js b/explicit-and-implicit-conversion-in-javascript.js index ede0ccd..d771948 100644 --- a/explicit-and-implicit-conversion-in-javascript.js +++ b/explicit-and-implicit-conversion-in-javascript.js @@ -19,14 +19,29 @@ Use console.log() to clearly show the before-and-after type conversions. */ -let result = "5" - 2; +let result = Number("5") - 2; //added explicit Number conversion console.log("The result is: " + result); -let isValid = Boolean("false"); +let isValid = Boolean(false);//removed quotes so that it will be seen as a false statement. if (isValid) { console.log("This is valid!"); +} else { //added else statement so it can still run + console.log("This is not valid!"); } let age = "25"; -let totalAge = age + 5; +let totalAge = Number(age) + 5; // was outputting 52 due to implicit conversion. Add Number conversion to fix output. console.log("Total Age: " + totalAge); + +//my own examples + +//explicit type conversion: +let numberOfApples = Number("10");//I used the Number conversion to explicitly make this a number despite it being in quotes making it a string +let numberOfOranges = 20; +let sumFruit = numberOfApples + numberOfOranges; +console.log("The fantastical fruit total is " + sumFruit); + +//implicit type conversion: + +let sum = 30 + "2"; +console.log(sum);//returns 302 because 30 was implicitly converted to a string and added to the string "2". From 23a73a2fcf4e5c75a2db16f7a188247843c150eb Mon Sep 17 00:00:00 2001 From: Benjo <130617538+benjo00@users.noreply.github.com> Date: Wed, 5 Mar 2025 19:00:37 -0600 Subject: [PATCH 2/2] add one more example --- explicit-and-implicit-conversion-in-javascript.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/explicit-and-implicit-conversion-in-javascript.js b/explicit-and-implicit-conversion-in-javascript.js index d771948..7b6febc 100644 --- a/explicit-and-implicit-conversion-in-javascript.js +++ b/explicit-and-implicit-conversion-in-javascript.js @@ -45,3 +45,8 @@ console.log("The fantastical fruit total is " + sumFruit); let sum = 30 + "2"; console.log(sum);//returns 302 because 30 was implicitly converted to a string and added to the string "2". + +//example with edge case: + +let example = Boolean(null);//since "null" is in the Boolean it automatically is false because "null" is falsy. +console.log(example);