Repository navigation
Angelika S #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Angelika S #8
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,25 @@ | ||
| import promptSync from 'prompt-sync'; | ||
| const prompt = promptSync(); | ||
|
|
||
| // Ask the user to enter a year | ||
| const input = prompt('Enter a year: '); | ||
| const year = Number(input); | ||
|
|
||
| // 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 | ||
| // Check if the year is valid | ||
| if ( | ||
| input.trim() === '' || !Number.isInteger(year) || year < 1 || year > 9999 | ||
| ) { | ||
| console.log('Invalid year'); | ||
| // Check if the year is divisible by 400 | ||
| } else if (year % 400 === 0) { | ||
| console.log(`${year} is a leap year`); | ||
|
|
||
| } else if (year % 100 === 0) { | ||
| console.log(`${year} is not a leap year`); | ||
| // Check if the year is divisible by 4 | ||
| } else if (year % 4 === 0) { | ||
| console.log(`${year} is a leap year`); | ||
| // If none of the conditions are true | ||
| } else { | ||
| console.log(`${year} is not a leap year`); | ||
|
Comment on lines
+12
to
+24
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. two things here first, the assignment gives the exact text to print yours is missing the "Yes, " / "No, " and the "!". when another program reads your output, the text has to match exactly second, you have four branches but only two different messages. you can separate deciding from printing const isLeapYear = (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0;
if (isLeapYear) {
console.log(`Yes, ${year} is a leap year`);
} else {
console.log(`No, ${year} is not a leap year`);
}now each message is written once, so there's only one place to fix a typo. this is DRY (don't repeat yourself), which came up in week 3 your version with the |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,30 @@ | ||
| // Do not change the line below | ||
| import { errorMessage, successMessage } from './app.js'; | ||
|
|
||
| // Count incorrect login attempts | ||
| 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 | ||
| } | ||
| // Check if login is already blocked | ||
| if (incorrectAttempts > 4) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. try this: enter a wrong password 4 times, then the correct one the page said "blocked" and then let me in the reason: you check the same number in two places with two different comparisons, the fix is to have one number, with a name, and one way of comparing it const MAX_INCORRECT_ATTEMPTS = 3;
let incorrectAttempts = 0;
function onLogin(username, password) {
if (incorrectAttempts > MAX_INCORRECT_ATTEMPTS) {
errorMessage('Login blocked: Too many incorrect attempts');
return;
}
const isAdmin = username === 'admin' && password === 'Hack1234';
const isUser = username === 'user' && password === '7654321';
if (isAdmin || isUser) {
successMessage('Logged in successfully');
return;
}
incorrectAttempts++;
if (incorrectAttempts > MAX_INCORRECT_ATTEMPTS) {
errorMessage('Login blocked: Too many incorrect attempts');
} else {
errorMessage('Incorrect credentials');
}
}a bare |
||
| errorMessage('To many incorrect attemps: your account is blocked'); | ||
| return; | ||
|
|
||
| } | ||
| // Check if username and password are correct | ||
| if( | ||
| (username === 'admin' && password === 'Hack1234') || (username === 'user' && password === '7654321') | ||
| ) { | ||
| successMessage('Logged in successfully'); | ||
| } else { | ||
| // Add one incorrect attempt | ||
| incorrectAttempts++; | ||
|
|
||
| // Block login after 4 incorrect attempts | ||
| if(incorrectAttempts >= 4) { | ||
| errorMessage('Login blocked: To many inccorect attempts'); | ||
| } else { | ||
| errorMessage('Inccorrect credentials'); | ||
|
Comment on lines
+23
to
+25
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. heads up, the spelling in these messages has drifted from the assignment: "Inccorrect", "inccorect", "To many", and on line 8 "attemps" javascript will never complain about a typo inside a string, to the computer it's just characters. the user is the one who sees it two things that help
also notice lines 8 and 23 are meant to be the same message but they're worded differently. write it once, in a constant, and use it in both places |
||
|
|
||
| // Do not change the line below | ||
| } | ||
| } | ||
| } | ||
|
Comment on lines
+27
to
+29
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. a reminder on indentation: it tells the reader where a block starts and ends here the three closing braces don't line up with the lines that opened them, so i had to count braces to work out what belongs to what the week 4 assignment introduces prettier, which should help with this if you ended up installing it. with "format on save" turned on in vs code, the editor does it for you |
||
| export { onLogin }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,37 +1,70 @@ | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. one thing about how this change looks in the pull request you fixed 4 bugs and added 1 feature, but the diff shows 79 lines added and 35 removed, because the whole file was also re-indented and re-spaced when a fix and a reformat are mixed together, the reviewer can't see the fix any more. try to keep them apart git commit -m "Fix three bugs in currency converter"
git commit -m "Add option 3: show exchange rate"
git commit -m "Reformat converter.js"small commits that each do one thing are much easier to review, and much easier to undo |
||
| import promptSync from 'prompt-sync'; | ||
|
|
||
| const prompt = promptSync(); | ||
|
|
||
| // Exchange rate for EUR/USD (How much 1 EUR is in USD) | ||
| // Exchange rate from EUR to USD | ||
| const EUR_USD_RATE = 1.1643; | ||
|
|
||
| // Menu display | ||
| conole.log("Hello and welcome to the currency converter. Please choose: "); | ||
| // Show Menu | ||
| 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]: "); | ||
| console.log("3: Display the current exchange rate"); | ||
|
|
||
| // Ask user to choose an option | ||
| const menuSelection = prompt("Select your option [1, 2, or 3]: "); | ||
|
|
||
| console.log("\n"); | ||
|
|
||
| if (menuSelection === "1") { | ||
| // EUR to USD | ||
| const eurAmountInput = prompt("Enter amount in EUR: "); | ||
| const eurAmountNum = Number(eurAmountInput); | ||
| 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.'); | ||
| } | ||
|
|
||
| // Convert EUR to USD | ||
| const eurAmountInput = prompt("Enter amount in EUR: "); | ||
| const eurAmountNum = Number(eurAmountInput); | ||
| // Check if the amount is valid | ||
| if (eurAmountInput.trim() === "" || | ||
| !Number.isFinite(eurAmountNum) || | ||
| eurAmountNum < 0) { | ||
|
|
||
| console.log("Please enter a valid non-negative number for the amount."); | ||
|
|
||
| } else { | ||
| // Calculate the amount in USD | ||
| const usdAmount = eurAmountNum * EUR_USD_RATE; | ||
| // Show the result | ||
| console.log( | ||
| `${eurAmountNum.toFixed(2)} EUR is equal to ${usdAmount.toFixed(2)} USD.` | ||
| ); | ||
| } | ||
|
|
||
| } else if (menuSelection === "2") { | ||
| // USD to EUR | ||
| const usdAmountInput = prompt("Enter amount in USD: "); | ||
| const usdAmountNum = Number(usdAmountInput); | ||
| if (Number.isNaN(usdAmountNum) || usdAmountNum < 0) { | ||
| console.log("Please enter a valid positive number for the amount."); | ||
| } else { | ||
| const eurAmount = usdAmountNum / eur_usd_rate; | ||
| console.log(usdAmountNum.toFixed(2) + ' USD is equal to ' + usdAmountNum.toFixed(2) + ' EUR.'); | ||
| } | ||
|
|
||
| // Convert USD to EUR | ||
| const usdAmountInput = prompt("Enter amount in USD: "); | ||
| const usdAmountNum = Number(usdAmountInput); | ||
| // Check if the amount is valid | ||
| if (usdAmountInput.trim() === "" || | ||
| !Number.isFinite(usdAmountNum) || | ||
| usdAmountNum < 0) { | ||
|
|
||
| console.log("Please enter a valid non-negative number for the amount."); | ||
|
|
||
| } else { | ||
| // Calculate the amount in EUR | ||
| const eurAmount = usdAmountNum / EUR_USD_RATE; | ||
| // Show the result | ||
| console.log( | ||
| `${usdAmountNum.toFixed(2)} USD is equal to ${eurAmount.toFixed(2)} EUR.` | ||
|
Comment on lines
+55
to
+58
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice catch on the 4th bug, the one that wasn't in the list the template literals are a good upgrade too |
||
| ); | ||
| } | ||
|
|
||
| } else if (menuSelection === "3") { | ||
|
|
||
| // Show exchange rate | ||
| console.log(`The current exchange rate is 1 EUR = ${EUR_USD_RATE} USD.`); | ||
|
|
||
| } else { | ||
| console.log("Invalid selection. Please choose either 1 or 2."); | ||
| // Wrong option | ||
| console.log("Invalid selection. Please choose 1, 2, or 3."); | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice validation, you covered empty input, decimals and the range. most people stop at the range
one small step further: give the condition a name, so the
ifreads like a sentencea well-named boolean is often better than a comment, because it can't go out of date
if this idea interests you, there's a thread worth pulling. in 1984 donald knuth argued that a program should be written first for a person to read, and only second for the computer. he called it "literate programming"
there's a well-known story that goes with it. knuth was asked to write a program that finds the most common words in a text, and he wrote a long, carefully explained one. doug mcilroy reviewed it and answered with six lines of shell that did the same job. both are in this column from 1986