Solved

javascript nested if

Posted on 2014-01-31
6
124 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

776 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