Solved

Javascript if statement in switch statement

Posted on 2013-12-19
6
351 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

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-…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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…

820 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