settingsAccountsettings
Menusettings

Q: Sum numbers of prices and calculate their VAT - JS function

+2 votes

I need to write a JS function that sums a variable number of prices and calculates their VAT (Value Add Tax, 20%).

The input comes as an array of numbers. The number of elements will be different every time.

The output should be printed to the console on a new line for every entry.

Input:
[1.20, 2.60, 3.50]

Output:
sum = 7.3
VAT = 1.46
total = 8.76

////////////////////////////////////
Input:
[3.12, 5, 18, 19.24, 1953.2262, 0.001564, 1.1445]

Output:
sum = 1999.732264
VAT = 399.94645280000003
total = 2399.6787168
 

asked in JavaScript category by user matthew44
edited by user golearnweb

1 Answer

+1 vote
 
Best answer

Here's the answer:

function sumVat(input) {
    var sum = 0;
    for (var num of input) sum += num;
    console.log("sum = " + sum);
    console.log("VAT = " + sum * 0.2);
    console.log("total = " + sum * 1.2);
}

//sumVat([1.20, 2.60, 3.50]);
sumVat([3.12, 5, 18, 19.24, 1953.2262, 0.001564, 1.1445]);

 

answered by user mitko
edited by user golearnweb
...