Skip to content Skip to sidebar Skip to footer

Js Cannot Read Property "length" Of Undefined

I'm trying to create an object using a given string where each word has a property stating its length. var strings = {}; function findLongestWord(str) { var splitStr = str.spli

Solution 1:

you are looping over wrong array. you should use i < splitStr.length.

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

  return strings;
}

Post a Comment for "Js Cannot Read Property "length" Of Undefined"