Repository navigation
Angelika S - #8
Angelika S #8angelika-arch wants to merge 2 commits into
Conversation
|
hi @angelika-arch, generally solid work, the logic in all three tasks is well thought through a few comments for you to review, the one on line 7 of |
amgando
left a comment
There was a problem hiding this comment.
generally solid work, well done
the comment on line 7 of login.js is the one worth spending time on. the rest are ideas to think about
finding that 4th bug in the converter was the best thing in this PR
| if ( | ||
| input.trim() === '' || !Number.isInteger(year) || year < 1 || year > 9999 | ||
| ) { |
There was a problem hiding this comment.
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 if reads like a sentence
const isValidYear = input.trim() !== '' && Number.isInteger(year) && year >= 1 && year <= 9999;
if (!isValidYear) {
console.log('Invalid year!');
}a 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
| 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`); |
There was a problem hiding this comment.
two things here
first, the assignment gives the exact text to print
Yes, 2028 is a leap year
No, 1900 is not a leap year
Invalid year!
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 else if chain is correct too, and arguably easier to follow against the three rules. there are always several ways
| // 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.
try this: enter a wrong password 4 times, then the correct one
1. admin / x -> Inccorrect credentials
2. admin / x -> Inccorrect credentials
3. admin / x -> Inccorrect credentials
4. admin / x -> Login blocked: To many inccorect attempts
5. admin / Hack1234 -> Logged in successfully
the page said "blocked" and then let me in
the reason: you check the same number in two places with two different comparisons, > 4 here and >= 4 on line 22. they disagree about attempt number 5. this kind of slip is so common it has a name, an "off-by-one error"
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 4 in the code is a "magic number", the reader has to guess what it means, and when it appears twice the two copies can drift apart
| errorMessage('Login blocked: To many inccorect attempts'); | ||
| } else { | ||
| errorMessage('Inccorrect credentials'); |
There was a problem hiding this comment.
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
- copy text like this straight from the assignment instead of retyping it
- install code spell checker in vs code, it underlines typos in strings and comments
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
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
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
| const eurAmount = usdAmountNum / EUR_USD_RATE; | ||
| // Show the result | ||
| console.log( | ||
| `${usdAmountNum.toFixed(2)} USD is equal to ${eurAmount.toFixed(2)} EUR.` |
There was a problem hiding this comment.
nice catch on the 4th bug, the one that wasn't in the list
the template literals are a good upgrade too
| @@ -1,37 +1,70 @@ | |||
|
|
|||
There was a problem hiding this comment.
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
No description provided.