Basic Algorithm Scripting - Find the Longest Word in a String

Tell us what’s happening:
Describe your issue in detail here.
// running tests
findLongestWordLength(“Google do a barrel roll”) should return 6.
// tests completed
// console output
2

what’s wrong with my code it works fine on all the user tests except for one

Your code so far

var longWord = "";
function findLongestWordLength(str) {
  let j = 1;
  let word = str.split(" ");
  for (let i=0; i<word.length; i++) {
    if (word[i].length > word[j].length) {
      longWord = word[i];
      j++;
    }
  }
  return longWord.length;
}

console.log(findLongestWordLength("Google do a barrel roll"));

Your browser information:

User Agent is: Mozilla/5.0 (Linux; Android 11; Nokia 3.4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Mobile Safari/537.36

Challenge: Basic Algorithm Scripting - Find the Longest Word in a String

Link to the challenge:

Hi,

What is the perpouse of j ?

var longWord = "";  // It would be best to place function specific variables inside the function. 

function findLongestWordLength(str) {
  let j = 1;  // What is the perpose of this?
  let word = str.split(" ");
  for (let i=0; i<word.length; i++) {
    if (word[i].length > word[j].length) { // What is 'j' meant to do here?
      longWord = word[i];
      j++;
    }
  }
  return longWord.length;
}

to check the second word position i thought it will be easier than a nested loop

Alright, let’s visualize this code.

function findLongestWordLength(str) {
  var longWord = ""; 
 let word = str.split(" "); 
     // Let us assume that the value if `word` = ["Google", "for", "me"] 
  let j = 1;  
  for (let i=0; i<word.length; i++) {
    if (word[i].length > word[j].length) { 
      longWord = word[i];
      j++;
    }
  }
  return longWord.length;
}

First for iteration:

if (6 > 3) then longWord = “Google” . j = 2

Second for iteration:

if(3 > 2) then longWord = “for”. j =3

Third iteration:

if(4 > undefined) …false

For loop ends with longWord = "for"

The problem obvious for me now but there’s on more question why it works on different string
Llike this one “The quick brown fox jumped over the lazy dog”
Because j++ is inside the condition

The goal of the loop is to iterate through an array of strings and find the longest.

Have you come across the Math.max() as yet? What max() does is to take two numbers and then return the largest one. So Math.max(1, 2) will return 2.

The reason that I have mentioned this is to give you an idea of the essence of what the for loop should be doing.

Consider this. Before entering the loop, longWord.length is 0. Instead of using j. You could say “Hey, if the length of the current word (word[i]) is greater then the current length of the value in longWord , then replace the current value of longWord with word[i], Keep on doing that until the loop ends”

[Spoiler]
It worked thanks a lot man here’s my code

var longWord = “”;
function findLongestWordLength(str) {
let word = str.split(" ");
for (let i=0; i<word.length; i++) {
if (word[i].length > longWord.length) {
longWord = word[i];
}
}
return longWord.length;
}

console.log(findLongestWordLength(“May the force be with you”));

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.