0
    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

5
  • 2
    .map(...) returns an array, that's why. Commented Jul 13, 2020 at 20:19
  • 2
    because map returns an array Commented Jul 13, 2020 at 20:19
  • Simply push the totalCalc onto the array, since map returns an array totalPaid.push(totalCalc(el)); Commented Jul 13, 2020 at 20:20
  • @B001ᛦ on here developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… when it pushes 'cow' into the animals array, push doesn't create an array inside the animals array, it simply just adds 'cow' into there, I expect push to do the same for me and instead of what it's doing right now I expect it to simply add the returned numbers into the empty array Commented Jul 13, 2020 at 20:24
  • @ISAE aw that's right, i got it, i got it ... i shouldve used concat Commented Jul 13, 2020 at 20:30

3 Answers 3

1

I would recommend you to go through javascript array map method, map will returns a new array. when you do

tipAmount.push(bills.map(el => tipCalc(el)))

it will push a new array returned by bills.map function. just spread new array into the push method,

    tipAmount.push(...bills.map(el => tipCalc(el)))
Sign up to request clarification or add additional context in comments.

Comments

1

You are pushing arrays into tipAmount with tipAmount.push(bills.map(...)). bills.map returns an array (a mapped version of itself), so tipAmount ends up being an array of arrays.

Comments

0
const bills = [124, 48, 268];
var tipAmount = [];
var 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 = tipAmount.concat(bills.map(el => tipCalc(el)));

console.log(tipAmount);

const totalCalc = (thething) => {

    let onArray = bills.indexOf(thething);
    let result = Number(bills[onArray]) + Number(tipAmount[onArray]);
    return result;
};

totalPaid = totalPaid.concat(bills.map( el => totalCalc(el)));

console.log(totalPaid);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.