PDA

View Full Version : Somothing to do with variables


gildash2
05-22-2004, 09:51 PM
i know this is a stupid error but:
<script language="javascript">
function hey()
{
var h1="hey"
var h1="Hey";
var h1="HEY";
var i=0;
if(document.form1.message.value=h1 && i=0)
{
document.form1.answer.value="Hello";
i=i+1;
if(document.form1.message.value=h1 && i=1)
{
document.form1.answer.value="Hello...Again..";
i=i+1;
}
}
}
</script>
<form name="form1">
<input type="text" name="message">
<input type="text" name="answer" readonly="readonly">
<input type="button" value="talk" onClick="hey()">
</form>
what i want it to check is if i=0 or if i=1 when the function hey() responds to the "hey"
somethings wrong with it, i cant figure it out.
the code is pretty simple and should explain itself, but i cant find the problem

coothead
05-23-2004, 02:42 AM
Greetings gildash2,

So you have decided to descend from the philosophical and esoterical
explorations of the lounge to the more mundane matters of javascript.

We are greatly honoured by your presence and humbly offer these
observations for your consideration....

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Three Heys and some i=i+1</title>
<meta http-equiv="Content-Type" content="application/xhtml+xml; charset=utf-8" />

<script type="text/javascript">
//<![CDATA[

function hey() {

var h1="hey"
var h2="Hey";
var h3="HEY";
var i=0;

if(document.forms[0][0].value==h1 && i==0){
document.forms[0][1].value="Hello";
}
else{
i=i+1;
if(document.forms[0][0].value==h2 && i==1){
document.forms[0][1].value="Hello...Again..";
}
else{
i=i+1;
if(document.forms[0][0].value==h3 && i==2){
document.forms[0][1].value="There's no need to yell..";
}
}
}
}

//]]>
</script>
</head>
<body>
<form action="">
<div>
<input type="text"/>
<input type="text" readonly="readonly"/>
<input type="button" value="talk" onclick="hey()"/>
</div>
</form>

</body>
</html>

            @invasion@

agent002
05-23-2004, 08:13 AM
Hi Coothead,
actually, Gildash started out here in the Client Side Scripting forum... and found the lounge later... :D

edit: you probably want to check if the value is any of the three (hey, Hey or HEY)... maybe you should just make it lowercase.

edit2: you also need to make the i variable global...
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Three Heys and some i=i+1</title>
<meta http-equiv="Content-Type" content="application/xhtml+xml; charset=utf-8" />

<script type="text/javascript">
//<![CDATA[

var i=0;

function hey() {
var h = "hey";

if(document.forms[0][0].value.toLowerCase()==h && i==0){
i++;
document.forms[0][1].value="Hello";
}
else if(document.forms[0][0].value.toLowerCase()==h && i==1){
i++;
document.forms[0][1].value="Hello...Again..";
}
else{
document.forms[0][1].value="There's no need to yell..";
}
}

//]]>
</script>
</head>
<body>
<form action="">
<div>
<input type="text"/>
<input type="text" readonly="readonly"/>
<input type="button" value="talk" onclick="hey()"/>
</div>
</form>

</body>
</html>

coothead
05-23-2004, 08:24 AM
Hi there Jere,

I know... I thought of saying return instead of descend
but decided on descend as it sounded more appropriate :D

and he still insists on using....

<script language="javascript"> :supereek:

gildash2
05-23-2004, 08:44 AM
O O O some new stuff here.. im guessing the //<![CDATA[ makes the variable i global? and if so, what does that mean?
and the [0][0] im guessing is the first text box in the div and the [0] [1] is the second... cool stuff.. havnt seen this before but it makes sense i guess... but if it checks the text box and when it checks to see if when lowercase it is equal to the variable h(hey) wouldnt HEY apply as well?

agent002
05-23-2004, 08:53 AM
CDATA is something Coothead just put there to confuse you :D It has nothing with the JavaScript to do, don't mind... the variable is now global because it isn't defined inside the function, like you did. If it was, it would only exist inside the function and be redefined as zero every time the function is called.

document.forms[0][1] - the first 0 is the form object - the first (and only) form on the page - and the 1 is the second element in that form (because the first element is 0), use names instead if that is confusing.

document.forms[0][0].value.toLowerCase() - that is the value of the input field, converted to lowercase. So whatever I typed, hey, Hey, HEY, HeY or hEy, that value is hey. Then it compares it to the value of the variable h, which is hey, in lowercase.

gildash2
05-23-2004, 09:06 AM
so lets say if i wanted to check if the inputed text is equal to h to the uppercase i would do something like this?:
else if(document.forms[0][0].value==h.toUpperCase() && i==1)
??

agent002
05-23-2004, 09:09 AM
that is correct, but the value of the variable had to be HEY instead of hey then, otherwise it would never compare to true :)

Kram
05-23-2004, 07:55 PM
Just curious Gil, But is this code anything to do with your AI project?