LONDON_10 | SAIM KORKMAZ | JS-1-1#510
Conversation
| function convertToBRL(price) { | ||
| price = price * 5.7 * 0.99; | ||
| return parseFloat(price.toFixed(2)); | ||
| } |
There was a problem hiding this comment.
Please look into pure functions and argument mutation.
It's considered a bad practice for a function to have side effects. Can you think of which side effect this function has and why would it be bad practice?
There was a problem hiding this comment.
only thing came my mind is "5.7" and "0.99" must have been const
| let step1 = add(startingValue, 10); | ||
| let step2 = multiply(step1, 2); | ||
| let goodCode = format(step2); |
There was a problem hiding this comment.
We should try to use const if we know the value is not going to be re-assinged/changed.
| if ( | ||
| answer == "It is certain." || | ||
| answer == "It is decidedly so." || | ||
| answer == "Without a doubt." || | ||
| answer == "Yes - definitely." || | ||
| answer == "You may rely on it." | ||
| ) { | ||
| return "very positive"; | ||
| } else if ( | ||
| answer == "As I see it, yes." || | ||
| answer == "Most likely." || | ||
| answer == "Outlook good." || | ||
| answer == "Yes." || | ||
| answer == "Signs point to yes." | ||
| ) { | ||
| return "positive"; | ||
| } else if ( | ||
| answer == "Reply hazy, try again." || | ||
| answer == "Ask again later." || | ||
| answer == "Better not tell you now." || | ||
| answer == "Cannot predict now." || | ||
| answer == "Concentrate and ask again." | ||
| ) { | ||
| return "negative"; | ||
| } else { | ||
| return "very negative"; | ||
| } |
There was a problem hiding this comment.
This is fine, but you are using a lot of magic words here. Try to look into what magic words are and how this could be improved.
Hint:
You can either use a JS object or array index to determine whether it's positive or negative
| } | ||
| function getTotal(a, b) { | ||
| total = a ++ b; | ||
| total = a + b; |
There was a problem hiding this comment.
This is missing a declaration, could you explain why it worked without a const or let declaration and why it would be a bad practice, even though it works?
There was a problem hiding this comment.
i made it global variable by mistake, but has a side effect it is it may cause unexpected behaviour
| function concatenate(firstWord, secondWord, thirdWord) { | ||
| // Write the body of this function to concatenate three words together. | ||
| // Look at the test case below to understand what this function is expected to return. | ||
| return firstWord.concat(" ", secondWord.concat(" ", thirdWord)); |
There was a problem hiding this comment.
this works, but could you think of a solution using Template Literals (I think it will be a cleaner solution)
There was a problem hiding this comment.
is it: return ${firstWord}${secondWord}${thirdWord}
| function calculateSalesTax(price) { | ||
| price = price + price / 5; | ||
| return price; | ||
| } |
There was a problem hiding this comment.
See previous comment about pure functions
| function addTaxAndFormatCurrency(price) { | ||
| price = price + price / 5; | ||
| return `£${price.toFixed(2)}`; | ||
| } |
There was a problem hiding this comment.
Just found out what it is
const newPrice = price + price/5
...a pure function should not modify its input parameters or any other external variables or state
There was a problem hiding this comment.
yes! that would be a better approach. We should try avoiding modification of parameters because if you call the same function multiple times you will keep getting different answers.
There was a problem hiding this comment.
Good job on the homework.
I have left a few comments to explore and think about, feel free to reach out on Slack or reply to comments if you would like to discuss them.
Oh and please try to get in the habit of filling out the pull request template, as this is very common in workplace.
Volunteers: Are you marking this coursework? You can find a guide on how to mark this coursework in
HOW_TO_MARK.mdin the root of this repositoryYour Details
Homework Details
Notes
What did you find easy?
What did you find hard?
What do you still not understand?
Any other notes?