- The reverse() method used to reverse the order of the elements in an array.
- The sort() method is used to sort the items of an array.
Code:-
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<h1>reverse() and sort() in number </h1>
<h1>Array Value : [1,50,4,56,100]</h1>
<input type="button" onclick="fun1();" value="Normal sort()">
<input type="button" onclick="fun2();" value="Normal reverse()">
<input type="button" onclick="fun3();" value="Correct sort()">
<input type="button" onclick="fun4();" value="Correct reverse()">
<script>
var ar_num = [1, 50, 4, 56, 100];
function fun1()
{
alert(ar_num.sort());
}
function fun2()
{
alert(ar_num.reverse());
}
function fun3()
{
ar_num.sort(function(a, b)
{
return a - b;
});
alert(ar_num);
}
function fun4()
{
ar_num.sort(function(a, b)
{
return b - a;
});
alert(ar_num);
}
</script>
</body>
</html>
Array sort and reverse:- Geniusofstudent
Reviewed by Network security
on
August 06, 2019
Rating:
No comments: