Hello, is there a way to correctly add past values ​​of arguments with new ones?

Code:

const TAX = 73; function getTax (buy) { let discount = buy / 100 * TAX; return discount; } console.log(getTax(9000)); console.log(getTax(18000)); console.log(getTax(27000)); console.log(getTax(36000)); console.log(`Налог с продаж (${TAX} %), к оплате: ${getTax()} Q`); 

In console.log( Sales Tax ($ {TAX}%), to be paid: $ {getTax ()} Q ); I need to get the sum of all the earlier arguments, that is, simply add up the values ​​of the earlier calculated argument values.

    1 answer 1

    It seems to be called memoization or something like that. You need to put somewhere an intermediate amount, for example in a closure, something like this -

     const TAX = 73; const getTax = (() => { let sum = 0; return (buy=0) => { sum += buy / 100 * TAX; return sum; } })() console.log(getTax(9000)); console.log(getTax(18000)); console.log(getTax(27000)); console.log(getTax(36000)); console.log(`Налог с продаж (${TAX} %), к оплате: ${getTax()} Q`); 

    And there are a million ways to do it differently)

    • Thank you very much :) - Feper