From 6b7adb2dcd6a1823e2b95e2aa6db76cac7c7b3e7 Mon Sep 17 00:00:00 2001 From: Nishant Kumar Date: Wed, 14 Aug 2024 02:27:34 +0530 Subject: [PATCH] Update 3.js The issues in these code is you should have to use let or const to declare the variables result and result2 . By using let or var keyword to declare result and result2, we ensure that each variable is scoped correctly and not overwritten by subsequent calls to the square function. I updated the code, Review it once. Thank You --- 3.js | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/3.js b/3.js index 321026d..ad70157 100644 --- a/3.js +++ b/3.js @@ -1,13 +1,14 @@ -// problem: why is it logging out 0? it should be 12 -// since 4 * 4 is 16, 2 * 2 is 4 and 16 - 4 is 12 -// fix it to log out 12! - function square(num) { - result = num * num - return result + let result = num * num; + return result; } -result = square(4) -result2 = square(2) -var subtracted = result - result2 -console.log(subtracted) +//the issues in these code is you should have to use let or const to declare the variables result and result2 + +//By using let or var keyword to declare result and result2, +//we ensure that each variable is scoped correctly and not overwritten by subsequent calls to the square function + +let result = square(4); +let result2 = square(2); +var subtracted = result - result2; +console.log(subtracted); // Output: 12