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.
Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    New Coder
    Join Date
    Mar 2012
    Location
    USA
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Is a loop needed? I have NO idea !

    Hello All,

    I have made the below code but i want to add something extra to it. I have tried to do it several times already but the code just seems to stop working

    What i need it to do is to keep asking the user to enter the CD Title and CD Cost repeatedly and to total all the CD Costs together.


    <html>
    <body>
    <script language='JavaScript'> //set the language
    a=(prompt('please enter the CD Title:',0)); // enter the cd title promt
    b=parseFloat(prompt('please enter the CD Cost:',0)); // enter the cd cost promt

    document.writeln(' your CD Title is: <b>' + a + '</b>'); //write the cd title to the screen in bold
    document.writeln(' your CD Cost is: <b>' + b + '</b>'); //write the cd cost to the screen


    </script>
    </body>
    </html>


    E.g.

    Customer wants to buy X CD's.

    The prompt will appear X times untill the customer has no more CD's they want to buy

    All the CD Titles & CD Costs should be written to the screen

    With Total

    End Product:

    your CD Title is: RnB Classic Vol 1 your CD Cost is: 1.00
    your CD Title is: RnB Classic Vol 2 your CD Cost is: 1.00
    your CD Title is: RnB Classic Vol 3 your CD Cost is: 1.00

    Total Cost 3.00


    Can anyone help?

  • #2
    Banned
    Join Date
    Apr 2011
    Posts
    656
    Thanks
    14
    Thanked 69 Times in 69 Posts
    Since X can vary from user to user, I would use a WHILE loop to keep prompting the user for info and do any processing you need to do until a counter reaches X in value. Then output whatever data you need to display.

  • #3
    New Coder
    Join Date
    Mar 2012
    Location
    USA
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question

    Quote Originally Posted by webdev1958 View Post
    Since X can vary from user to user, I would use a WHILE loop to keep prompting the user for info and do any processing you need to do until a counter reaches X in value. Then output whatever data you need to display.
    Ummm I haven't got a clue on how to do while loops or loops in fact. My code just seems to stop working when i try.

  • #4
    Banned
    Join Date
    Apr 2011
    Posts
    656
    Thanks
    14
    Thanked 69 Times in 69 Posts
    Quote Originally Posted by samhor View Post
    Ummm I haven't got a clue on how to do while loops or loops in fact. My code just seems to stop working when i try.
    So what question are you asking then, and so I'm not sure what you want someone to do.

    This sounds like a homework exercise. There is a good example/tutorial on WHILE loops on w3schools

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,537
    Thanks
    77
    Thanked 4,381 Times in 4,346 Posts
    I would suggest that, instead of first asking how many CDs the user wishes to buy, you instead simply keep asking the questions until the user responds with a blank answer (non-answer) to the first question.

    My code just seems to stop working when i try.
    And we won't write the code for you since this is homework. But if you SHOW us what you have tried, we might be able to give you clues as to what you are doing wrong.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,015
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    Quote Originally Posted by Old Pedant View Post
    I would suggest that, instead of first asking how many CDs the user wishes to buy, you instead simply keep asking the questions until the user responds with a blank answer (non-answer) to the first question.
    Or clicks the cancel button (unlike a form field that returns null).
    Hint - use a while loop .....

    You have said "I haven't got a clue on how to do while loops or loops". If so, there is something seriously wrong. Have you not attended (or paid attention in) your classes, or is your teacher incompetent and sets assignments on topics not yet properly covered?
    Last edited by Philip M; 03-20-2012 at 12:18 AM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #7
    Banned
    Join Date
    Apr 2011
    Posts
    656
    Thanks
    14
    Thanked 69 Times in 69 Posts
    Quote Originally Posted by webdev1958 View Post
    Since X can vary from user to user, I would use a WHILE loop to keep prompting the user for info and do any processing you need to do until a counter reaches X in value. Then output whatever data you need to display.
    Actually, I think a do-while loop is a better option. But in any case, depending on how you choose to do it, you could use a FOR, WHILE or DO-WHILE loop.

    The w3schools link I posted has examples on all type of loops.

    If you need more help, post back with your updated code

  • #8
    New Coder
    Join Date
    Mar 2012
    Location
    USA
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    I would suggest that, instead of first asking how many CDs the user wishes to buy, you instead simply keep asking the questions until the user responds with a blank answer (non-answer) to the first question.


    And we won't write the code for you since this is homework. But if you SHOW us what you have tried, we might be able to give you clues as to what you are doing wrong.
    Thanks for all your assumptions but you need correcting this is not homework put simply myself trying to upgrade my knowledge of js

  • #9
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,537
    Thanks
    77
    Thanked 4,381 Times in 4,346 Posts
    Quote Originally Posted by samhor View Post
    Thanks for all your assumptions but you need correcting this is not homework put simply myself trying to upgrade my knowledge of js
    Then, really, you shouldn't be using prompt() at all. Or document.write().

    Learn to use <form>s and DOM methods, etc., as you really will need to do to create any normal web site.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #10
    Banned
    Join Date
    Apr 2011
    Posts
    656
    Thanks
    14
    Thanked 69 Times in 69 Posts
    [0t]

    Quote Originally Posted by Old Pedant View Post
    you shouldn't be using prompt() at all. Or document.write().
    There's nothing wrong with using prompt or document.write when starting out. Heck, even when I was at school we initially were taught how to use them. Not only to make us aware they exist but to help us learn the basics of looping without having to worry about the formatting or method of input/output too much.

    Later on we moved onto more appropriate DOM methods

    Quote Originally Posted by Old Pedant View Post
    And we won't write the code for you since this is homework.
    And who is the "we" you are referring to? I hope it's not me included in the "we" because you don't have the clout to speak on my behalf . Although I don't normally do so, in the past, where appropriate I have written code for homework and I will continue to do so in the future where appropriate.

    Also, I assume you are aware that doing someone's homework is not against this forums rules, but yes it is frowned upon.

    [/ot]
    Last edited by webdev1958; 03-20-2012 at 01:09 AM.

  • #11
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,537
    Thanks
    77
    Thanked 4,381 Times in 4,346 Posts
    I just don't think it is that much harder to learn to use a <form> than it is to learn to use promot( ) so I don't see the point in taking the time to learn to use the latter. But what the heck, you want to help him write this, then go for it!

    And as for homework: I am citing the *SPIRIT* of Rule 1.5 of this forum. Yes, I know it doesn't specify that we can't give homework answers. It just says they can ask for homework answers. Semantic quibbling. The intent is clear, to me. Oh, and yes I have helped a *LOT* with homework. And on a few occasions tossed up my hands in frustration and written the code when I couldn't get across to the student. But I sure prefer to think that is a last resort. (On the other hand, I don't understand what relevance there is in, say, a Library Science student taking a course in JavaScript. It's really only those who claim to be computer science majors that bug me when they want us to write the code for them. Who the heck is going to do that for them when they get their first job?)
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #12
    Banned
    Join Date
    Apr 2011
    Posts
    656
    Thanks
    14
    Thanked 69 Times in 69 Posts
    [ot]

    That's ok I was just making sure I wasn't included in your "we"

    [/ot]

  • #13
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,537
    Thanks
    77
    Thanked 4,381 Times in 4,346 Posts
    Code:
    <html>
    <body style="background-color: white;">
    <script type-"text/javascript"> <!-- language= is obsolete -->
    var total = 0;
    while ( true )
    {
        a=(prompt('please enter the CD Title:','')); // enter the cd title promt
        if ( a == "" ) break; // exit the loop when no title is given
        b=parseFloat(prompt('please enter the CD Cost:',0)); // enter the cd cost promt
        total += b;
        document.writeln(' your CD Title is: <b>' + a + '</b><br/>'); //write the cd title to the screen in bold
        document.writeln(' your CD Cost is: <b>$' + b.toFixed(2) + '</b><hr/>'); //write the cd cost to the screen
    }
    document.writeln(' your total cost for the CDs is: <b>$' + total.toFixed(2) + '</b>');
    </script>
    </body>
    </html>
    There. Homework or not, a solution that meets the request.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #14
    New Coder
    Join Date
    Mar 2012
    Location
    USA
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I think i have done it now . Any ideas on how to put it in to a table to make it look nicer?

    <html>
    <pre>
    <body>
    <script language="JavaScript">

    c=parseFloat(prompt("please enter the number of cds you will be purchasing:",0));
    counter=0;
    sum=0;

    while (counter<c)

    {


    a=(prompt("please enter the CD Title:",0));
    b=parseFloat (prompt("please enter the CD Cost:",0));

    counter=counter+1;

    sum=sum+b;

    document.write("CD title entered: <b><i> " + a + "</b></i></br>");
    document.write("CD cost entered: <b><i> " + b + "</b></i></br>");

    }



    document.write("total cost is:", "=", sum);

    </script>
    </body>
    </pre>
    </html>

  • #15
    Banned
    Join Date
    Apr 2011
    Posts
    656
    Thanks
    14
    Thanked 69 Times in 69 Posts
    Quote Originally Posted by Old Pedant View Post
    ..... But what the heck, you want to help him write this, then go for it!
    If the OP (not you ) comes back with updated code, I will


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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