Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Trying to make an array work with Push method

I'm trying to work with arrays and the push method. I want two different arrays that compute from a set of variables. When I am done I want an array for three restaurant bills and then an array with a restaurant bills with the tip added for each (20% tip for bills under $50, 15% for bill between $50 and $200, 10% tips for bill over $200). Here is what I have For some reason, when this runs, I only get one value on each array and it seems to be wrong Math, what am I doing wrong?

(NO!! THIS IS NOT MY HOMEWORK!!!! I have a autodidact Web developer who always SELF-ASSIGNS my OWN HOMEWORK)

const grandTotal = [];

const tipTotal = [];



 const burgerKing = 48;

 const redRobin = 124;

 const bonefishGrill = 268;



if (burgerKing <= 50) {

    const burgerKingTip = burgerKing * 0.2
    tipTotal.push(burgerKingTip);
    grandTotal.push(burgerKingTip + burgerKing);

} else if (redRobin <= 201 || redRobin >= 50) {

    const redRobinTip = redRobin * 0.15;
    tipTotal.push(redRobinTip);
    grandTotal.push(redRobin + redRobinTip);

} else if (bonefishGrill <= 268) {

    const bonefishGrillTip = bonefishGrill * 0.1; 
    tipTotal.push(bonefishGrillTip);
    grandTotal.push(bonefishGrillTip + bonefishGrill);
}

console.log(tipTotal);

console.log(grandTotal);

1 Answer

Your first condition evaluates to true

if (burgerKing <= 50) {

so only the code for that condition is executing.