Allen Pitts
asked on
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++++++++++++
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++++++++++++
ASKER
Hello Cathal,
That works but it gets rid of the nested if
Thanks
Allen
That works but it gets rid of the nested if
Thanks
Allen
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>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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"
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"
You got it!
Open in new window