jeudi 27 juillet 2017

Else statement not working

I am trying to return the longest even word in the array using a forEach loop and return "00" when there is no even word. I am able to get the part of returning the longest even word to work correctly, but when I introduce the else statement it doesn't work anymore:

function FindlongestWord(input) {
  var arrWords = input.split(' ');
  var wordlength = 0;
  var word = '';
  arrWords.forEach(function(wrd) {
    if (wordlength < wrd.length && wrd.length % 2 == 0) {
      wordlength = wrd.length;
      word = wrd;
    } else  {
      return "00";
    }
  });
  return word;
}

Aucun commentaire:

Enregistrer un commentaire