Geoff Cox wrote on 24 mrt 2008 in comp.lang.javascript
:
Hello,
I am not getting the logic right here...
I want to send to a php file the results of clicking some buttons but
only when an email address has been added, so
function checkBeforeSend(){
if (not all questions answered) {
alert("complete all questions");
} else if (no email added) {
alert ("add email address");
} else {
send();
}
}
something wrong here - ideas please!
Logically nothing is wrong.
The {} are superfluous, or there are two missing.
The last else "belongs to the last if, btw.
Javascript has no elssif like vbscript,
so write these keywords on seperate lines.
Good logical indenting will show you:
function checkBeforeSend(){
if (! allQuestionsAnswered)
alert("complete all questions")
else
if (! emailAdded)
alert ("add email address")
else
send();
};
But if you want complete {} do it this way:
function checkBeforeSend(){
if (! allQuestionsAnswered) {
alert("complete all questions");
}
else { // this { you missed
if (! emailAdded) {
alert ("add email address");
}
else {
send();
};
}; // and this corresponding closing }
};
Another way is by exchangeing the else clauses for early return clauses,
this is perhaps easier to do and to maintain:
function checkBeforeSend(){
if (! allQuestionsAnswered) {
alert("complete all questions");
return;
};
if (! emailAdded) {
alert ("add email address");
return;
};
send();
};
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)