user3985836
user3985836

Reputation:

Javascript calculator returning incorrect values

I am new to javascript so I am probably making a really simple mistake, but basically what it is is that I am trying to make a small price calculator in javascript and it works but the problem is that the number it returns is ridiculously high.

I have done a lot of searching and I cant find an answer, I have tried avoiding floating numbers and dividing as I am doing now as it got me closer to what I believe is the correct number. I have tried using .toFixed on more of my code to see if any of that is causing my missfortune but no luck. As it is now if you try it against 300 business cards in color, single sided it will probably say that it is "£100.0614.29" the only way I got this lower was by adding a stupid amount more zeroes

var click = (amount / 3) / 1000;

if (color === "color") {
  click = (amount / 5) / 1000;
};

here.

But that will only get it down to 1000 ish and if I remove the click charge it is still 1000 ish.

Edit

I am looking more for an explanation why I am getting these results and help getting the answer to make it work like I think it should. Giving enough time and sleep I think I could fix this, I just dont understand why this is happenening.

Upvotes: 2

Views: 70

Answers (2)

Krzysztof Krawiec
Krzysztof Krawiec

Reputation: 127

Hi there is your code changed a bit. First of all i have added a check when you divide number of "jobs". Lets say that you can fit 4 "jobs" on a single page - then when the order is 7 "jobs" you nedd to count two pages not one(like it was in your script)

But finally all you need to do is to set price (for B&W pages) and priceColor (for color ones)

    var type = prompt("What type of job is it?").toLowerCase();
    console.log(type);

    var amount = prompt("how many would you like?");
    console.log(parseInt(amount));

    if (type === "business cards" || type === "bus cards" || type === "bus card") {
      amount = (amount % 21) > 0? (amount / 21)+1 : amount / 21;
      console.log(parseInt(amount));

    } else if (type === "leaflets" || type === "Flyers" || type === "a5") {
      amount = (amount % 4) > 0? (amount / 4)+1 : amount / 4;
      console.log(parseInt(amount));
      
    } else if (type === "letterheads" || type === "a4") {
      amount = (amount % 2) > 0? (amount / 2)+1 : amount / 2;
      console.log(parseInt(amount));
    };

    var color = prompt("Black and white or color?").toLowerCase();
    console.log(color);

    var side = prompt("Is it single or double sided?").toLowerCase();
    console.log(side);

    var price = 10;
    var priceColor = 15;

    if (side == "double sided") {
      price = price * 2;
      priceColor = priceColor * 2;
    };

    if (color === "color") {
      price = priceColor;
    };


    function add() {
      return "£" +(price*amount);
    };

    console.log(add())

Hope this helps.

Upvotes: 0

Igor
Igor

Reputation: 15893

Conversion of types in arithmetic operations is done in the order the operations are executed. In your expression, everything is converted to strings that are concatenated.

var rip = 11;
var click = 22;
var type = 33.44;

return "£" + rip + click + type.toFixed(2);

= "£112233.44"

return "£" + (rip + click + type).toFixed(2);

= "£66.44"

Upvotes: 2

Related Questions