Getting Started with Javascript

Mon 10 August 2015

Filed under javascript

Some resources for basic javascript

HTML Dom Events reread: (http://www.w3schools.com/js/js_performance.asp)

Sort Ascending

var points = [40, 100, 1, 5, 25, 10];
points.sort(function(a, b){return a-b});

Sort Descending

var points = [40, 100, 1, 5, 25, 10];
points.sort(function(a, b){return b-a});

The Compare Function: function(a, b){return a-b} The purpose of the compare function is to define an alternative sort order.

The compare function should return a negative, zero, or positive value, depending on the arguments:

function(a, b){return a-b} When the sort() function compares two values, it sends the values to the compare function, and sorts the values according to the returned (negative, zero, positive) value.

Example:

When comparing 40 and 100, the sort() method calls the compare function(40,100).

The function calculates 40-100, and returns -60 (a negative value).

The sort function will sort 40 as a value lower than 100.


Comments


DeeKras.com © Dee Kras Powered by Pelican and Twitter Bootstrap. Icons by Font Awesome and Font Awesome More