I have been trying to write a function, where a for loop goes over an array with three numbers (bills), multiplies each array element depending on its value, and then returns the new array(tips), containing the multiplied values. I tried doing it with a for loop, however, the returned array only contains element number 2, while elements 0 and 1 return empty. Where am I going wrong? Thanks a lot for your help!
var bills = [10, 20, 40];
function tipCalc(arr) {
var tips = [];
for (i = 0; i < arr.length; i++) {
if (arr[i] < 10) {
tips[i] = arr[i] * 0.6;
} else if (arr[i] > 10 && arr[i] < 20) {
tips[i] = arr[i] * 0.5;
} else if (arr[i] > 30) {
tips[i] = arr[i] * 0.2;
}
}
return tips;
}
console.log(tipCalc(bills))