From 0730ba4f6c2956cb03e077046abf090ab1d8bb1d Mon Sep 17 00:00:00 2001 From: Murwarid Date: Wed, 11 Nov 2026 19:48:30 +0100 Subject: [PATCH] Fix bugs and add option 3 for exchange rate in currency converter --- package-lock.json | 42 ++++++++++++++++++++++++++++++++++++++++++ package.json | 5 +++++ task-1/leap-year.js | 20 +++++++++++++++----- task-2/login.js | 19 +++++++++++++++---- task-3/converter.js | 14 +++++++++----- 5 files changed, 86 insertions(+), 14 deletions(-) create mode 100644 package-lock.json create mode 100644 package.json diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..18e597d --- /dev/null +++ b/package-lock.json @@ -0,0 +1,42 @@ +{ + "name": "c56-core-week-2", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "dependencies": { + "prompt-sync": "^4.2.0" + } + }, + "node_modules/ansi-regex": { + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-4.1.1.tgz", + "integrity": "sha512-ILlv4k/3f6vfQ4OoP2AGvirOktlQ98ZEL1k9FaQjxa3L1abBgbuTDAdPOpvbGncC0BTVQrl+OM8xZGK6tWXt7g==", + "license": "MIT", + "engines": { + "node": ">=6" + } + }, + "node_modules/prompt-sync": { + "version": "4.2.0", + "resolved": "https://registry.npmjs.org/prompt-sync/-/prompt-sync-4.2.0.tgz", + "integrity": "sha512-BuEzzc5zptP5LsgV5MZETjDaKSWfchl5U9Luiu8SKp7iZWD5tZalOxvNcZRwv+d2phNFr8xlbxmFNcRKfJOzJw==", + "license": "MIT", + "dependencies": { + "strip-ansi": "^5.0.0" + } + }, + "node_modules/strip-ansi": { + "version": "5.2.0", + "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-5.2.0.tgz", + "integrity": "sha512-DuRs1gKbBqsMKIZlrffwlug8MHkcnpjs5VPmL1PAh+mA30U0DTotfDZ0d2UUsXpPmPmMMJ6W773MaA3J+lbiWA==", + "license": "MIT", + "dependencies": { + "ansi-regex": "^4.1.0" + }, + "engines": { + "node": ">=6" + } + } + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..3131708 --- /dev/null +++ b/package.json @@ -0,0 +1,5 @@ +{ + "dependencies": { + "prompt-sync": "^4.2.0" + } +} diff --git a/task-1/leap-year.js b/task-1/leap-year.js index e05d215..c300f3c 100644 --- a/task-1/leap-year.js +++ b/task-1/leap-year.js @@ -2,8 +2,18 @@ import promptSync from 'prompt-sync'; const prompt = promptSync(); -// Write your code here -// Guidance: -// Step 1: prompt the user to enter a year -// Step 2: convert the user input to a number so we can perform calculations -// Step 3: Implement the logic +const input = prompt('Enter a year: '); +const year = Number(input); + + +if (isNaN(year) || year < 1 || year > 9999 || !Number.isInteger(year)) { + console.log('Invalid year!'); +} else if (year % 400 === 0) { + console.log(`Yes, ${year} is a leap year`); +} else if (year % 100 === 0) { + console.log(`No, ${year} is not a leap year`); +} else if (year % 4 === 0) { + console.log(`Yes, ${year} is a leap year`); +} else { + console.log(`No, ${year} is not a leap year`); +} \ No newline at end of file diff --git a/task-2/login.js b/task-2/login.js index ca9ba92..5726b0b 100644 --- a/task-2/login.js +++ b/task-2/login.js @@ -4,10 +4,21 @@ import { errorMessage, successMessage } from './app.js'; let incorrectAttempts = 0; function onLogin(username, password) { - // Write your code here. - // Use the variables 'username' and 'password' to access the input values - // Use incorrectAttempts to track the number of failed attempts -} + const isValidUser= + (username === 'admin' && password === 'Hack1234') || + (username === 'user' && password === '7654321'); + + + if(incorrectAttempts >= 4) { + errorMessage('login blocked:Too many incorrect attempts'); + } else if (isValidUser) { + successMessage('Login successful'); + + } else { + incorrectAttempts++; + errorMessage('Incorrect ceredentials'); + } + // Do not change the line below export { onLogin }; diff --git a/task-3/converter.js b/task-3/converter.js index 0f54a18..e2c1813 100644 --- a/task-3/converter.js +++ b/task-3/converter.js @@ -5,10 +5,10 @@ const prompt = promptSync(); const EUR_USD_RATE = 1.1643; // Menu display -conole.log("Hello and welcome to the currency converter. Please choose: "); +console.log("Hello and welcome to the currency converter. Please choose: "); console.log("1: Convert EUR to USD"); console.log("2: Convert USD to EUR"); -const menuSelection = prompt("Select your option [1 or 2]: "); +const menuSelection = prompt("Select your option [1, 2 or 3]: "); console.log("\n"); @@ -16,11 +16,11 @@ if (menuSelection === "1") { // EUR to USD const eurAmountInput = prompt("Enter amount in EUR: "); const eurAmountNum = Number(eurAmountInput); - if (Number.isNaN(eurAmountNum) || eurAmountNum > 0) { + if (Number.isNaN(eurAmountNum) || eurAmountNum <= 0) { console.log("Please enter a valid positive number for the amount."); } else { const usdAmount = eurAmountNum * EUR_USD_RATE; - console.log(eurAmountNum.toFixed(2) + ' EUR is equal to ' + usdAmount.toFixed(2) + ' USD.'); + console.log(eurAmountNum.toFixed(2) + ' EUR is equal to ' + usdAmount.toFixed(2) + 'USD.'); } } else if (menuSelection === "2") { // USD to EUR @@ -32,6 +32,10 @@ if (menuSelection === "1") { const eurAmount = usdAmountNum / eur_usd_rate; console.log(usdAmountNum.toFixed(2) + ' USD is equal to ' + usdAmountNum.toFixed(2) + ' EUR.'); } +} else if (menuSelection === "3") { + // Display exchange rate + + console.log("The current exchange rate is 1 EUR = " + EUR_USD_RATE.toFixed(4) + " USD."); } else { - console.log("Invalid selection. Please choose either 1 or 2."); + console.log("Invalid selection. Please choose either 1, 2, or 3."); }