Solved

javascript nested if

Posted on 2014-01-31
6
123 Views
Last Modified: 2014-01-31
Hello expert,

Writing a nested if statement. Made up example
to investigate.

In code copied below the code works as long as all three variables
with integer values are the same, as written.

But when one of the integer values is change to
a number different from the other two, say 2,
it was expected for the result would be
"they are not all equal". But instead
it returns "undefined"

Why does it not return "they are not all equal"?

Thanks.

Allen in Dallas

+++++++++begin code++++++++++++

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

<style type="text/css">
 body {font-family: arial;}
 </style>
</HEAD>
<body>
This page uses a nested if.<br /><br />

<script>
var a=1;
var b=1;
var c=1;
var answer
if (a == b){
 if (a == c){
answer = ("they are equal 3");
}
else
{
("they are not all equal");
}
}
document.write(answer);
</script>


</body>
</html>

+++++++++end code++++++++++++
0
Comment
Question by:9apit
  • 4
  • 2
6 Comments
 
LVL 58

Expert Comment

by:Gary
ID: 39824697
<script>
var a=1;
var b=1;
var c=1;
var answer
if (a == b && b == c){
answer = ("they are equal 3");
}
else
{
answer = ("they are not all equal");
}
document.write(answer);
</script>

Open in new window

0
 

Author Comment

by:9apit
ID: 39824718
Hello Cathal,

That works but it gets rid of the nested if

Thanks

Allen
0
 
LVL 58

Expert Comment

by:Gary
ID: 39824735
Ok misunderstood what you were wanting, nested example

<script>
var a=1;
var b=2;
var c=1;
var answer
if (a == b){
    if (a == c){
        answer = ("they are equal 3");
    }
    else
    {
        answer = ("they are not all equal");
    }
}
else
{
         answer = ("they are not all equal");
}
document.write(answer);
</script>

Open in new window

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 58

Accepted Solution

by:
Gary earned 500 total points
ID: 39824740
But that would not be a real world example of logic, this is still nested but is a better example

<script>
var a=1;
var b=1;
var c=1;
var answer = ("they are not all equal");
if (a == b){
    if (a == c){
        answer = ("they are equal 3");
    }
}
document.write(answer);
</script>

Open in new window

0
 

Author Closing Comment

by:9apit
ID: 39824927
Excellent answer.
I think that the reason why the code I copied into the original question
returned "undefined" was because changing the numbers where they
were not all equal created an outcome that was not provided for by the script.
Since the outcome was not provided for by the script the script did
not know what to do so it returned "undefined"
0
 
LVL 58

Expert Comment

by:Gary
ID: 39824937
You got it!
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

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
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…

863 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

25 Experts available now in Live!

Get 1:1 Help Now