I am trying to match two values in two arrays
var months ['jan', 'feb', 'march'];
var noDays ['31', '28', '31'];
Then i want to fin the months with the ma开发者_C百科ximum number and return them as such
"both jan and march have a total of 31 days"
any suggestions please
Well, that problem could be solved with a simple algorithm:
var months = ['jan', 'feb', 'march'];
var noDays = [31, 28, 31];
var maxDays = 0;
var longestMonths = [];
for (var i = 0; i<Math.min(months.length, noDays.length);i++){
if(noDays[i]>maxDays){
maxDays = noDays[i];
longestMonths = [months[i]];
}else if(noDays[i]==maxDays)
longestMonths.push(months[i]);
}
After executing this code, maxDays is 31
and longestMonths is ['jan', 'march']
精彩评论