Find the Longest Word in a String error when I return

Tell us what’s happening:
why is my code is not satisfying just this one case
(“May the force be with you”)?
But when I console.log(Math.max.apply(null, arr)); it is displaying 5
still error when I return it? please help. Thank you.

Your code so far


var newstr="";
var arr = [];
function findLongestWordLength(str) {
newstr = str
.split(' ');

for(let i = 0;i < newstr.length; i++){
    arr.push(newstr[i].length);
  }
  return Math.max.apply(null, arr);
}

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

// running tests

findLongestWordLength("May the force be with you")

should return 5. // tests completed

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36 Edg/84.0.522.52.

Challenge: Find the Longest Word in a String

Link to the challenge:

Your code contains global variables that are changed each time the function is run. This means that after each test completes, subsequent tests start with the previous value. To fix this, make sure your function doesn’t change any global variables, and declare/assign variables within the function if they need to be changed.

Example:

var myGlobal = [1];
function returnGlobal(arg) {
  myGlobal.push(arg);
  return myGlobal;
} // unreliable - array gets longer each time the function is run

function returnLocal(arg) {
  var myLocal = [1];
  myLocal.push(arg);
  return myLocal;
} // reliable - always returns an array of length 2
1 Like