The Web Design Group

... Making the Web accessible to all.

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> Unordered list in Ascending order.
s2clue
post Sep 7 2007, 02:30 PM
Post #1





Group: Members
Posts: 6
Joined: 7-September 07
Member No.: 3,746



How do I sort the output from the user into an unordered list that is sorted in ascending order?

CODE
<html>
<head>
<title>Input Output</title>

<script type="text/javascript">

var n1;
var n2;
var n3;

n1 = window.prompt( "Please enter the 1st integer: ", "0" );
n2 = window.prompt( "Please enter the 2nd integer: ", "0" );
n3 = window.prompt( "Please enter the 3rd integer: ", "0" );

document.write("You have entered <ol>" + n1 + ", " + n2 + ", " + n3 + "</ol>");

</script>

</body>

</html>
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
pandy
post Sep 7 2007, 03:45 PM
Post #2


🌟Computer says no🌟
********

Group: WDG Moderators
Posts: 20,731
Joined: 9-August 06
Member No.: 6



You use UL instead of OL and put each answer inside a LI.
http://htmlhelp.com/reference/html40/lists/li.html
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
s2clue
post Sep 7 2007, 03:50 PM
Post #3





Group: Members
Posts: 6
Joined: 7-September 07
Member No.: 3,746



QUOTE(pandy @ Sep 7 2007, 03:45 PM) *

You use UL instead of OL and put each answer inside a LI.
http://htmlhelp.com/reference/html40/lists/li.html


CODE
<html>
<head>
<title>Input Output</title>
</head>

<body>

<script type="text/javascript">

var n1;
var n2;
var n3;

n1 = window.prompt( "Please enter the 1st integer: ", "0" );
n2 = window.prompt( "Please enter the 2nd integer: ", "0" );
n3 = window.prompt( "Please enter the 3rd integer: ", "0" );

document.write("You have entered <ul><li>" + n1 + "</li> <li>" + n2 + "</li> <li>" + n3 + "</li> </ul>");

</script>

</body>

</html>


Like that? The output is showing up with bullets now, however it is not sorting them into order.

This post has been edited by s2clue: Sep 7 2007, 03:51 PM
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Darin McGrew
post Sep 7 2007, 04:30 PM
Post #4


WDG Member
********

Group: Root Admin
Posts: 8,365
Joined: 4-August 06
From: Mountain View, CA
Member No.: 3



Are you allowed to use the sort() method?

If not, then I suppose you could implement a simple bubble sort. If you're always going to have 3 inputs, then you could even write out 3 if-then clauses and not bother looping.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
s2clue
post Sep 7 2007, 04:40 PM
Post #5





Group: Members
Posts: 6
Joined: 7-September 07
Member No.: 3,746



Yes I will always have only 3 inputs. I could use sort() probably, however I am not familiar with it. I'm just getting started in my javascript class, and fumbling already smile.gif

This post has been edited by s2clue: Sep 7 2007, 04:41 PM
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
s2clue
post Sep 7 2007, 05:01 PM
Post #6





Group: Members
Posts: 6
Joined: 7-September 07
Member No.: 3,746



QUOTE(s2clue @ Sep 7 2007, 04:40 PM) *

Yes I will always have only 3 inputs. I could use sort() probably, however I am not familiar with it. I'm just getting started in my javascript class, and fumbling already smile.gif

Ok, I went with the sort() method, thanks. I got something sorted now. Just need to try to get it into a bulleted list.
CODE
<html>
<head>
<title>Input Output</title>
</head>

<body>

<script type="text/javascript">

var n1;
var n2;
var n3;

n1 = window.prompt( "Please enter the 1st integer: ", "0" );
n2 = window.prompt( "Please enter the 2nd integer: ", "0" );
n3 = window.prompt( "Please enter the 3rd integer: ", "0" );

function sortNumber(a,b)
{
return a - b
}var arr = new Array(3)
arr[0] = n1
arr[1] = n2
arr[2] = n3

document.write(arr + "<br />")
document.write(arr.sort(sortNumber))


</script>

</body>

</html>
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Darin McGrew
post Sep 7 2007, 05:31 PM
Post #7


WDG Member
********

Group: Root Admin
Posts: 8,365
Joined: 4-August 06
From: Mountain View, CA
Member No.: 3



Have you learned about for loops yet? That's the clean way to do it.

But if not, you can just print out arr[0], arr[1], and arr[2] the way you were printing out n1, n2, and n3.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
s2clue
post Sep 7 2007, 05:54 PM
Post #8





Group: Members
Posts: 6
Joined: 7-September 07
Member No.: 3,746



QUOTE(Darin McGrew @ Sep 7 2007, 05:31 PM) *

Have you learned about for loops yet? That's the clean way to do it.

But if not, you can just print out arr[0], arr[1], and arr[2] the way you were printing out n1, n2, and n3.

Yes, we have touched on loops.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Brian Chandler
post Sep 7 2007, 10:09 PM
Post #9


Jocular coder
********

Group: Members
Posts: 2,460
Joined: 31-August 06
Member No.: 43



QUOTE
How do I sort the output from the user into an unordered list that is sorted in ascending order?


You can't. Because "unordered" means not sorted, so the moment you sort it, it is no longer unordered. However, the HTML spec abuses "unordered", to mean "unnumbered"; perhaps that's what you mean?



User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
s2clue
post Sep 7 2007, 10:37 PM
Post #10





Group: Members
Posts: 6
Joined: 7-September 07
Member No.: 3,746



QUOTE(Brian Chandler @ Sep 7 2007, 10:09 PM) *

QUOTE
How do I sort the output from the user into an unordered list that is sorted in ascending order?


You can't. Because "unordered" means not sorted, so the moment you sort it, it is no longer unordered. However, the HTML spec abuses "unordered", to mean "unnumbered"; perhaps that's what you mean?

exactly, my fault.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post

Reply to this topicStart new topic
2 User(s) are reading this topic (2 Guests and 0 Anonymous Users)
0 Members:

 



- Lo-Fi Version Time is now: 26th April 2024 - 08:11 AM