开发者

javascript match two arrays then display max value

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 31and longestMonths is ['jan', 'march']

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜