Nougati
11-06-2012, 04:32 AM
Hey guys,
This is my first time on this website ever, so I have no idea how many people come through here, nor do I know how useful this post will be for me, but bare with me.
Anyway, the topic:
Without going to far into the redundancies, in my high school IT class, my teacher is a bit... slow. Henceforth I am usually having to accept some parts of my understanding as simply inexistent. This is an example.
We started Javascript and began with box sort algorithms (input fields that would sort the values in to an order). However my troubles began when I made it to 10 box sort, and considering my teacher hadn't "come up with the answer yet", troubleshooting my code was virtually bashing my head against a wall.
So I got one of my mates from another class to send me his code so I could compare. One thing I noticed is that his code is more comprehensive and has functions that i can only assume his teacher taught him.
So if someone could walk me through the process of the program, from the button press to the outcome of the boxes, that would be incredible.
I have basic knowledge of all the functions of the loops, but still not extensively.
<html>
<head>
<title>Bubble Sorting</title>
<script type = "text/javascript">
function arraySort()
{
var count;
var myNums=new Array();
var temp;
var B1
var Max =10;
var swapped=true;
for(count=1; count<=Max;count++)
{
myNums[count]=parseInt(document.getElementById('box'+count).value);
}
while (swapped)
{
swapped=false;
for (B1=1;B1<=(Max-1);B1++)
{
if(myNums[B1]>myNums[B1+1])
{
temp=myNums[B1];
myNums[B1]=myNums[B1+1];
myNums[B1+1]=temp;
swapped=true;
}
}
}
for(count=1; count<=Max;count++)
{
document.getElementById('box'+count).value=myNums[count];
}
alert("Sorted");
}
</script>
</head>
<body>
<input id = "box1" type = "text" name = "box1" value = "7" /><br>
<input id = "box2" type = "text" name = "box2" value = "1" /><br>
<input id = "box3" type = "text" name = "box3" value = "3" /><br>
<input id = "box4" type = "text" name = "box4" value = "3" /><br>
<input id = "box5" type = "text" name = "box5" value = "7" /><br>
<input id = "box6" type = "text" name = "box6" value = "1" /><br>
<input id = "box7" type = "text" name = "box7" value = "3" /><br>
<input id = "box8" type = "text" name = "box8" value = "3" /><br>
<input id = "box9" type = "text" name = "box9" value = "7" /><br>
<input id = "box10" type = "text" name = "box10" value = "1" /><br>
<input id = "sortButton" type = "button" name = "sprt" value = "Sort" onclick =
"arraySort()" />
</body>
</html>
Main things I don't understand is:
var swapped=true
...
swapped=false;
for (B1=1;B1<=(Max-1);B1++)
{
if(myNums[B1]>myNums[B1+1])
{
temp=myNums[B1];
myNums[B1]=myNums[B1+1];
myNums[B1+1]=temp;
swapped=true;
}
and
var count;
...
for(count=1; count<=Max;count++)
{
myNums[count]=parseInt(document.getElementById('box'+count).value);
}
Any help whatsoever would be amazing.
Thanks.
This is my first time on this website ever, so I have no idea how many people come through here, nor do I know how useful this post will be for me, but bare with me.
Anyway, the topic:
Without going to far into the redundancies, in my high school IT class, my teacher is a bit... slow. Henceforth I am usually having to accept some parts of my understanding as simply inexistent. This is an example.
We started Javascript and began with box sort algorithms (input fields that would sort the values in to an order). However my troubles began when I made it to 10 box sort, and considering my teacher hadn't "come up with the answer yet", troubleshooting my code was virtually bashing my head against a wall.
So I got one of my mates from another class to send me his code so I could compare. One thing I noticed is that his code is more comprehensive and has functions that i can only assume his teacher taught him.
So if someone could walk me through the process of the program, from the button press to the outcome of the boxes, that would be incredible.
I have basic knowledge of all the functions of the loops, but still not extensively.
<html>
<head>
<title>Bubble Sorting</title>
<script type = "text/javascript">
function arraySort()
{
var count;
var myNums=new Array();
var temp;
var B1
var Max =10;
var swapped=true;
for(count=1; count<=Max;count++)
{
myNums[count]=parseInt(document.getElementById('box'+count).value);
}
while (swapped)
{
swapped=false;
for (B1=1;B1<=(Max-1);B1++)
{
if(myNums[B1]>myNums[B1+1])
{
temp=myNums[B1];
myNums[B1]=myNums[B1+1];
myNums[B1+1]=temp;
swapped=true;
}
}
}
for(count=1; count<=Max;count++)
{
document.getElementById('box'+count).value=myNums[count];
}
alert("Sorted");
}
</script>
</head>
<body>
<input id = "box1" type = "text" name = "box1" value = "7" /><br>
<input id = "box2" type = "text" name = "box2" value = "1" /><br>
<input id = "box3" type = "text" name = "box3" value = "3" /><br>
<input id = "box4" type = "text" name = "box4" value = "3" /><br>
<input id = "box5" type = "text" name = "box5" value = "7" /><br>
<input id = "box6" type = "text" name = "box6" value = "1" /><br>
<input id = "box7" type = "text" name = "box7" value = "3" /><br>
<input id = "box8" type = "text" name = "box8" value = "3" /><br>
<input id = "box9" type = "text" name = "box9" value = "7" /><br>
<input id = "box10" type = "text" name = "box10" value = "1" /><br>
<input id = "sortButton" type = "button" name = "sprt" value = "Sort" onclick =
"arraySort()" />
</body>
</html>
Main things I don't understand is:
var swapped=true
...
swapped=false;
for (B1=1;B1<=(Max-1);B1++)
{
if(myNums[B1]>myNums[B1+1])
{
temp=myNums[B1];
myNums[B1]=myNums[B1+1];
myNums[B1+1]=temp;
swapped=true;
}
and
var count;
...
for(count=1; count<=Max;count++)
{
myNums[count]=parseInt(document.getElementById('box'+count).value);
}
Any help whatsoever would be amazing.
Thanks.