最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - "cannot read property 'split' of undefined" - Stack Overflow

programmeradmin1浏览0评论

I'm getting the following error:

cannot read property 'split' of undefined

However the array variable with the split method attached is correctly defined in the previous code line.

function findLongestWord(str) {

  for (i = 0; i < str.length; i++) {
    var array = str.split(" ");
    array[i].split("");
  }
}

findLongestWord("The quick brown fox jumped over the lazy dog");

I'm getting the following error:

cannot read property 'split' of undefined

However the array variable with the split method attached is correctly defined in the previous code line.

function findLongestWord(str) {

  for (i = 0; i < str.length; i++) {
    var array = str.split(" ");
    array[i].split("");
  }
}

findLongestWord("The quick brown fox jumped over the lazy dog");

Share Improve this question edited Apr 20, 2018 at 9:52 Biffen 6,3496 gold badges31 silver badges37 bronze badges asked Apr 20, 2018 at 9:49 Adrian DanlosAdrian Danlos 31 gold badge2 silver badges6 bronze badges 4
  • 3 Do you want to iterate over each word, or do you want to iterate over each character? Figure that out, and then you can figure out which line you need to remove – CertainPerformance Commented Apr 20, 2018 at 9:51
  • 1 what is your expected output ? – Muhammad Usman Commented Apr 20, 2018 at 9:52
  • 3 str.length is 44. array has 9 elements. What do you think happens when you access array[10]? – Ivar Commented Apr 20, 2018 at 9:52
  • 2 Possible duplicate of Javascript: find longest word in a string – Durga Commented Apr 20, 2018 at 9:54
Add a ment  | 

3 Answers 3

Reset to default 1

str.length is actually the number of letters in the string, while array is an array of words. That's why i can go up to 45, while your array has only 9 elements - that's why when it tries to access array[10] it gets undefined and it can't split it. This should help:

function findLongestWord(str) {
  var array = str.split(" ");
  for (i = 0; i < array.length; i++) {
    array[i].split("");
  }
}

findLongestWord("The quick brown fox jumped over the lazy dog");

If you want it to actually return the longest word, you need to do something like this:

function findLongestWord(str) {
  var longestWord = ""
  var array = str.split(" ");
  for (i = 0; i < array.length; i++) {
    if(array[i].length > longestWord.length){
      longestWord = array[i]
    }
  }
  return longestWord
}

findLongestWord("The quick brown fox jumped over the lazy dog");

str.length get the length of the string counting chars, so 44, then you do the split by spaces, so you get the words but you continue to call index of each char in the string, to get longest word instead of:

array[i].split("");

you must do:

var longest = "";
array.forEach(function(word) {
    if(word.length > longest.length) {
      longest = word;
    }
});

You can directly apply split(" ") and use reduce() method of array to get required result.

The reduce() method applies a function against an accumulator and each element in the array (from left to right) to reduce it to a single value.

DEMO

const str = "The quick brown fox jumped over the lazy dog";

let result = str.split(" ").reduce((r, v) => r.length > v.length ? r : v, '');

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

发布评论

评论列表(0)

  1. 暂无评论