0

I need to use variable arguments to inner function.

I have this function that I cannot change:

function SUM() { 
  var res = 0;
  for (var i = 0; i < arguments.length; i++) {
    res += (arguments[i]==null?0:arguments[i]);
  }
  return mSum(res);
}

Well because this function sometimes returns extra decimal values, I want to wrap it into a function like

function MySUM( return parseFloat(SUM().toFixed(10))    );

the problem is that SUM function cannot read "arguments" of outer function that calls it, and I don't know how to pass this arguments from mySUM to inner SUM function. What I expect is some like this

function MySUM( return parseFloat(SUM(arguments).toFixed(10))    );

but it doesn't work.

3
  • please add an example of your array. Commented Jul 18, 2019 at 15:20
  • What is the original SUM() function supposed to return? Commented Jul 18, 2019 at 15:23
  • So you're adding a wrapper to another wrapper? Can you change mSum()? You shouldn't need to check for null since js will treat it like 0 in this case. res += arguments[i]; should give you the same result for res. Commented Jul 18, 2019 at 15:25

2 Answers 2

1

You can spread the arguments using the ... operator, send them to the SUM function:

function MySUM() {
    return parseFloat(SUM(...arguments).toFixed(10));
    // Here --------------^
}
Sign up to request clarification or add additional context in comments.

Comments

1

You can do it like this:

function MySUM(arguments) {
    return parseFloat(SUM(arguments).toFixed(10))
}

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.