3 Replies - 416 Views - Last Post: 17 July 2012 - 09:25 PM

#1 StephyRwar  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 15-May 12

Task: List numbers in order.

Posted 17 July 2012 - 06:42 PM

Hey guys,

I just have this task my dad gave to me for "Fun' Lol.
I have to create a programme that would list their three things

Their name, their email and their user score. This score would be from a game. We manually input the three parts of infomation.

I first thought i could use a bubble sort, but then i realised that only sorts an array of predetermined infomation. Does anyone know any other way of sorting numbers from highest to lowest as they get inputted? Is there a way?

Or am i going to have to do a loop that sorts the infomation after every time it is inputted?

Thanks
xx Steph

Is This A Good Question/Topic? 0
  • +

Replies To: Task: List numbers in order.

#2 JMRKER  Icon User is offline

  • D.I.C Addict

Reputation: 124
  • View blog
  • Posts: 816
  • Joined: 25-October 08

Re: Task: List numbers in order.

Posted 17 July 2012 - 08:44 PM

Since this is "Fun" program for your "Father",
see if you can explain this irregular solution to him
to show that you can understand what is going on.

I purposely did not comment any of the lines so see if
you can fill in the comments where appropriate to explain
what is occuring during execution of the script.

<html>
<head>
<title> Untitled </title>
<script type="text/javascript">
// From: http://www.dreamincode.net/forums/topic/286141-task-list-numbers-in-order/

var Recs = [];
//  Recs = ['a,z,100','b,y,70','c,x,80','d,v,60','e,u,90'];  // for testing purposes only

function enterInfo() {
  var tarr = [];
  tarr.push(document.getElementById('user').value);
  tarr.push(document.getElementById('email').value);
  tarr.push(document.getElementById('score').value);
  Recs.push(tarr.join(','));
  document.getElementById('results').innerHTML = Recs.join('<br>');
}
function sortBy(fld) {
  var tstr = [];
  var tarr = [];
  var tmp = [];
  for (var i=0; i<Recs.length; i++) {
    tstr = Recs[i].split(',');
    tarr.push(tstr[fld]+':'+i);   
  }
  tstr = tarr.sort();
  var str = '';
  for (var i=0; i<tstr.length; i++) {
    tmp = tstr[i].split(':');
    str += Recs[tmp[1]]+'<br>';
  }
  document.getElementById('results').innerHTML = str;
}
</script>
</head>
<body>
<input type="text" id="user" value=""> Name<br>
<input type="text" id="email" value=""> Email<br>
<input type="text" id="score" value=""> Score<br>
<button onclick="enterInfo()">Enter</button>
<br>Sort by: 
<label><input type="radio" name="srt" onclick="sortBy(0)"> User </label>
<label><input type="radio" name="srt" onclick="sortBy(1)"> Email </label>
<label><input type="radio" name="srt" onclick="sortBy(2)"> Score </label>
<p>
<div id="results"></div>
</body>
</html>



There are some "improvements" you could make for string sorts versus numerical sorts.
So as an additional challenge, see if you can figure out what and where these changes should be made. :bananaman:
Was This Post Helpful? 0
  • +
  • -

#3 StephyRwar  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 15-May 12

Re: Task: List numbers in order.

Posted 17 July 2012 - 09:06 PM

Hey, thankyou so much for replying. :P And i promise this is for my father -- he runs a website and has a fishing game, he wants a scoreboard for it so here it is. Sigh lol.

I understand how you got the feilds and such up, using the variables, but i guess my question is how do i sort them one by one by the highest score? The only way i can think of is bubble sort and that requires the variables in one place.... ._. incase you cant tell im new to javascripting lol.
Was This Post Helpful? 0
  • +
  • -

#4 StephyRwar  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 15-May 12

Re: Task: List numbers in order.

Posted 17 July 2012 - 09:25 PM

IUSHDFIUAZWD NEVERMIND

i am so proud of myself right now. Thankyou, you helped me a fudge load and got me thinking on the right track!

<html>
<head>
<title>Bubble Sort Example</title>

</head>
<body>
<script language="Javascript">

First = prompt("Score - Name - User");
Second = prompt("Score - Name - User");
Third = prompt("Score - Name - User");
Forth = prompt("Score - Name - User");
Fifth = prompt("Score - Name - User");
var a  = [ First, Second, Third, Forth, Fifth ]; 
var temp;

 
    // sorting the array using two for loops
	for (var pass = 0; pass < a.length - 1; pass++)
	{
	    for (var i = 0; i < a.length - 1; i++)
		{
		    // swapping the elements around 
		    if ( a[i] < a[i + 1] )
		    {
			    temp = a[i];  //assigning the higher value to a temp variable
				a[i] = a[i + 1];  // assigning the lower value to the element with higher value
				a[i + 1] = temp;  // assigning higher value stored in temp to the element with one index higher.
            }
		}
	}

	document.writeln("Scores:<br/>");

	document.writeln(a.join(", <br/>")); // output the sorted array elements
 
</script>
</body>
</html> 


The only bad thing about this is you have to put the score first or it wont work, but i dont mind ill be the only one writing in it. THANKYOU! Ahhh so proud right now.

Now to make it pretty :D
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1