I’m trying to write a simple javascript, or so I think, using “while” loops, “prompts”, and “alerts”.
I want the user to have a prompt to ask how many items he bought
then I want to take the number he entered and return the same number of prompts for the prices, then I want to use a while loop to total those prices up and add 7% sales tax to the whole thing, and then print the total off in an alert window
So here’s what I have so far:
<html> <head> <title> Meal Total </title>
<script language="JavaScript">
var result;
meal=prompt("Number of Items: ");
</script> <script language = "JavaScript">
var tip = 1.07; </script>
</head> <body bgcolor = "white"> <center> <script language = "JavaScript">
if (meal> 0) {
var index = 0;
while (index < meal) {
prompt("price of the item");
index = meal;
}
};
else if (meal == 0) {
alert("Number is 0");
} else if (meal <0) {
alert("You entered is negative");
} </script></center> </body></html>
As you can see, I have most of the coding done. I have it detecting a negative or 0 for the number inputed. I need the followign things corrected: First, why does it ignore the number of prompts given for the imput value? When I open it and enter two, only 1 prompt window is shown. Second, how do I sum the prompt boxes?
You should clean up your code a bit:
<center>andbgcolor="white"parts since they do not add anything – your<center>element is empty, and a page is white by default andbgcoloris deprecated anyway;after the}ofif, but since you said it worked partially this probably is not the problem. Be sure not to put;s afterifclauses, though.A working version: http://jsfiddle.net/pimvdb/2dqSr/.