Question: Write a function addWithSurcharge 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. Example: addWithSurcharge(5, 15) should return 23. Answer: function addWithSurcharge (a,b) { if (a <= 10 && b <= 10) { return a + b + 2; } else if (a > 10 && b > 10) { return a + b + 4; } else return a + b + 3; } ; or function addWithSurcharge(a, b) { let surcharge = 0; if (a <= 10) { surcharge = surcharge + 1; } else { surcharge = surcharge + 2; } if (b <= 10) { surcharge = surcharge + 1; } else { surcharge = surcharge + 2; } return a + b + surcharge; } > in the first example we declare a function addWithSurcharge >>> it has two parameters >>> we create if / else if / else statements to...
Comments
Post a Comment