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 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Nov 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    function and Math.random

    It's saying "missing operand; found else" on line 8.
    Why is it written? And how can I fix it?

    var player = prompt("Choose a number between 1-5:")
    var computer = function(number)
    {
    var comNum = Math.random(1, 5);
    if (comNum>player){
    console.log("Computer wins!");
    };
    else if (player>comNum) //line 8
    {
    console.log("Player wins!");
    }
    else if(player===comNum){
    console.log("TIE!");
    }
    };

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,894
    Thanks
    202
    Thanked 2,528 Times in 2,506 Posts
    You do not seem to have taken on board the advice you were given in your other post

    Using Math.random and "if, else if, else"

    var comNum = Math.random(1, 5);
    is not the way to generate a random number between 1 and 5.


    Quizmaster: What season is said to start on the longest day in December?
    Contestant: Spring.
    Quizmaster: December, for God's sake!
    Contestant: Summer.

    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.

  • #3
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    'Found else' suggests something wrong preceding:
    Code:
    if (comNum>player){
    console.log("Computer wins!");
    };
    else if (player>comNum) //line 8
    {
    console.log("Player wins!");
    }

  • #4
    New to the CF scene
    Join Date
    Nov 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Philip M View Post
    You do not seem to have taken on board the advice you were given in your other post

    Using Math.random and "if, else if, else"

    var comNum = Math.random(1, 5);
    is not the way to generate a random number between 1 and 5.


    Quizmaster: What season is said to start on the longest day in December?
    Contestant: Spring.
    Quizmaster: December, for God's sake!
    Contestant: Summer.
    Then what is? And how do I fix my program? I don't understand how the previous thread helps me...

    Quote Originally Posted by Logic Ali View Post
    'Found else' suggests something wrong preceding:
    So what should I do?

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,894
    Thanks
    202
    Thanked 2,528 Times in 2,506 Posts
    Quote Originally Posted by Collinar View Post
    Then what is? And how do I fix my program? I don't understand how the previous thread helps me...


    So what should I do?
    I showed you how to generate a random number in your previous thread Post #3.

    Logic Ali has shown you what is wrong with your program. To fix it - simply delete the incorrect semi-colon.
    After that you should make an effort to study Javascript.

    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.


  •  

    Posting Permissions

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