Find the Longest Word in a String -- Help, please! Can't find a mistake

Find the Longest Word in a String -- Help, please! Can't find a mistake
0

#1

Tell us what’s happening:

Your code so far

function findLongestWord(str) {
  var newStr=str.split(' ');
  for (i = 0; i < newStr.length; i++) {
    newArray.push(newStr[i].length);
  }
  return Math.max.apply(null, newArray);
}
findLongestWord("May the force be with you");

Your browser information:

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

Link to the challenge:


#2

Take a look at console.log , the words are being pushed into an array that is not defined.


#3

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 new 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

#4
  Find the Longest Word in a String ****my solution****

function findLongestWord(str) {

var stringOne = str.split(’ ');

function length(val){

return val.length;

}

var charLength = stringOne.map(length);

function largestNumber(prev,curr){

if(prev < curr){

return curr;

}else{

return prev;

}}

return charLength.reduce(largestNumber);
}