PDA

View Full Version : Randomize chosen value



HeyEverybody
02-01-2011, 08:23 PM
Hi,

I got following script + HTML:

<script type="text/javascript">

function tjek() {
if ((document.forms[0].spil.value=='HIDDENWORD')) {
alert('Correct message');
window.location.reload( true );
}
else if ((document.forms[0].spil)) {
alert('Wrong message');
}
}

function tastkontrol() {
document.forms[0].spil.value = document.forms[0].spil.value.toUpperCase();
}

</script>

<form method="post" onsubmit="tjek();return false;">
<input type="text" name="spil" onkeyup="tastkontrol();return false;">
<input type="submit" value="Send">
</form>
It's a game, were the person has to guess the hidden word.

I know want to add this to the script, so the HIDDENWORD changes into either APPLE or HOUSE when refreshing page:

<script type="text/javascript">

code=new Array;

code[1]="APPLE";
code[2]="HOUSE";

function Show() {

num = Math.random();
num = 1 + ( num * ((code.length - 1)-1));
num = Math.round(num);

document.write("" + code[num] + "");
}

Show();

</script>
I've tried some stuff, but I don't got a clue, how to merge these to scripts into eachother.

Thanks in Advance.

Old Pedant
02-01-2011, 09:10 PM
Your random number code is wrong. Way, way off.

It should be:

var num = Math.floor( code.length * Math.random() );

coothead
02-01-2011, 09:12 PM
Hi there HeyEverybody,

does this help...


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="language" content="english">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">

<title></title>

<script type="text/javascript">

var code,num,df;

function init(){

code=['APPLE','HOUSE','HIDDENWORD'];
num=Math.floor(Math.random()*code.length);

df=document.forms[0];

df[0].focus();

df.onsubmit=function(){
return tjek();
}
df[0].onkeyup=function() {
tastkontrol();
}
}

function tastkontrol() {
df[0].value=df[0].value.toUpperCase();
}

function tjek() {
if(df[0].value==code[num]) {
alert('Correct message');
window.location.reload();
}
else {
alert('Wrong message');
}
}

window.addEventListener?
window.addEventListener('load',init,false):
window.attachEvent('onload',init);

</script>

</head>
<body>

<form action="#" method="post">
<div>
<input type="text" name="spil">
<input type="submit" value="Send">
</div>
</form>

</body>
</html>

coothead

HeyEverybody
02-01-2011, 09:42 PM
Old Pedant; I'll remember that.

coothead; WOW! Thank you very much! :D

coothead
02-01-2011, 09:49 PM
No problem, you're very welcome. ;)