Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 129
  • Last Modified:

javascript nested if

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
Allen Pitts
Asked:
Allen Pitts
  • 4
  • 2
1 Solution
 
GaryCommented:
<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
 
Allen PittsBusiness analystAuthor Commented:
Hello Cathal,

That works but it gets rid of the nested if

Thanks

Allen
0
 
GaryCommented:
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
Industry Leaders: 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!

 
GaryCommented:
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
 
Allen PittsBusiness analystAuthor Commented:
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
 
GaryCommented:
You got it!
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now