Code
function Taxes(taxRate, purchases) {
let total = 0;
console.log(purchases);
for (let i = 0; i <= purchases.length; i++) {
total += purchases[i];
}
console.log(total);
return total * (taxRate/100 + 1);
}
console.log(Taxes(18, [15, 34, 66, 45]));
Explanation
I attempted to make a tax adder. The program adds the given list of array(the price of things that've been bought), add them together and multiply the answer with the tax rate. I converted it into python code and it works flawlessly.
However i encountered an error where in the for loop, the total is not summed with the indexed value so it gives an undefined error when i try to log it. I tried to replace it with a number and it works. But when i use a variable, it doesn't. How do i use a variable to pick an index.
SideNote
I know that i don't have to use a for loop to sum up the numbers in the array, but let's say i want to do it this way