const bills = [124, 48, 268];
const tipAmount = [];
const totalPaid = [];
const tipCalc = (bill) => {
let tip
switch(true) {
case(bill < 50 ):
tip = bill*0.2;
console.log(tip);
return tip;
case(bill > 50 && bill < 200):
tip = bill*0.15;
console.log(tip);
return tip;
case(bill > 200):
tip = bill*0.1;
console.log(tip);
return tip;
}
};
tipAmount.push(bills.map(el => tipCalc(el)));
console.log(tipAmount[0]);
const totalCalc = (thething) => {
let onArray = bills.indexOf(thething);
let result = Number(bills[onArray]) + Number(tipAmount[0][onArray]);
return result;
};
totalPaid.push(bills.map( el => totalCalc(el)));
console.log(totalPaid[0]);
So first of all, this code works right now and I am well aware that I went for an overkill, I just wanted to try working with 2 arrays and stuff and now I'm trying to figure out why this is happening...
when I do tipAmount.push(bills.map(el => tipCalc(el))); an array inside the tipAmount array gets created and to further access the data I've put in tipAmount I have to do tipAmount[0][i], I'm trying to understand why this is happening and what I would have needed to have done to access this data simply by doing tipAmount[i]
thank you in advance
.map(...)returns an array, that's why.mapreturns an arraymapreturns an arraytotalPaid.push(totalCalc(el));