Find the Longest Word in a String(not satisfied with my current solution)

Tell us what’s happening:

I realized that i merely came up with a solution based on various results that did not really differ much. I’d say the only thing unique about my solution is are the names of the variables.
here is my solution:
Your code so far

function findLongestWord(str) {
  var splitting = str.split(" ");//  str is split into 8 arrays
  var loop = splitting[0];
for (var i=0; i< splitting.length; i++){
  if(splitting[i].length > loop.length){
    loop = splitting[i];
  return loop.length;  

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

I originally came up with this code and while it yielded results, it did not solve the algorithm:

function findLongestWord(str) {
  var splitting = str.split(' ');//  str is split into 8 arrays
  var lazyDog = splitting[4].length; //  returns 6 in string 1 and 8 in string 4
  var theForce = splitting[2].length;//  returns 5 in string 2
  var googleBarrel = splitting[0].length; // returns 6 in string 3
  var whatIs = splitting[5].length; //returns 8 in string 4
  var whatIf = 19;
  var combine = [lazyDog,theForce,googleBarrel,whatIs,whatIf];   // an array of variables from splitting to whatIf
//   newArray eliminates the need for lazyDog, theForce, googleBarrel
  var newArray ={
    //   concatenation of newArray's values into other variables
   var combine = splitting[0].concat(splitting[1]);
    return val.length;
  });//  newArray keeps track of the number values inside each array in var splitting   
 return combine.length;

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

some of the code is arbitrary but I was wondering if I could create a solution to the algorithm? Any ideas or hints? I don’t need a full blown answer.
Your browser information:

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

Link to the challenge:

I’ve edited your post for readability. When you enter a code block into the forum, remember to precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.


1 Like