Having a little bit of trouble with Math.max.apply(); seem to be able to get the result I want to show within a console.log(); but when adding it to a variable and console loggin that it just throws a -Infinity message within the console.
The idea is to grab all the numbers from the .onsale classes then add to a variable and work out the max number from those, then inject that max number into an element on the page but got stuck trying to calculate the largest number from an array and add it to a variable in this case savingMax. Have I gone down the wrong path?
Here is my code so far.
<div class="onsale">79</div>
<div class="onsale">91</div>
<div class="onsale">20</div>
jQuery(document).ready(function($){
var savingArray = [];
var savingMax; //Example variable for holding the highest number ready for inserting in DOM
$(".onsale").each(function (){
var saving = $(this).text();
var savingAmount = saving.replace(/\D+/g, '');
savingArray.push(savingAmount);
});
console.log(Math.max.apply(Math, savingArray));
});
The document ready is needed on my theme I am apply the code on. Jsfiddle Example