View Single Post
Old 04-19-2004, 07:03 PM   PM User | #15
neil9999
New Coder

 
Join Date: Oct 2003
Location: UK
Posts: 37
Thanks: 0
Thanked 0 Times in 0 Posts
neil9999 is an unknown quantity at this point
Thumbs up Q: How do I shorten my lengthly if() statements?

A: There are a number of 'tricks' you can use to do this.

1:
Take this code:

PHP Code:
<script type="text/javascript">
var 
a=prompt("Enter number","");
var 
b=prompt("Enter number","");
if(
a==b){
alert(true);
}
else{
alert(false);
}
</script> 
As if() statements always return either true or false, you can just do this:

PHP Code:
<script type="text/javascript">
var 
a=prompt("Enter number 2","");
var 
b=prompt("Enter number 2","");
alert(a==b);
</script> 
This is alerting the result of the test, which is (a==b).
2:
PHP Code:
<script type="text/javascript">
var 
a=prompt("Enter number 3","");
var 
b=prompt("Enter number 3","");
if(!
a||!b){
alert("Enter a number")
}
else if(
a==b){
alert("Equal");
}
else{
alert("Not equal");
}
</script> 
As you can see, this tests whether two inputted numbers are equal, alerting a different message depending on:

A) If they don't enter two numbers

B) If the numbers are equal

C) If the numbers are not equal.

As in each if(), else if(), else statement, we are doing the same thing (alerting) but alerting something different we can use this:

PHP Code:
<script type="text/javascript">
var 
a=prompt("Enter number 4","");
var 
b=prompt("Enter number 4","");
[
B]alert((!a||!b)?"Enter a number":(a==b)?"Equal":"Not equal");[/B]
</script> 
Note the line in bold. This is how we write it:

First we start with alert(. We then put our first condition (inside the first if()) in brackets. So:

alert((!a||!b)

We then put a question mark after, to show it's an if() statement. We then put the text to alert if this condition is true, so:

alert((!a||!b)?"Enter a number"

Instead of else, we put a colon : . We then repeat what we have already done. So:

alert((!a||!b)?"Enter a number":(a==b)?"Equal"

The bit in bold is the condition for the else if() statement. The bit after is the resulting alert. If there are more else if() statements, we put a colon, and repeat. When we get to the last else, we just put a colon and the text to alert if the previous if() and else if()s were all false. In this case, we put a bracket to close the alert() function. We end up with:

alert((!a||!b)?"Enter a number":(a==b)?"Equal":"Not equal");
3:
In if() statements or similar, we never need to use the following:

if(a==true)
if(b==false)

Instead of the first, we just need the a. So:

if(a)

Instead of the second, we put an ! in front of the variable. So:

if(!b)
4:
Take this:

PHP Code:
<script type="text/javascript"
function 
disdate(){ 
var 
d=new Date(); 
var 
day=d.getDate(); 

if(
d.getMonth()==0){
var 
months="Jan"
}
else if(
d.getMonth()==1){
var 
months="Feb"
}
else if(
d.getMonth()==2){
var 
months="Mar"
}
//repeat right up until:
else if(d.getMonth()==11){
var 
months="Dec"
}
document.getElementById("thedate").value=months;

</script> 
As we are testing a variable against numbers 0-11, we can use an array to store all our months:

PHP Code:
<script type="text/javascript"
function 
disdate(){ 
var 
d=new Date(); 
var 
day=d2.getDate(); 
var 
months=["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"]; 
document.getElementById("thedate").value=months[d.getMonth()]

</script> 
All the months are stored in an array named months. We refer to each item like this:

months[0] for the first

months[1] for the second

months[2] for the third etc.

Instead of the number, we use d.getMonth() inside the square brackets, as this returns the number of the item we want.
neil9999 is offline