IF、OR 和 AND 以及不同的结果 JavaScript
IF, OR and AND and the different results JavaScript
我要解决这个问题:
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.
所以我展示了这个解决方案:
const addWithSurcharge = (a, b) => ((a <= 10) || (b <= 10) || Math.min(a + b, 20)) ? ((++a)) + ((++b)) : (2+a) + (2+b);
一切顺利,直到函数将 (11, 10) 作为参数。如果我使用 OR 给我 23,如果我使用 AND 结果给我 25,但不是我想要的 24。我做错了什么吗?看来我只传递了对其中一个参数的添加。我正在学习,所以任何帮助将不胜感激。
您可以对待每个值并检查该值并添加附加费。
const
surcharge = v => v <= 10 ? 1 : 2,
addWithSurcharge = (a, b) => a + surcharge(a) + b + surcharge(b);
如果,否则如果和其他。作为一个魅力。谢谢Nina Scholk
const
surchage = v => v <= 10 ? 1 : v <= 20 ? 2 : 3,
addWithSurcharge = (a, b) => a + surchage1(a) + b + surchage1(b);
我要解决这个问题:
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.
所以我展示了这个解决方案:
const addWithSurcharge = (a, b) => ((a <= 10) || (b <= 10) || Math.min(a + b, 20)) ? ((++a)) + ((++b)) : (2+a) + (2+b);
一切顺利,直到函数将 (11, 10) 作为参数。如果我使用 OR 给我 23,如果我使用 AND 结果给我 25,但不是我想要的 24。我做错了什么吗?看来我只传递了对其中一个参数的添加。我正在学习,所以任何帮助将不胜感激。
您可以对待每个值并检查该值并添加附加费。
const
surcharge = v => v <= 10 ? 1 : 2,
addWithSurcharge = (a, b) => a + surcharge(a) + b + surcharge(b);
如果,否则如果和其他。作为一个魅力。谢谢Nina Scholk
const
surchage = v => v <= 10 ? 1 : v <= 20 ? 2 : 3,
addWithSurcharge = (a, b) => a + surchage1(a) + b + surchage1(b);