<html>
<script lang="text/javascript">
document.write("<h3>PROGRAM 6 : </h3><h4>REMOVE HIGHEST ELEMENT FROM ARRAY AND SORTING DATA IN ARRAY</h4>");
var arr=new Array();
arr[0]=5;
arr[1]=20;
arr[2]=2;
arr[3]=7;
arr[4]=15;
display("BEFORE SORTING<br>");
sort();
display("AFTER SORTING <br>");
arr.pop(); //POP IS INBUILD FUNCTION
display("REMOVING HIGHEST DATA AND DISPLAYING <br>");
//*******DISPLAYING FUNCTION***********
function display(msg)
{
document.write(msg);
for(var i in arr)
document.write(arr[i]+"<BR>");
}
//***********SORTING FUNCTION***********
function sort()
{
var temp;
for(var i in arr)
{
for(var j in arr)
{
if (arr[i]<arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
}
</script>
<body>
</body>
</html>
Great example of iterating an array and removing each element. Thanks for sharing!
ReplyDelete