Find the longest word in a string challenge on Fcc

Find the longest word in a string challenge on Fcc
0

#1

Dear team,

pls i am having issues with challenge: Basic Algorithm Scripting: Find the Longest Word in a String

findLongestWordLength(“The quick brown fox jumped over the lazy dog”) should return a number.

findLongestWordLength(“The quick brown fox jumped over the lazy dog”) should return 6.

findLongestWordLength(“May the force be with you”) should return 5.

findLongestWordLength(“Google do a barrel roll”) should return 6.

findLongestWordLength(“What is the average airspeed velocity of an unladen swallow”) should return 8.

findLongestWordLength(“What if we try a super-long word such as otorhinolaryngology”) should return 19.

i used the code below and it didnt work but when i tried it on the console of my browser it worked perfectly…pls what do i do

function findLongestWord(str) {
  var strSplit = str.split(" ");
  var longestWord = 0;
  for(var i = 0; i < strSplit.length; i++){
    if(strSplit[i].length > longestWord){
	longestWord = strSplit[i].length;
     }
  }
  return longestWord;
}
findLongestWord("The quick brown fox jumped over the lazy dog");

6// this was the returned answer


#2

Add the link to the challenge.


#3

Also, if don’t use Internet Explorer. It’s known to cause problems for people doing these challenges.


#4

Why did you change the function name that was given in the challenge? My guess is you copied and pasted this solution from another forum thread. Why do I think this? Because the curriculum has changed recently and the function name you are using is the old function name of the old curriculum and mentioned in many older forum posts.


#5

what i pasted here was the guide that i looked at before trying to solve the challenge…but i have tried it with the correct function name and it didnt work before copying the guide to know if the problem was from me…


#6

https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-algorithm-scripting/find-the-longest-word-in-a-string


#7

Look at what the test results are saying and compare that with the function name you have declared.


#8

this is mine and this cansolve the challenge:

function findLongestWordLength(str) {
 var strSplit = str.split(" ");
  var longestWord = 0;
  for(var i = 0; i < strSplit.length; i++){
    if(strSplit[i].length > longestWord){
	longestWord = strSplit[i].length;
     }
  }
  return longestWord;
}
findLongestWordLength("The quick brown fox jumped over the lazy dog");

#9

it only pass the the first condition of the challenge


#10

i have seen my error,i did: var strSplit = str.split(""); instead of: var strSplit = str.split(" ");


#11

Regardless of where you got the code, it does not work for this challenge.

Why? Because for following test case, your code returns 1 instead of 6.

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

#12

but the code is working now sir


#13

That is good to know. Looks like you figured out your issue.


#14

thanks for your insight