On Feb 2, 2:45 pm, stev...@hotmail.com wrote:
> Thanks for that Daz - and to RobG for his response. I have used
> simple conditional operators before, but only for simple statements.
> Nesting them should prove fun...
>
> Am I right, though, that you cannot have multiple statements for the
> True and False bits (ie after the question mark)? I did try before
> grouping them together with {braces} separated by semi-colons but it
> didn't work. Does this method only work if you have a single action
> for each True/False?
I posted this before, and more than an hour later, it doesn't appear
to have gone through to the server, so I apologise if it shows up
twice.
Steve, you are correct in your statement. I didn't understand the
question earlier, and posted a wrong response. I also posted the
correct response shortly after, which again doesn't appear to have
made it to the server. I often misread things as I suffer from mild
reading difficulties, although as you can see, my typing is not
affected. Hehe.
Below is a working example of the nested shorthand 'if' statement.
Simply copy and paste it into a blank file, with an html extension,
open it in your browser of choice, and ensure JavaScript is enabled. I
have only tested it on Firefox 1.5.0.8, however.
<html>
<head>
<script type="text/javascript">
function testNumber(number)
{
if (isNaN(number))
{
alert("You did not enter a valid number");
return false;
}
var message = (number < 50)
? (number < 25)
? (number < 0)
? "less than zero"
: "between 0 and 25"
: "between 24 and 50"
: (number < 75)
? "between 49 and 75"
: (number < 100)
? "between 74 and 101"
: "more than 100";
alert(number +" is "+ message);
}
</script>
</head>
<body>
Please enter a number:<br />
<input id="input" type="text" size="3"
maxlength="3" />
<input type="submit" value="Test Number"
onclick="testNumber(document.getElementById('input ').value)" />
</body>
</html>
I have tried to make the code as readable as possible, although I am
not entirely sure if my method is the best method of acheiving this. I
am however, quite certain that using normal 'if' statements would take
up more space in the file and consume more bandwidth (which would most
likely be negligable), but normal 'if' statements would probably be
easier to read.
Basically, whatever follows the question marks (?), will be returned/
executed when the condition in the brackets in-line and above it
returns 'true'. Anything following a colon (

, will be returned/
executed when the corresponding condition returns 'false'.
As you can see, I have nested shorthand 'if' statements within
shorthand 'if' statements to demonstrate how it can be done. The code
doesn't really serve any practical purpose other than to illustrate
how shorthand 'if' statements can be nested.
To anyone who's reading this and can see bad coding practises, please
feel free to point them out to me, but bare in mind I am fairly new to
JavaScript, so my knowledge is limited. With that said, I am always
looking for ways to better my coding skills, so constructive
criticism, along with a suitable explanation is appreciated.
All the best.
Daz.