Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Feb 2009
    Posts
    23
    Thanks
    4
    Thanked 0 Times in 0 Posts

    while loop question

    Evening all:

    I'm working on a for loop and was hoping for some help. My husband and I collect records, 45's that is. I'm trying to develop an interface that would allow input and then return a dollar value.

    For example, I would like a prompt to ask for the number of records, then loop through alert boxes asking for dollar value for each record, then return a total dollar value.

    I'm missing something, and I'm not sure what, I've been through it many times and somehow keep getting errors, or no output. Could someone please look at this and let me know what I am missing?

    function calculate(){
    var recnumber = parseInt(prompt("How many records were purchased?","2")); //prompt for number of records (45s)
    var rectotal = parseFloat(0); // declare and initialize running total
    var counter = 0; //counter

    while (counter <= recnumber)
    {
    var indrec = parseFloat(prompt("What is the dollar value of the record?","12.00"); //prompt for dollar value of a single record
    rectotal = (rectotal + indrec); //add indvidual record value to running total
    }
    return rectotal;

  • #2
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,108
    Thanks
    38
    Thanked 498 Times in 492 Posts

    Lightbulb

    Try changing this part:
    Code:
    do {
      var indrec = parseFloat(prompt("What is the dollar value of the record?","12.00"); //prompt for dollar value of a single record
      rectotal = (rectotal + indrec); //add indvidual record value to running total
    }
    while (counter <= recnumber);
    return rectotal;
    Above version causes execution of script at least once.

    Other versions of while (condition) { doSomething }
    if not written correctly, would cause script to be jumped over and not executed.

    See: http://www.w3schools.com/js/js_looping.asp

  • #3
    New Coder
    Join Date
    Feb 2009
    Posts
    23
    Thanks
    4
    Thanked 0 Times in 0 Posts
    I'll give it a shot, thanks for your reply.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •