Solved

javascript nested if

Posted on 2014-01-31
6
126 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…
Suggested Courses

738 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