Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

else if javascript [closed]

I have a simple function that is supposed to prompt the user for the company name, followed by a confirmation message box that asks the user "Would you like to order a Web Design package today?" The problem is with my 'if' and 'else if' statements. The 'if' statement should show an alert box "Thank you for your order" when the user clicks 'OK'; and an 'else if' statement that should show an alert box "We appreciate your time" when the user clicks 'Cancel'. The function is called by an onClick script. The prompt and confirmation boxes appear but I can't figure out why my if and else if boxes do not appear. What am I missing? Any help is appreciated. Thanks.

<SCRIPT language = "Javascript">

function submitOrder() {

var companyName = prompt("Please enter company name.", "")

var willOrderPackage = confirm("Would you like to order a Web Design package today?")

if (willOrderPackage == "true") {
   alert("Thank you for your order.")

}else if{
   alert("We appreciate your time.")
}
</SCRIPT>
like image 948
Tracie Richardson Avatar asked Nov 28 '22 09:11

Tracie Richardson


2 Answers

There are actually two different if..else constructs. The first is a simple if-else:

if ( condition ) {
   // Do stuff if condition is true
} else {
   // Do stuff if condition is not true
}

The next is an if-else-if:

if ( condition ) {
   // Do stuff if condition is true
} else if ( differentCondition ) {
   // Do stuff if differentCondition is true
} else {
   // Do stuff if neither condition nor differentCondition is true
}

You can also use else-if as many times as you like, e.g.:

if ( condition ) {
   // Do stuff
} else if ( condition2 ) {
   // etc
} else if ( condition3 ) {
   // etc
} else if ( condition4 ) {
   // etc
} else {
   // etc
}

And every part of an if..else is optional, except for the initial if block. So if you don't want to do anything if condition is not true, you can just omit the else block entirely:

if ( condition ) {
   // do stuff if condition is true
}

HTH

Going beyond your question for a moment, the expression being evaluated in your if statement's condition is a bit wibbly-wobbly.

willOrderPackage will always be true or false, but it's important to note that true and "true" or false and "false" are different. The first is boolean, the second is a string.

So, your if statement should be asking:

if ( willOrderPackage == true ) {

Even better than that, when you evaluate an expression in an if statement, there's an implied == true at the end of it which is invisible. For instance:

if ( willOrderPackage == true ) {

would be interpreted as :

if ( (willOrderPackage == true) == true )

The benefit of this is that you can actually omit the whole == true bit from your code, so you can just write:

if ( willOrderPackage ) {

And effectively you're still saying "if willOrderPackage is true"

Hope that helps clear up a few things for you!

like image 146
jamesinc Avatar answered Dec 15 '22 18:12

jamesinc


your else if has no condition, use else

as stated

if (willOrderPackage == "true") {
alert("Thank you for your order.")

is wrong: willOrderPackage is a boolean so it can be

(willOrderPackage == true)

or even better

(willOrderPackage)
like image 32
mrmryb Avatar answered Dec 15 '22 18:12

mrmryb