1

Learning the basics and had this exercise:

"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. The call addWithSurcharge(5, 15) should return 23."

I had put:

function addWithSurcharge(num1, num2) {
if (num1 <= 10) {
num1 += 1;
} else {
num1 += 2;
}
if (num2 <= 10) {
num2 += 1;
} else {
num2 += 2;
}
return num1 + num2;
}

Which works, but want to learn better syntax. Could something like

if (num1, num2 <= 10) {...

work? I realize can't do the || operator because it could affect the wrong num.

4 Answers 4

2

Put the numbers into an array instead, and iterate over the array so you don't have to repeat both the num1 and num2:

function addWithSurcharge(...nums) {
  return nums.reduce((a, num) => {
    if (num <= 10) {
      num += 1;
    } else {
      num += 2;
    }
    return a + num;
  }, 0);
}
console.log(addWithSurcharge(5, 15));

Without .reduce, the above is equivalent to:

function addWithSurcharge(...nums) {
  nums.forEach((num, i) => {
    if (num <= 10) {
      nums[i] += 1;
    } else {
      nums[i] += 2;
    }
  });
  return nums[0] + nums[1];
}
console.log(addWithSurcharge(5, 15));

You can use the conditional operator to make the if/else part more concise without bothering with reassignment, too:

const addWithSurcharge = (...nums) => (
  nums.reduce(
    (a, num) => a + num + (num <= 10 ? 1 : 2),
    0
  )
);
console.log(addWithSurcharge(5, 15));

Sign up to request clarification or add additional context in comments.

Comments

2

You could also do:

function addWithSurcharge(num1, num2) {
return num1 + num2 + num1<=10?1:2 + num2<=10?1:2;
}

The num1<=10?1:2 basically means if(num1<=10){1} else{2} which works really well!

2 Comments

This is pretty clever! Thanks for teaching me this ? operator.
Yeah sure! The basic format is like this: bool?val1:val2 If the bool is true then it will return val1 Otherwise it will return val2. So we can use this as an inline if(){}else(){} statement. How that works is num1<=10 is the boolean and 1 and 2 are the nums
2

The most basic improvement is a helper function that you can call twice:

function withSurcharge(num) {
  if (num <= 10) return num + 1;
  else return num + 2;
}
function addWithSurcharge(num1, num2) {
  return withSurcharge(num1) + withSurcharge(num2);
}

You could further shorten the if/else statement to use a conditional expression, but not much is gained by that:

return num + (num <= 10 ? 1 : 2);

1 Comment

Thanks for this. I'm still struggling with syntax. From the examples I'm playing with and seeing I didn't know you could put a functions parameter as another functions parameter--I probably said that wrong.
1

One way you could do this would be to use the ternary opertator

function addWithSurcharge(num1,num2)
{        
    num1 += num1 <= 10 ? 1 : 2;
    num2 += num2 <= 10 ? 1 : 2;
    return num1 + num2;
}

console.log(addWithSurcharge(10,5));

If you use a ? when assigning a value it'll evaluate whatever is on the left hand side of the ? and either return the left hand side of the : if it's true or the right hand side if it's false.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.