...

View Full Version : sorting numbers



gencoglu
06-13-2007, 10:33 PM
I am tring to sort some six numbers..the problem is that it doesnt work when
I use "document.getElementById" instead of "document.write()
my program is supposed to write six numbers every second..line by line
Could you get it work ? Thanks





<html>

<body>
<script>
setInterval("sortN()",1000);

function sortN()
{
for(var m=1;m<=6;m++)
{
arr[m] = Math.floor(Math.random()*49)+1;
document.getElementById("kut").innerHTML+=arr.sort(sortN)+".."+"<br>";
} }
</script>
<div id="kut"></div>
<input type="button" value="baslat"onclick="sortN()">
</body>
</html>

glenngv
06-13-2007, 10:50 PM
Is this what you need?

<html>
<head>
<script type="text/javascript">
var timer;
function compareFunc(a, b){ //sort in ascending order
return a - b;
}
function sortN()
{
var arr=[];
for(var m=1;m<=6;m++)
{
arr[m] = Math.floor(Math.random()*49)+1;
}
document.getElementById("kut").innerHTML += arr.sort(compareFunc)+".."+"<br />";
timer = setTimeout("sortN()",1000);
}
</script>
</head>
<body>
<form>
<input type="button" value="baslat" onclick="sortN()" />
<input type="button" value="stop" onclick="clearTimeout(timer)" />
<div id="kut"></div>
</form>
</body>
</html>

gencoglu
06-14-2007, 12:17 PM
Thanks...a lot...you are perfect
it worked for me....

gencoglu
06-15-2007, 10:47 PM
hi glenngv,
Your code works fine...Now there is a slight problem...some numbers are dublicated...Is there a way to avoid dublications or clearing the lines which cosist dublicated numbers ?

Many thanks in advance..

glenngv
06-15-2007, 11:33 PM
<html>
<head>
<script type="text/javascript">
Array.prototype.inArray = function(element){
for (var i=0; i<this.length; i++){
if (this[i]==element){
return true;
}
}
return false;
}


var timer;
var size = 6;

function compareFunc(a, b){ //sort in ascending order
return a - b;
}
function sortN()
{
var num, arr=[], m=0;
do
{
num = Math.floor(Math.random()*49)+1;
if (!arr.inArray(num)) {
arr[m] = num;
m++;
}
}
while (m<size)
document.getElementById("kut").innerHTML += arr.sort(compareFunc)+".."+"<br />";
timer = setTimeout("sortN()",1000);
}
</script>
</head>
<body>
<form>
<input type="button" value="baslat" onclick="sortN()" />
<input type="button" value="stop" onclick="clearTimeout(timer)" />
<div id="kut"></div>
</form>
</body>
</html>

gencoglu
06-16-2007, 12:12 AM
wow..you are unbelievable...
it works......thanks indeed

gencoglu
06-16-2007, 02:51 PM
Hi glenngv,
I think I need some more describtions about what those lines I coloured green mean...If you could write some commend next to those lines I will be very happy.
.Thanks in advance




<html>
<head>
<script type="text/javascript">
Array.prototype.inArray = function(element){
for (var i=0; i<this.length; i++){
if (this[i]==element){
return true;
}
}
return false;
}


var timer;
var size = 6;

function compareFunc(a, b){ //sort in ascending order
return a - b;
}
function sortN()
{
var num, arr=[], m=0;
do
{
num = Math.floor(Math.random()*49)+1;
if (!arr.inArray(num)) {
arr[m] = num;
m++;
}
}
while (m<size)
document.getElementById("kut").innerHTML += arr.sort(compareFunc)+".."+"<br />";
timer = setTimeout("sortN()",1000);
}
</script>
</head>
<body>
<form>
<input type="button" value="baslat" onclick="sortN()" />
<input type="button" value="stop" onclick="clearTimeout(timer)" />
<div id="kut"></div>
</form>
</body>
</html>

1212jtraceur
06-16-2007, 05:59 PM
I think I can explain these for you:

The first green line declares a method of all Arrays. It is a function that takes one argument, an Object, and returns a Boolean (2nd/3rd green lines) indicating whether or not that Object is in the Array.

The fourth green line uses Array literal notation to define an empty Array.

The last green line checks to see if the random Number is not in the previously defined Array, using the prototype method defined at the top.

Does that help? Search for 'javascript prototype' and 'javascript literal notation' (no quotes) to learn more about those concepts.

gencoglu
06-16-2007, 06:58 PM
Thanks....it helps..
I should search for more sample of the same code...

I like javascript!!!!!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum