May 2017
Intermediate to advanced
448 pages
10h 10m
English
If our plugin needs to provide more than one global function, we could declare them independently. Here, we'll revise our plugin, adding a function to compute the average of an array of numbers:
(($) => { $.sum = array => array.reduce( (result, item) => parseFloat($.trim(item)) + result, 0 ); $.average = array => Array.isArray(array) ? $.sum(array) / array.length : '';})(jQuery);
For convenience and brevity, we're using the $.sum() plugin to assist us in returning the value for $.average(). To decrease the chance of errors, we also check the argument to make sure it is an array before computing the average.
Now that a second method is defined, we can call it in the same fashion:
$(() => { const $inventory ...Read now
Unlock full access