From 11dbe2ea222b0c34f80262d97031aa1338282c40 Mon Sep 17 00:00:00 2001 From: sameersayani Date: Sun, 12 Apr 2026 02:47:49 +0530 Subject: [PATCH 1/2] corrected version --- demo.js | 34 +++++++++++----------------------- 1 file changed, 11 insertions(+), 23 deletions(-) diff --git a/demo.js b/demo.js index 580067d..c337abc 100644 --- a/demo.js +++ b/demo.js @@ -23,29 +23,17 @@ async function fetchData(url) { } } -//function to convert any currency to INR using a fixed exchange ratefunction, intentioanally add cognitive complexity by using multiple if-else statements +//function to convert any currency to INR using a fixed exchange ratefunction function convertToINR(amount, currency) { - let exchangeRate; - if (currency === 'USD') { - exchangeRate = 74.85; - } else if (currency === 'EUR') { - exchangeRate = 88.50; - } else if (currency === 'GBP') { - exchangeRate = 103.25; - } else if (currency === 'JPY') { - exchangeRate = 0.68; - } else if (currency === 'AUD') { - exchangeRate = 55.30; - } else if (currency === 'CAD') { - exchangeRate = 59.20; - } else if (currency === 'CHF') { - exchangeRate = 82.10; - } else if (currency === 'CNY') { - exchangeRate = 11.50; - } else if (currency === 'HKD') { - exchangeRate = 9.60; - } else { + const exchangeRates = { + USD: 74.85, + EUR: 88.50, + GBP: 103.20, + JPY: 0.68 + }; + if (exchangeRates[currency]) { + return amount * exchangeRates[currency]; + } else { throw new Error('Unsupported currency'); - } - return amount * exchangeRate; + } } \ No newline at end of file From 958dced3e3b797734fc6006e2bbc4afa9110ce01 Mon Sep 17 00:00:00 2001 From: sameersayani Date: Sun, 12 Apr 2026 02:56:22 +0530 Subject: [PATCH 2/2] PR comment fixed --- demo.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/demo.js b/demo.js index c337abc..65bee37 100644 --- a/demo.js +++ b/demo.js @@ -23,7 +23,7 @@ async function fetchData(url) { } } -//function to convert any currency to INR using a fixed exchange ratefunction +//function to convert any currency to INR using a fixed exchange rate function function convertToINR(amount, currency) { const exchangeRates = { USD: 74.85, @@ -31,7 +31,7 @@ function convertToINR(amount, currency) { GBP: 103.20, JPY: 0.68 }; - if (exchangeRates[currency]) { + if (Object.hasOwn(exchangeRates, currency)) { return amount * exchangeRates[currency]; } else { throw new Error('Unsupported currency');