Newbie learning JavaScript - need help with a question please

S

Sean

I have been given a task of having a user enter 3 numbers then
displaying them in ascending order using nested if...else statements
as part of my course. I beleive I am close but am having issues with
the last two if statement not running.

Can someone please tell me if I am going about this in the right way?

Have I got all the possibilities that three numbers can appear
correct?

If you can tell me what I am doing wrong and point me in the right
direction that would be appreciated.

Regards

Sean



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Strict//EN"
"http://www.w3.org/TR/html4/strict.dtd">

<html>
<head>
<title>Enter Title Here</title>
<META NAME="Generator" CONTENT="Stone's WebWriter 3.5">
<meta http-equiv="content-type" content="text/html;
charset=iso-8859-1">
<script type="text/javascript">
<!-- hide from incompatible browsers

var num1, num2, num3;

num1 = prompt("Please enter the first number", "");
num2 = prompt("Please enter the second number", "");
num3 = prompt("Please enter the third number", "");

if (num1 < num2)
if (num2 < num3)
document.write("possibilty one: " + num1 + num2 + num3);
else
{
if (num1 < num2)
if (num2 > num3)
document.write("possibilty two: " + num1 + num3 +
num2);
else
{
if (num1 < num2)
if (num2 < num3)
document.write("possibilty three: " + num1 +
num2 + num3);
else //1
{
if (num1 < num2)
if (num2 > num3)
document.write("possibilty four: " +
num1 + num2 + num3);
else
{
}
}
}

}


// Stop hiding from incompatible browsers -->
</script>
</head>

<body>

</body>

</html>
 
H

humbads

Please don't ever do sorting using if-else statements in real life.
Here's a much faster way, although it lacks input validation.

var numberArray = new Array();
numberArray[0] = prompt("Please enter the first number", "");
numberArray[1] = prompt("Please enter the second number", "");
numberArray[2] = prompt("Please enter the third number", "");
alert('I sorted the numbers! ' + numberArray.sort().join(', '));

As for your original question, I would recommend working it out on
paper rather than asking someone to debug it for you, otherwise you
will never develop debugging skills. For three numbers, there are only
six combinations to work out (assuming distinct numbers are entered).

Also, if you are going to use bracketing, use it consistently for both
the if and the else blocks.

if () {
// do this
} else {
// do that
}
 
S

Sean

Thanks for your help.

We haven't started to learn about Arrays yet but i will keep them in
mind. We HAVE to use if...else statements because that is what we are
learning at the moment and they want us to get the practise even if
it's not the best method. Onlt been doing the course for a few weeks
so far.

As you mentioned the two main problems I have are working out the six
possible combinations on paper.

Writing down the logic on paper first.

I am finding myself just getting more and more confused at the moment.

And it's been 25 years since I was last at school so it's a bit of a
hard slog. If you could help me further I would appreciate it.

Regards

Sean
 
C

Christopher Benson-Manica

Sean said:
If you can tell me what I am doing wrong and point me in the right
direction that would be appreciated.

Well, to start with, it would be helpful if you posted well-formatted
code. Another thing would be to make a habit of using braces with all
if-else constructs - it makes what you are doing explicitly clear.

Incidentally, you're clearly missing two cases - there are six
possible combinations of three numbers, but you only print four. What
you want is something like

if( num1 < num2 ) {
if( num2 < num3 ) {
// num1, num2, num3
}
else if( num1 < num3 ) {
// num1, num3, num2
}
else {
// num3, num1, num2
}
}
else if( num2 > num3 ) {
// num3, num2, num1
}
else if( num1 > num3 ) {
// num2, num3, num1
}
else {
// num2, num1, num3
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top