Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6744151
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:02:57+00:00 2026-05-26T12:02:57+00:00

I’m trying to write a simple javascript, or so I think, using while loops,

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T12:02:58+00:00Added an answer on May 26, 2026 at 12:02 pm

    You should clean up your code a bit:

    • Indent everything to make it more readable
    • Remove the <center> and bgcolor="white" parts since they do not add anything – your <center> element is empty, and a page is white by default and bgcolor is deprecated anyway
    • All your JavaScript code should be just in one block, no need for three of them. Also you should put all JavaScript into the head instead of mixing part of it into the body
    • Your code should not work because of your ; after the } of if, but since you said it worked partially this probably is not the problem. Be sure not to put ;s after if clauses, though.

    A working version: http://jsfiddle.net/pimvdb/2dqSr/.

    var result = 0; // result is 0 at first
    var meal   = +prompt("Number of Items: "); // convert into a number with +
    var tip    = 1.07;
    
    if (meal > 0) {
        var index = 0;
        while (index < meal) {
            var price = +prompt("price of the item");
            result = result + price; // add price to result
            index++; // increment index, so that while loop will stop after 'meal' times.
                     // If you set it to 'meal' directly, then 'index' will be equal to
                     // 'meal' after the first iteration, and the while loop will only
                     // run once (after the first iteration 'index < meal' is false).
        }
        alert(result * tip); // alert total price * tip
    
    } else if (meal == 0) {
        alert("Number is 0");
    
    } else if (meal < 0) {
        alert("You entered is negative");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a French site that I want to parse, but am running into
I am trying to loop through a bunch of documents I have to put
I'm making a simple page using Google Maps API 3. My first. One marker
I have thousands of HTML files to process using Groovy/Java and I need to
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.