Click to See Complete Forum and Search --> : can you spot the mistake?


nicoletta
01-27-2003, 09:13 AM
here we are again, i have almost completed the script but it is not working properly. what it does it : the game produced randoms numbers, the user imput numbers and th egame has to compare them and tell the user that 1) if the user imput the same number there is an alert saying you won and if the numbers are different there is an alert saying try again
I have written the program so that you can actually see what numbers the computer generates to make it easier for testing but i don't understand why it doesn't work properly
can you help
this is the script
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<H3>MasterMind</H3>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
<script language="Javascript">
<!--
function SymError ()
{
return true;
}
window.onerror = SymError;

var gameNumber = new Array ()

var size = prompt (" how many number do you want to play with?","")
document.writeln ("you want to play with " + size +" numbers");
for (i=0 ; i<size ; i++)
{
gameNumber [i] = Math.floor(Math.random () *6 )+1;
}
document.writeln ( "<br>these are the game numbers " + gameNumber + "</br>");


var userInput ;

for (j = 1; j <=size;j ++)
{
var userInput = window.prompt ("please enter a digit from 1 to 6","");

{
document.writeln ("guess "+ userInput);
}
}
var n = 0
for (n=0; n<size; n++)
{
if (userInput[n] == gameNumber[n])

window.alert("Congratulations");
else {
window.alert("Better luck next time");
}
}
-- >

</script>
</HEAD>

<BODY>

</BODY>
</HTML>

gil davis
01-27-2003, 09:31 AM
You do realize that the prompt() method returns a string, and the Math.floor returns an integer, don't you?

That means that they will never be equal. They aren't the same type of data.

Try this:

userInput = Number(window.prompt ("please enter a digit from 1 to 6",""));

nicoletta
01-27-2003, 11:01 AM
dear gill
thank you for your reply
I know that the prompt () return a string and the math return an integer. What i'm trying to do it is a game where the computer generates randoms numbers and the user needs to guess them, take for example Master Mind but with numbers
and all i want to do is to ask the user to insert the numbers and tell th euser that he/she got the right numbers or he/she lost inputting the wrong numbers....

Ribeyed
01-27-2003, 11:37 AM
hi,
ok ran this code see your problem, i'll post this code i used to find it, run it you will see that you are writing all your numbers to all rows in your array.
say you chose 3 numbers, it generates these numbers:
2,3,5

when i write out to screen what is in each cell in your array i get this
2,3,5
2,3,5
2,3,5

this is your problem

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<H3>MasterMind</H3>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
<script language="Javascript">

function SymError ()
{
return true;
}
window.onerror = SymError;

var gameNumber = new Array ()

var size = prompt (" how many number do you want to play with?","")
document.writeln ("you want to play with " + size +" numbers");
for (i=0 ; i<size ; i++)
{
gameNumber [i] = Math.floor(Math.random () *6 )+1;
}
document.writeln ( "<br>these are the game numbers " + gameNumber + "</br>");


var userInput ;

for (j = 1; j <=size;j ++)
{
var userInput = window.prompt ("please enter a digit from 1 to 6","");

{
document.writeln ("guess "+ userInput);
}
}
var n = 0
for (n=0; n<size; n++)
{
document.writeln ("User Input was:"+userInput+" Answer was:"+gameNumber+"<BR>")
if (userInput[n] == gameNumber[n])

window.alert("Congratulations");
else {
window.alert("Better luck next time");
}
}


</script>
</HEAD>

<BODY>

</BODY>
</HTML>

So your end results are correct. none of them match.

Ribeyed
01-27-2003, 12:31 PM
Hi sorry, that last post is not correct, plz forgive me javascript not that **** hot. tried this again but with this line

document.writeln ("User Input was:"+userInput+" Answer was:"+gameNumber[n]+"<BR>")
the gamenumber array is fine. Its the userInput that is not.

Beacuse you write the values to the userInput variable during a for loop at the top when your loop the second time the first value is overwriten. When you come to use the variable further down in your code you call the variable userInput which will be the last value asigned to it during the first loop, then you are using the same number but looping it and eveluating it equal to the answer.
So say i chose 3 numbers
it randoms 4,5,7
i type in as my answer 3,5,8
on these 2 lines
var userInput = window.prompt ("please enter a digit from 1 to 6","");
document.writeln ("guess "+ userInput);
you take the number for the user assign it to the userInput variable. So the first time 3 is assigned.
next pass of the loop userInput is set to 5
thrid time its set to 8
When you are checking the numbers against your numbers in the array. you are saying
8 = 4
8 = 5
8 = 7
because by the time it gets to the last loop userInput is 8. It will be 8 every time you loop.
hopeful this helps you and not hinders you:)

nicoletta
01-27-2003, 01:53 PM
thank you for your help, but still it doesn't work...nikki

Ribeyed
01-27-2003, 02:06 PM
got it, phew! Im an ASP programer and only use small amounts of javascript this was hard for my, but it works so here it is:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<H3>MasterMind</H3>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
<script language="Javascript">

function SymError ()
{
return true;
}
window.onerror = SymError;

var gameNumber = new Array ()

var size = prompt (" how many number do you want to play with?","")
document.writeln ("you want to play with " + size +" numbers");
for (i=0 ; i<size ; i++)
{
gameNumber [i] = Math.floor(Math.random () *6 )+1;
}
document.writeln ( "<br>these are the game numbers " + gameNumber + "</br>");

var userInput = new Array ()
for (j = 0; j <size;j ++)
{

userInput[j] = window.prompt('please enter a digit from 1 to 6','number');

{
document.writeln (userInput[j]);
}
}

for (j=0; j<size; j++)
{
document.writeln ("User Input was:"+userInput[j]+" Answer was:"+gameNumber[j]+"<BR>")
if (userInput[j] == gameNumber[j])

window.alert("Congratulations");
else {
window.alert("Better luck next time");
}
}


</script>
</HEAD>

<BODY>

</BODY>
</HTML>

nicoletta
01-27-2003, 03:07 PM
thank you thank you thank you and again i dunno how to thank you

Ribeyed
01-27-2003, 03:32 PM
np;)