From 7207c54115c4db3ba6449532d4b7af59aaf5bd08 Mon Sep 17 00:00:00 2001 From: augustkellerwrites Date: Mon, 10 Mar 2025 19:22:53 -0500 Subject: [PATCH 1/3] debugged --- explicit-and-implicit-conversion-in-javascript.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/explicit-and-implicit-conversion-in-javascript.js b/explicit-and-implicit-conversion-in-javascript.js index ede0ccd..3ff8836 100644 --- a/explicit-and-implicit-conversion-in-javascript.js +++ b/explicit-and-implicit-conversion-in-javascript.js @@ -20,13 +20,15 @@ Use console.log() to clearly show the before-and-after type conversions. let result = "5" - 2; -console.log("The result is: " + result); +let numResult = Number(result) +console.log("The result is: " + numResult); // While the original code did get the correct output due to implicit conversion, this gets it through excplicit conversion. let isValid = Boolean("false"); if (isValid) { - console.log("This is valid!"); + console.log("This is valid!"); // This is correct. Any non-empty string is considered truthy, even the string "false". } let age = "25"; -let totalAge = age + 5; -console.log("Total Age: " + totalAge); +let numAge = Number(age); +let totalAge = numAge + 5; +console.log("Total Age: " + totalAge); // Strings and numbers are concatenated by the + operator. To fix, I converted the sting "age" into a number. From d5427522753a506223b04e76530ab8da2ee2a276 Mon Sep 17 00:00:00 2001 From: augustkellerwrites Date: Mon, 10 Mar 2025 19:38:34 -0500 Subject: [PATCH 2/3] Examples of Type Conversion --- explicit-and-implicit-conversion-in-javascript.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/explicit-and-implicit-conversion-in-javascript.js b/explicit-and-implicit-conversion-in-javascript.js index 3ff8836..f8041a1 100644 --- a/explicit-and-implicit-conversion-in-javascript.js +++ b/explicit-and-implicit-conversion-in-javascript.js @@ -20,7 +20,7 @@ Use console.log() to clearly show the before-and-after type conversions. let result = "5" - 2; -let numResult = Number(result) +let numResult = Number(result); console.log("The result is: " + numResult); // While the original code did get the correct output due to implicit conversion, this gets it through excplicit conversion. let isValid = Boolean("false"); @@ -32,3 +32,13 @@ let age = "25"; let numAge = Number(age); let totalAge = numAge + 5; console.log("Total Age: " + totalAge); // Strings and numbers are concatenated by the + operator. To fix, I converted the sting "age" into a number. + +let redDucks = Boolean(null); +if (redDucks) { + console.log("Ducks are red!"); +} else { + console.log("Ducks aren't red!"); // Example of explicit conversion, converting 0 to false. Also edge case because I used null. +} + +let myAge = "My age is " + 35; +console.log(myAge); \ No newline at end of file From 02cfecdc6c529d973cd9255ae96fa1d87c3d8ff8 Mon Sep 17 00:00:00 2001 From: augustkellerwrites Date: Mon, 10 Mar 2025 19:40:55 -0500 Subject: [PATCH 3/3] final changes --- explicit-and-implicit-conversion-in-javascript.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/explicit-and-implicit-conversion-in-javascript.js b/explicit-and-implicit-conversion-in-javascript.js index f8041a1..2dd4be4 100644 --- a/explicit-and-implicit-conversion-in-javascript.js +++ b/explicit-and-implicit-conversion-in-javascript.js @@ -40,5 +40,5 @@ if (redDucks) { console.log("Ducks aren't red!"); // Example of explicit conversion, converting 0 to false. Also edge case because I used null. } -let myAge = "My age is " + 35; +let myAge = "My age is " + 35; // Example of implicit conversion, STRING CONCATENATION WITH THE + OPERATOR. console.log(myAge); \ No newline at end of file