Solved

Javascript if statement in switch statement

Posted on 2013-12-19
6
345 Views
Last Modified: 2013-12-19
Good morning expert,

Building app that should have an if statement in a switch statement.
Not having much luck so tried paring down to the bare bones.

This works

 var alpha = true;
    if (alpha === true)
    {document.write("Alpha is true")
    }
But when I put it in a switch statement, as copied below
it breaks.

Thanks

Allen in Dallas

<!DOCTYPE html>
<html>
<HEAD>
<title>JS Conditional w/ Switch Statement</title>

<style type="text/css">
 body {font-family: arial;}
 </style>
</HEAD>

<body>

This JavaScript uses an If statement in a switch statement<br>
<br><br>
<script>

 var alpha = true;

switch (beta)
  {
  case 0:
    if (alpha == true)
    {document.write("Alpha is true")}
    break;
  case 1:
    if (alpha != true)
    {document.write("Alpha is not true")}
    break;
}
</script>

</body>
</html>

Open in new window

0
Comment
Question by:9apit
6 Comments
 
LVL 2

Accepted Solution

by:
ITTallahassee earned 500 total points
ID: 39729216
It is valid to put an if statement inside of switch, but to start you haven't declared beta or what beta is (switch variable).  And since you don't have a "Default" case and "Beta" isn't declared nothing is happening. Did you mean to leave your script outside of the <head>?
0
 
LVL 7

Expert Comment

by:dimmergeek
ID: 39729230
I'm not sure what you mean by "it breaks".
If you mean it is not running, that is because you are switching based on the value of beta, but never setting beta.
If you set beta to 0 or 1 all works well.
0
 
LVL 7

Expert Comment

by:dimmergeek
ID: 39729235
Sorry, ITTallahassee, I did not refresh my screen before submitting my comment.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 5

Expert Comment

by:Iphonehelper
ID: 39729278
hello,
i have no idea about this but i am learning java and i hope i can help
try this,

maybe it is not working because you did not declare beta.

<!DOCTYPE html>
<html>
<HEAD>
<title>JS Conditional w/ Switch Statement</title>

<style type="text/css">
 body {font-family: arial;}
 </style>
</HEAD>

<body>

This JavaScript uses an If statement in a switch statement<br>
<br><br>
<script>

 var alpha = true;

switch (alpha)
  {
  case true:
    if (alpha == true)
    {document.write("Alpha is true")}
    break;
  case false:
    if (alpha != true)
    {document.write("Alpha is not true")}
    break;
}
</script>

</body>
</html>
0
 
LVL 5

Expert Comment

by:Iphonehelper
ID: 39729284
oh sorry guys i also didnt refresh my page
0
 

Author Closing Comment

by:9apit
ID: 39729307
Excellent explanation
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now