...

View Full Version : function and Math.random



Collinar
11-10-2012, 04:39 PM
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!");
}
};

Philip M
11-10-2012, 06:16 PM
You do not seem to have taken on board the advice you were given in your other post

http://www.codingforums.com/showthread.php?t=281817

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.

Logic Ali
11-10-2012, 07:20 PM
'Found else' suggests something wrong preceding:


if (comNum>player){
console.log("Computer wins!");
};
else if (player>comNum) //line 8
{
console.log("Player wins!");
}

Collinar
11-10-2012, 08:21 PM
You do not seem to have taken on board the advice you were given in your other post

http://www.codingforums.com/showthread.php?t=281817

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...


'Found else' suggests something wrong preceding:
So what should I do?

Philip M
11-10-2012, 09:11 PM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum