-5

I cannot figure out what to do. I keep getting an error because the array exceeds the bounds but I am not sure why. This is the latest instance of the code there have been many attempts.

private static String compareWords(String[] words) {
    String longestWord = words[0];
    for (int i = 0; i <= words.length; i ++){
        while (longestWord.length()< words[i].length()){
            longestWord=words[i];                
        }
    }                
    return longestWord;
}
Andrew Li
  • 55,805
  • 14
  • 125
  • 143

1 Answers1

-2

Your post isn't javascript, but here's basically what you want to do. It should be easy to change it over from javascript.

function compareWords(words){
  var longestWord = '';
  for(var i = 0; i < words.length; i++){
    if(words[i].length > longestWord.length){
      longestWord = words[i];
    }
  }
  return longestWord;
}
var words = ['gunslinger', 'gundam', 'dragon', 'shirt', 'unicorn', 'internationalization'];
compareWords(words);
Jeremy Jackson
  • 2,247
  • 15
  • 24