Reputation: 35
I have to solve this problem:
Write a function that adds two amounts with surcharge. For each amount less than or equal to 10, the surcharge is 1. For each amount greater than 10, the surcharge is 2. The call(5, 15) should return 23.
So I displayed this solution:
const addWithSurcharge = (a, b) => ((a <= 10) || (b <= 10) || Math.min(a + b, 20)) ? ((++a)) + ((++b)) : (2+a) + (2+b);
All goes smooth until the function takes (11, 10) as paramethers. If I use OR gives me 23, if I use AND gives me as a result 25, but not the desired 24. Am I doing something wrong? It seems that I am passing only the adding to one of the parameters. I am in my way of learning, so any help will be appreciate it.
Upvotes: 2
Views: 99
Reputation: 35
And for if, else if and else. Works as a charm. Thanks Nina Scholk
const
surchage = v => v <= 10 ? 1 : v <= 20 ? 2 : 3,
addWithSurcharge = (a, b) => a + surchage1(a) + b + surchage1(b);
Upvotes: 0
Reputation: 386624
You could treat every value and check the value and add the surcharge.
const
surcharge = v => v <= 10 ? 1 : 2,
addWithSurcharge = (a, b) => a + surcharge(a) + b + surcharge(b);
Upvotes: 2