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 11 of 11

Thread: Loops?

  1. #1
    New to the CF scene
    Join Date
    Oct 2015
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Loops?

    So I have to get all my messages to show up, but it's just showing the default message. Is there a problem with my innerHTML code?

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>Voicemails</title>
    <link rel="stylesheet" type="text/css" href="mystyles.css">
    </head>
    <body>
    <h3>Your phone messages for the day</h3><br>
    <section id ="answer"></section>

    <script>
    var i;
    var msg;
    var answer;
    var voicemail;

    for ( var i= 0; i < 11; i++) {

    var randomNum = Math.floor((Math.random() * 10) + 1);

    switch (voicemail) {


    case 1:
    msg = ' You forgot your shoes at my house';
    break;

    case 2:
    msg = ' Hey it is Michael from Dr.Trishas office, could you call back to confirm your appointment';
    break;

    case 3:
    msg = ' Can you go pick up pizza for dinner. Mom will be home late';
    break;

    case 4:
    msg = 'Yooooo guess what! I just saw Anthony and Sarah at the mall. Call me ASAP!';
    break;

    case 5:
    msg = ' Pick up your phone!';
    break;

    default:
    msg = ' You have no messages';
    break;



    }


    }


    document.getElementById("answer").innerHTML +=msg;





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

  2. #2
    Senior Coder
    Join Date
    Aug 2006
    Posts
    1,872
    Thanks
    12
    Thanked 401 Times in 398 Posts
    For starters:
    Code:
    switch (voicemail) {
    What's the value of 'voicemail' here?

  3. #3
    Senior Coder
    Join Date
    Aug 2010
    Posts
    1,246
    Thanks
    44
    Thanked 273 Times in 271 Posts
    with this ...
    document.getElementById("answer").innerHTML +=msg;
    outside of the loop it will only run one time

  4. #4
    New to the CF scene
    Join Date
    Oct 2015
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Honestly not really sure... Just put voicemail for the switch value. I'm new at javascript so and Javascript kind of confuses me.

  5. #5
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    8,922
    Thanks
    4
    Thanked 885 Times in 873 Posts
    It looks like it would be a lot simpler code if you put the messages in an array instead of using a loop and a switch.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  6. #6
    New to the CF scene
    Join Date
    Oct 2015
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by felgall View Post
    It looks like it would be a lot simpler code if you put the messages in an array instead of using a loop and a switch.
    If I could take the easier route, but that's the rules for this assignment I have to do!

  7. #7
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    4,794
    Thanks
    15
    Thanked 513 Times in 508 Posts
    Quote Originally Posted by Leenangu View Post
    Honestly not really sure... Just put voicemail for the switch value.
    so what do you expect voicemail to be (i.e. contain what value)?
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  8. #8
    New to the CF scene
    Join Date
    Oct 2015
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Dormilich View Post
    so what do you expect voicemail to be (i.e. contain what value)?
    To be the messages.

  9. #9
    Senior Coder
    Join Date
    Aug 2010
    Posts
    1,246
    Thanks
    44
    Thanked 273 Times in 271 Posts
    generally, the purpose of the for loop would be to
    iterate through the case s,but the line ...
    var randomNum = Math.floor((Math.random() * 10) + 1);
    makes me think you want to print a randomly
    selected message 10 times

    the code right after
    a particular case
    is run if
    voicemail contains the associated value
    if the value is not, in this case
    1 2 3 4 or 5 then the code right after
    default is run


  10. #10
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    8,922
    Thanks
    4
    Thanked 885 Times in 873 Posts
    Quote Originally Posted by Leenangu View Post
    If I could take the easier route, but that's the rules for this assignment I have to do!
    It is really unfortunate that so many classes teaching JavaScript use problems to test particular JavaScript commands that are best solved in JavaScript using completely different commands rather than coming up with problems that actually need the code being taught. (worst are the ones that insist on using long obsolete commands that shouldn't be taught any more - at least that doesn't apply in this case).

    Not sure if this is the way to fix it but the following makes the most sense for fixing the code so it actually does something (in addition to fixing the loop to update rather than overwrite the prior messages)

    Replace

    Code:
    var randomNum = Math.floor((Math.random() * 10) + 1);
    with

    Code:
    voicemail = Math.floor((Math.random() * 10) + 1);
    With a few more minor modifications I actually got the code to do something - not sure if it is what's wanted though - JS Bin - Collaborative JavaScript Debugging
    Last edited by felgall; 11-06-2015 at 01:55 AM.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  11. #11
    New to the CF scene
    Join Date
    Nov 2015
    Location
    http://bytearcher.com
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Try moving this part two lines up so it's placed inside the for-loop.

    Quote Originally Posted by Leenangu View Post
    document.getElementById("answer").innerHTML +=msg;
    and changing this switch case to be evaluated against randomNum.

    Quote Originally Posted by Leenangu View Post
    switch (voicemail) {
    Then it'll work.


 

Tags for this Thread

Posting Permissions

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