Solved

Javascript if statement in switch statement

Posted on 2013-12-19
6
341 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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
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…

762 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

13 Experts available now in Live!

Get 1:1 Help Now