Solved

javascript nested if

Posted on 2014-01-31
6
122 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

When you need to keep track of a simple list of numbers or strings, the Array object is your most direct tool.  As we saw in my earlier EE Article (http://www.experts-exchange.com/A_3488.html), typical array handling might look like this: (CODE) B…
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…

708 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

17 Experts available now in Live!

Get 1:1 Help Now