Hi, Im having a problem with a javascript calculation i am trying to do. ---------------- function Skirting() { var width = prompt("Please enter the total width of your room (meters):", ""); var length = prompt("Please enter the total length of skirting (meters):",""); var price = 2; return width + length * price ; ---------------- For some reason when i enter width=2 length=2 i get the answer 24. All i want is a simple calculation of width + length * price = 8. Can anyone see the mistake, do you need to put a plus in brackets or something? Thank you for any help
When you return the value, the actual thing that is happening is that your script concatenates the width value to the length value multiplied by price. To convert width to integer use Number(width) function Skirting() { var width = prompt("Please enter the total width of your room (meters):", ""); var length = prompt("Please enter the total length of skirting (meters):",""); var price = 2; return Number(width) + length * price ; Code (markup):
Thank you for the quick reply agnus Still no luck its still messing up, any chance someone can have a look at this/.... function Walls() { var width = prompt("Please enter the total width of your room (meters):", ""); var length = prompt("Please enter the total length of skirting (meters):",""); var price = 5; return Number(width) * 2 + length * price ; The javascript just dosnt to read it correctly for example it will use 3+4 =34 It should be... width x2 + length x price