3
06/11/2024 8:48 오전
주제 스타터
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.
입력:
[1.20, 2.60, 3.50]
출력:
sum = 7.3
VAT = 1.46
total = 8.76
////////////////////////////////////
입력:
[3.12, 5, 18, 19.24, 1953.2262, 0.001564, 1.1445]
출력:
sum = 1999.732264
VAT = 399.94645280000003
total = 2399.6787168
1개 답글
2
06/11/2024 8:49 오전
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]);
