From ef5dd0470fc617f4faeb3845188cc4e14ddb8f40 Mon Sep 17 00:00:00 2001 From: Shravan Samudrala Date: Wed, 19 Mar 2025 01:17:10 -0500 Subject: [PATCH 1/2] Answer added --- ...t-and-implicit-conversion-in-javascript.js | 39 ++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/explicit-and-implicit-conversion-in-javascript.js b/explicit-and-implicit-conversion-in-javascript.js index ede0ccd..b4abd17 100644 --- a/explicit-and-implicit-conversion-in-javascript.js +++ b/explicit-and-implicit-conversion-in-javascript.js @@ -16,7 +16,6 @@ Write their own code that demonstrates: Include at least one edge case, like NaN, undefined, or null . Use console.log() to clearly show the before-and-after type conversions. -*/ let result = "5" - 2; @@ -30,3 +29,41 @@ if (isValid) { let age = "25"; let totalAge = age + 5; console.log("Total Age: " + totalAge); + +*/ + +//Fixed code + +//Part 1: +//1. Explicitly convert string 5 to number 5 substracting would have forced the conversion but this conversion gives more clarity +let result = Number("5") - 2; // 3 +console.log("The result is: " + result); //The result is: 3 + +//2. Conversion of string "false" to boolean as Boolean("false") is truthy and will return "This is valid!" but doe to this conversion the boolean will be false and execute else statement i.e "This is not valid!" +let isValid = JSON.parse("false"); +if (isValid) { + console.log("This is valid!"); +} else { + console.log("This is not valid!"); // "This is not valid!" +} + +// 3. when it is age = "25" the output of the totalAge will be 255 which is not the expected result instead by converting the age to number the output will be correct i.e 30 +let age = "25"; +let totalAge = Number(age) + 5; // 30 +console.log("Total Age: " + totalAge) // Total Age: 30 + + +// Implicit Conversion Example: + +let implResult = "10" * 2; // The multiplication operator "*" forces the string "10" to be converted to a number. +console.log("After implicit conversion: " + implResult); // 20 + +// Explicit Conversion Example: +let NumberString = "123"; // valid number string +let expliNumber = Number(NumberString); // explicitly converts string to a number +console.log("Explicitly converted '123' to number:", expliNumber); // 123 + +// Edge Case: +let nonNumberString = "Edge Case"; +let invalidConversion = Number(nonNumberString); +console.log("Converting 'Edge Case' results in:", invalidConversion); // NaN From 35e6f2df5404f76309fbfea980016da74f995c1e Mon Sep 17 00:00:00 2001 From: Shravan Samudrala Date: Thu, 20 Mar 2025 14:10:39 -0500 Subject: [PATCH 2/2] Solution added --- explicit-and-implicit-conversion-in-javascript.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/explicit-and-implicit-conversion-in-javascript.js b/explicit-and-implicit-conversion-in-javascript.js index b4abd17..f2e4c4d 100644 --- a/explicit-and-implicit-conversion-in-javascript.js +++ b/explicit-and-implicit-conversion-in-javascript.js @@ -55,8 +55,8 @@ console.log("Total Age: " + totalAge) // Total Age: 30 // Implicit Conversion Example: -let implResult = "10" * 2; // The multiplication operator "*" forces the string "10" to be converted to a number. -console.log("After implicit conversion: " + implResult); // 20 +let implConvr = "10" * 2; // The multiplication operator "*" forces the string "10" to be converted to a number. +console.log("After implicit conversion: " + implConvr); // 20 // Explicit Conversion Example: let NumberString = "123"; // valid number string