Else if javascript

I have a simple function that should ask the user for the name of the company, and then a confirmation box that asks the user: "Do you want to order a web design package today?" The problem is with my "if" and "else if" statements. A warning message “Thank you for your order” should appear in the if statement when the user clicks OK; and the “else if” operator, which should display the “We value your time” warning window when the user clicks “Cancel”. The function is called by the onClick script function. Prompt and confirmation lines will appear, but I can’t understand why my if and else if the boxes are not displayed. 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> 
+4
source share
5 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 following is 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 want, for example:

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

And each if ifelel part is optional, except for the initial if block. Therefore, if you do not want to do anything if the condition not true, you can simply completely exclude the else block:

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

NTN

Having gone beyond your question for a moment, the expression evaluated in your if condition is a little wibbly-wobbly.

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

So your if statement should ask:

 if ( willOrderPackage == true ) { 

Even better, when you evaluate an expression in an if statement, it means == true at the end of it, which is invisible. For instance:

 if ( willOrderPackage == true ) { 

will be interpreted as:

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

The advantage of this is that you can actually omit the entire == true bit from your code so you can simply write:

 if ( willOrderPackage ) { 

And effectively you still say "if willOrderPackage is true"

Hope that helps you sort out a few things!

+14
source

your else if no condition, use else

as noted

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

false: willOrderPackage is boolean so it may be

 (willOrderPackage == true) 

or even better

 (willOrderPackage) 
+7
source

also better to use:

 if (willOrderPackage) { 

instead:

 if (willOrderPackage == "true") { 
+2
source

Change to:

 if (willOrderPackage) { alert("Thank you for your order.") }else{ alert("We appreciate your time.") } 
+1
source

Here's how to do it right:

 <script type="text/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) { alert("Thank you for your order."); }else{ alert("We appreciate your time."); } } </script> 

What I've done:

  • streamlined coding style
  • switches to type attribute instead of language
  • added semicolons on each ending line
  • redundant if keyword removed
  • added closing curly bracket that was initially missing.
  • comparison with true string is removed, comparison with boolean values ​​is not required.
0
source

Source: https://habr.com/ru/post/1432599/


All Articles