1.

var A = {    x: function() {        console.log('x');    },    y: function() {        console.log('y');    },    z: function() {        console.log('z');    }     } A.x().y().z();

Answer»

The sort() method is used to sort the elements of an array. But the output of the above is not what expected out of sort() function. The output will be [ 1, 15, 2, 30, 4, 45, 7 ] and it is far from desired.

This is because the default sort is ACCORDING to TRING Unicode points. The fix to it is by ADDING an anonymous function and tell to sort according to ascending or descending order. 

Ascending order is as below:

CONST array = [1, 2, 15, 4, 30, 7, 45]; console.log(array.sort((a, b)=> a-b)); //[ 1, 2, 4, 7, 15, 30, 45 ]

Descending order is as below:

const array = [1, 2, 15, 4, 30, 7, 45]; console.log(array.sort((a, b)=> b-a)); //[ 45, 30, 15, 7, 4, 2, 1 ]


Discussion

No Comment Found