@md5dalton
Posted
Hello Tyler,
To avoid repeating yourself when you write your code, try identifying repetitions in your code patterns which can lead to it being hard to maintain, for example you can:
- De-structure the
props
object and use the values extracted where needed. - Use functions to make your code reusable and organized, you can write a function that accepts a value and returns a string with two decimal points to get Price:
This is how I'd rewrite Output component without changing to much of your code:
// De-structure values you need from props and leave props if you need to use it somewhere
function Output ({ people, billAmount, percentage, ...props }) {
// Minimum value of people should be 1
people = people < 1 ?? 1
// Function to get Price
const getPrice = value => value.toFixed(2)
// Use that function to get corresponding values
const getTip = getPrice(billAmount * (percentage/100)/people)
const getTotal = getPrice(billAmount * (1 + percentage/100)/people)
...
}
Marked as helpful
@tylermaks
Posted
@md5dalton thank you, much appreciated!