0

new user here. Can anyone help me solve this question? Refer question here https://i.stack.imgur.com/kbKCl.jpg

This is what I manage to do so far, full of errors of course, is it correct if I use switch case? Or maybe I can use if...else statement?

<!DOCTYPE html>
<html>
<head>


<script type="text/javascript">


var price;
var weight = parseFloat(prompt("Enter amount of laundry: ", ""));
var rate;
switch (weight)
{
case 1:
  rate = weight <= 10;
  price = 2.00;
  break;

 case 2:
  rate = weight >= 10 && weight <= 20;
  price = 1.50;
  break;

case 3:
   rate = weight >= 20;
   price = 1.00;
   break;
}

var total = price * weight;


alert
(
"\nWeight :    " + weight + 
"\nRate   :    " + price + 
"\nTotal  : RM " + total 
);

</script>
</head>

</html>`

1 Answers1

0

You could use a swtich statement as explained here

If you don't want to use this statement, you can always resolve this with if/else to find out what the user entered:

if (weight <= 10)
{
  price = 2.00;
}
else if (weight <= 20)
{
  price = 1.50;
}
else
{
  price = 1.00;
}

For information: the correct use of the swith statement can be found here

Community
  • 1
  • 1
Bob Claerhout
  • 781
  • 5
  • 24
  • After the user key in the amount, how do the system ask the user using confirm box to choose 'ironing' or not? And what is the formula for calculating the additional price for 'ironing' ? // you can refer the image on the very top of post. Thank you. – user2691484 Aug 17 '13 at 09:06
  • http://www.w3schools.com/js/tryit.asp?filename=tryjs_confirm is an example of the confirmbox. Replace the 'x="You pressed OK!";' with 'price += 5' and you should get the result you want. – Bob Claerhout Aug 17 '13 at 09:25
  • If this solved your problem, please mark this answer as the good one. Thanks – Bob Claerhout Aug 18 '13 at 21:20