-
-
Notifications
You must be signed in to change notification settings - Fork 438
London 10-Mahendra Balal-javascript core 1-coursework-week2 #445
base: main
Are you sure you want to change the base?
London 10-Mahendra Balal-javascript core 1-coursework-week2 #445
Conversation
if (isHappy) { | ||
return "I am happy"; | ||
} else { |
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.
There's a pattern in programming to simplify if .. else
statements. You can rewrite this block as follows:
if (isHappy) {
return "I am happy";
}
return "I am not happy";
the pattern is called return early pattern, and you can read more here https://medium.com/swlh/return-early-pattern-3d18a41bba8
function printOddNumbers(limit) { | ||
let i = 1; | ||
while (i <= limit) { | ||
if(i % 2 !==0) { | ||
console.log(i); | ||
} | ||
i++; | ||
} | ||
} |
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.
this works fine, but you could make it 2x more performant by skipping over even numbers. For example:
function printOddNumbers(limit) { | |
let i = 1; | |
while (i <= limit) { | |
if(i % 2 !==0) { | |
console.log(i); | |
} | |
i++; | |
} | |
} | |
function printOddNumbers(limit) { | |
let i = 1; | |
while (i <= limit) { | |
console.log(i); | |
i+=2; | |
} | |
} |
if (price1 < 0 || price2 < 0) { | ||
return "Invalid price for an item"; | ||
} |
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 add, always good to handle edge cases even if the exercise didn't mention it
let totalPrice = price1 + price2; | ||
let discount = Math.min(price1, price2); | ||
let discountedPrice = totalPrice - discount; | ||
|
||
return discountedPrice; |
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.
I think this can be simplified with the following:
let totalPrice = price1 + price2; | |
let discount = Math.min(price1, price2); | |
let discountedPrice = totalPrice - discount; | |
return discountedPrice; | |
return Math.max(price1, price2); |
What do you think?
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 work @mabalal, I left a few minor comments
Volunteers: Are you marking this coursework? You can find a guide on how to mark this coursework in
HOW_TO_MARK.md
in 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?