Practise exercise 16: Shopping Trip

So, my code works, kind of.
Using console.log it prints the correct amount, but for some reason it does not alert the user in a pop up with the sum / correct amount. I cant figure out why, any ideas?

var bread = 2;
var milk = 1.5;
var cheese = 4;
var yoghurt = 1.2;


alert("Hi! Welcome to Mike's Shop!");

var inputBr = prompt("How much bread would you like?");
var inputMi = prompt("How much milk would you like?");
var inputCh = prompt("How much cheese would you like?");
var inputYo = prompt("How much yoghurt would you like?");

var sum = ((inputBr*bread)+(inputMi*milk)+(inputCh*cheese)+(inputYo*yoghurt));

alert("Thank you for your order, please pay", sum);
console.log("Total amount eur:", sum);

Hi @MacCypher this code should work…

1 Like

Yes it works, accept for this part: alert("Thank you for your order, please pay", sum);
This is the result on screen:


As you can see, the actual amount of [sum] is not printed on screen.
:man_shrugging:

1 Like

You might want to use:
alert("Thank you for your order, please pay " + sum);

since the string format does not work in the same way for “alerts”.

Carlos Z

Thank you @thecil
That worked!

1 Like