0

I have no clue why this code doesn't work.

var tipCalculator = [1.2,1.15,1.10];
var restaurantBills = [124,48,738,10,300,150];
var finalBills = [];

function calculateBills(){
    for (var i = 0 ; i < restaurantBills.length ;  i++){

        switch(true){
            case restaurantBills[i]<50:
                finalBills.push(restaurantBills[i]*tipCalculator[0]);
                break;
            case restaurantBills[i]>50 && restaurantBills[i]<200 :
                finalBills.push(restaurantBills[i]*tipCalculator[1]);
                break;
            case restaurantBills[i]>200:
                finalBills.push(restaurantBills[i]*tipCalculator[2]);
                break;
            default:
                break;
        };
    };

return finalBills;};

console.log(calculateBills);
Alon Eitan
  • 11,997
  • 8
  • 49
  • 58
chris
  • 47
  • 2
  • 6
    You never *call* the function. Then the function gets its input from global state, not parameters, and doesn't deal with bills of exactly 50 or 200 dollars. – jonrsharpe Jul 11 '19 at 18:40
  • calculateBills is a function and you are not executing it.... your console should clearly show the function reference – epascarello Jul 11 '19 at 18:40
  • https://stackoverflow.com/questions/988363/how-can-i-debug-my-javascript-code – Heretic Monkey Jul 11 '19 at 18:41
  • 2
    What exactly is `switch(true){` suppose to be – Alon Eitan Jul 11 '19 at 18:41
  • to add to the above -- you need to add `()` to call a function for example: `calculateBills()` – Mark Jul 11 '19 at 18:42
  • yes i totally missed the () at the end. thank you epascarello – chris Jul 11 '19 at 18:43
  • @AlonEitan it is a bad practice (in most people's eyes) way of using it like if statements. – epascarello Jul 11 '19 at 18:44
  • @AlonEitan `switch (true) { case condition: ...` will run the first `case` where `condition` evaluates to true. – Heretic Monkey Jul 11 '19 at 18:45
  • @epascarello Oh, I have never seen it before, good to know – Alon Eitan Jul 11 '19 at 18:45
  • @AlonEitan you tell me , that's what was shown in the course im taking and its working after i added the () i forgot in the last line – chris Jul 11 '19 at 18:46
  • @epascarello is it considered a bad practice because of runtime issues? – chris Jul 11 '19 at 18:49
  • @chris I don't think it causes any runtime issues, but usually switch statements contain the expression to evaluate in the brackets, and then each case is a possible value. Using it in reverse like you are can be a little confusing to read at first. – IceMetalPunk Jul 11 '19 at 19:04

2 Answers2

1

calculate bills is a function and so must be called

calculateBills();

then your code should be fine

0

As pointed by jonrsharpe you never called the function that is returning the array. To make a function execute you need to call it.

change the line from

console.log(calculateBills);

to

console.log(calculateBills());

for debugging

Browsers like Chrome and Firefox comes with javascript debugger. Goto dev tools -> debugger where you would be able to set break points and trace the execution of your code.

Get Started with Debugging JavaScript in Chrome DevTools

How can I debug my JavaScript code?