Repository navigation
Angelika S - #9
angelika-arch wants to merge 1 commit into
Conversation
| function isBookApplicable(searchString) { | ||
| // Your code here | ||
| const title = "The fundamentals of JavaScript"; | ||
| const normalizedSearch = searchString.trim().toLowerCase(); |
There was a problem hiding this comment.
minor indentation here, probably by accident since the rest of your code is properly intended.
important to think of 3 audiences for your code:
- the computer
- other developers
- yourself in the future (in a few months you'll forget what you wrote here)
indentation signals scope (to the reader) in most programming languages.
python uses indentation (and nothing else) to define scope as part of the language itself
not a big deal here, but it can be a big deal if you don't notice it
| const first = Number(numbers[0]); | ||
| const second = Number(numbers[1]); | ||
| const year = Number(numbers[2]); | ||
|
|
There was a problem hiding this comment.
the section above has 6 lines of code doing things which are not documented anywhere (comments would be OK, better would be clearer code, see example below)
// parse incoming string: "MDY ##-##-####" or "DMY ##-##-####"
const parts = dateString.split(" ");
const format = parts[0]; // format is either "MDY" or "DMY"
const numbers = parts[1].split("-");
const first = Number(numbers[0]);
const second = Number(numbers[1]);
const year = Number(numbers[2]);minor adjustment
const first = Number(numbers[0]);
const second = Number(numbers[1]);
const year = Number(numbers[2]);can we rewritten as ... (this is called "destructuring the array")
const [first, second, year] = date.split("-")alternative, shorter, a little cleaner.
here the .map() function is used to convert each string to a number using the Number() constructor (see examples here).
// parse incoming string
const [format, date] = dateString.split(" ") // prefix MDY or DMY followed by space
const [first, second, year] = date.split("-").map(Number) // year is always last|
|
||
| if (format === "MDY") { | ||
| return { day: second, month: first, year: year }; | ||
| } |
There was a problem hiding this comment.
you're missing the alternative case of "DMY" as format string
imagine if someone passes any other string prefix
all of the following would work with the current version of your code but would fail in surprising ways
parseDateString("DMM 10-21-1983")
parseDateString("DDD 10-21-1983")
parseDateString("hi-there 10-21-1983")so generally speaking you need:
- complete coverage of all cases
- a default (
else) case to catch everything else
a better version of this section of the code would be:
if (format === "MDY") {
return ({ day: second, month: first, year });
} else if (format === "DMY") {
return ({ day: first, month: second, year });
} else {
throw new Error('Invalid format prefix') // or something like this
}you can also do this with a switch case statement
let result = {}
result.year = year;
switch (format) {
case 'MDY':
result.day = second
result.month = first
break;
case 'DMY':
result.day = first
result.month = second
break;
default:
throw new Error(`Invalid format prefix: ${format}`)
}
return result;There was a problem hiding this comment.
on second thought, a regex ("regular expression") is a good fix for some of the issues you can find here where you try to match the regex and, if it fails, you throw an error.
this way you don't run into problems with parsing the string if it has multiple spaces or only MM-DD and missing YYYY etc.
const correctFormat = /^(MDY|DMY) (\d{1,2})-(\d{1,2})-(\d{4})$/see here: https://regex101.com but be careful, regex is a big topic with small and rare usage. it's something that you use maybe once every year or two for very specific reasons like this one where you want to control for a very specific format in a piece of text.
| return { day: second, month: first, year: year }; | ||
| } | ||
|
|
||
| return { day: first, month: second, year: year }; |
There was a problem hiding this comment.
modern javascript allows you to simplify this if attribute name and value are the same. notice below I replaced year: year with just year
also make sure you surround any return value that can span multiple lines, like an object literal, with () to make sure it's returned as a single value. Without this, the risk is a line break right after return will break your code.
return ({ day: first, month: second, year});| @@ -0,0 +1,23 @@ | |||
| export function convertHoursToMinutes(hours) { | |||
| return hours * 60; | |||
| } | |||
There was a problem hiding this comment.
only comment here is to replace these magic numbers with constants like MINUTES_PER_HOUR
you can have a list of constants at the top of the file
const MINUTES_PER_HOUR = 60so the function becomes
export function convertHoursToMinutes(hours) {
return hours * MINUTES_PER_HOUR;
}| console.log("Wind chill in " + cityName3 + ": " + windChill3.toFixed(2) + "°C"); No newline at end of file | ||
| printWindChill("Amsterdam", 22, 15); | ||
| printWindChill("Berlin", 15, 20); | ||
| printWindChill("Copenhagen", -5, 25); No newline at end of file |
There was a problem hiding this comment.
overall this one is very good, nice work
|
hi @angelika-arch, generally speaking, solid work, well done a few comments for you to review, let me know if you have any questions |
amgando
left a comment
There was a problem hiding this comment.
generally solid work, well done
consider a few of the ideas i mentioned here and check out the links
there are 100 ways to solve any single problem. think of coding as art AND science
No description provided.