newbie - Please help me. Where can I find this?

  • Thread starter everhoping optimist
  • Start date
E

everhoping optimist

If I write this as my script -

<script language = "JavaScript">
<!--
var passWord;
prompt ( "What is the password?","");
(passWord == "pass" ); ? alert("Verified"): alert("Incorrect
password");
//-->

</script>

The learning material that I have asks me to enter a ? character.

Why is this?? I cannot find any reference to its meaning

Many Thanks in Advance
 
E

everhoping optimist

Sorry, folks!!

It has just occured to me that this strange ? mark actually belongs to
a condtional operator. (sorry for the dumb question. It did say that
I'm new, didn't I!!)
 
B

Bobbo

everhoping said:
If I write this as my script -

<script language = "JavaScript">
<!--
var passWord;
prompt ( "What is the password?","");
(passWord == "pass" ); ? alert("Verified"): alert("Incorrect
password");
//-->

</script>

The learning material that I have asks me to enter a ? character.

Why is this?? I cannot find any reference to its meaning

The ? : syntax is called the ternary operator and is similar logically
to an if ... else construct, except it's much handier. The various
parts of the syntax are:

<condition> ? <true-case> : <false-case>

So, if the <condition> evaluates to true then the <true-case> is
returned, otherwise the <false-case> is returned.

Moving on to your specific question, the code above should probably
look something like this (assuming you need to store the password for
later):

var passWord = prompt("What's the password?");
alert( (passWord=="monkey") ? "Correct" : "Wrong");

The first line declares a variable and sets it to the result of the
prompt() call. Hence it will store the password that the user has
typed.

The second line displays an alert box, with text depending on the
password that the user entered. If the user entered 'monkey' as a
password then it will display 'correct', otherwise it will display
'wrong'. Remember this is case sensitive so 'monkey' and 'Monkey' are
different.

I'd suggest playing around with this construct using alert boxes until
you're happy with the way in which it works.

Hope this helps.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top