If else

Posted by Stratus on Sun 14 Sep 2003 02:33 AM — 10 posts, 35,433 views.

#0
For some reason I'm trying out JScript, and every time I put an if.. else statement in the script Mushclient gives me an error saying: Syntax error
Line in error:
else

I mean, I even used this as the code to see if it was a mess up in my script.

function OnHello (name, output, wildcards) {
name = wildcards(1);
if(name = "Kyglus")
{
world.send("\"I love you, Kyglus.")
else
world.send("\"I don't think we've ever met, " + name + ".")
end if
} //end

Am I doing something wrong here?
USA #1
Either you use the bracket form of the if or you don't... it seems that there you're opening the if block, but then not closing it.

You should have either:

function OnHello (name, output, wildcards) {
 name = wildcards(1);
 if(name = "Kyglus")
  world.send("\"I love you, Kyglus.")
 else
  world.send("\"I don't think we've ever met, " + name + ".")
} //end

or

function OnHello (name, output, wildcards) {
 name = wildcards(1);
 if(name = "Kyglus")
 {
  world.send("\"I love you, Kyglus.")
 }
 else
 {
  world.send("\"I don't think we've ever met, " + name + ".")
 }
} //end



Modified to add the following:
You probably also want to terminate your statements with semi-colons, but it's been a while since I've done any J-Script so I don't remember. My bet is that you do, though.
Amended on Sun 14 Sep 2003 02:41 AM by David Haley
#2
It still gave me the same error message.
USA #3
Could you paste the exact code you are using this time please? Just to make sure I know exactly what you've got :)
#4
function OnHello (name, output, wildcards) {
name = wildcards(1);
if(name = "Kyglus");
world.send("\"I love you, Kyglus.");
else;
world.send("\"I don't think we've ever met, " + name + ".");
} //end
USA #5
Ah... you don't want those semi-colons after the if and the else.
#6
function OnHello (name, output, wildcards) {
name = world.gettriggerinfo(name, 101);
if(name = "Kyglus")
{
world.send("\"I love you, Kyglus.")
}
else
{
world.send("\"I don't think we've ever met, " + name + ".")
}
} //end

Worked. Apparently wildcards(1) doesn't work with JScript, is there anything like it that does?
USA #7
I don't know about the wildcards(1) thing, but that (the one with brackets) looks syntactically correct. You never want to put semi-colons after an if statement. It works like this:

if ( expression )
BLOCK
else
BLOCK

Where a block is either:

statement

or

{
list of statements
}
Amended on Sun 14 Sep 2003 03:14 AM by David Haley
Australia Forum Administrator #8
Quote:

if(name = "Kyglus")


Is this really right? JScript is similar to C, and in C you would write:


if(name == "Kyglus")


Quote:

Apparently wildcards(1) doesn't work with JScript, is there anything like it that does?


Take a look at the exampscript.js file that ships with MUSHclient. It is there to help. You need to use a special function to convert VB arrays to Jscript arrays:


wildcards = VBArray(wildcardsVB).toArray();	
name = wildcards [0];


#9
You're correct Nick.

if(name = "Kyglus")


will always be true, because its assigning "Kyglus" to name.

correct would be

if (name == "Kyglus")


better would be

if ("Kyglus" == name)


because then you will get an error immediately if you forget the second =