longest Word function receive 1 argument 'sentence' the function return the longest word in the sentence,only letters will be co | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

longest Word function receive 1 argument 'sentence' the function return the longest word in the sentence,only letters will be co

Hi, i am using js and i need help from someone to fix my code.please!!! https://code.sololearn.com/WqmCzf9qsQmS/?ref=app

30th Jul 2022, 1:12 PM
k gready
k gready - avatar
10 Answers
+ 3
Please post your code, so we can see where you make mistake and help you to dont repeat it.
30th Jul 2022, 1:19 PM
PanicS
PanicS - avatar
+ 2
Do you need to check what word is longest or to get how much this word have characters? In this code i did both, and I used forEach because it have cleaner syntax: https://code.sololearn.com/WOzBB39C0RWc/?ref=app
30th Jul 2022, 2:42 PM
PanicS
PanicS - avatar
+ 2
You can return word just, and than check length if you need this info also, later in code outside of function. (We can only return 1 thing from function,for more than one data we can use array or object) I make 2 variable just because I didnt know what data you need. So remove my console.log and place just return longestWord, if you need length you can always save returned value to some variable and do anything you wish with this data like returnedWord.length
30th Jul 2022, 3:04 PM
PanicS
PanicS - avatar
+ 1
You have to check each character of word, and if this is not letter(upercase or lowercase) remove from word. Best is to use regex for it,for thus you dont need some advanced regex pattern, check some regex tutorial like this: https://regexlearn.com/learn
30th Jul 2022, 5:22 PM
PanicS
PanicS - avatar
0
const longestWord = (sentence)=>{ var sEntenceSplit = sentence.split(' '); var longestWord=0; for(var i=0; i<sEntenceSplit.length; i++){ if(sEntenceSplit[i].length > longestWord && sEntenceSplit[i].includes("co")){ longestWord = sEntenceSplit[i].length; } } return longestWord; } console.log( longestWord("code sololearn com") );
30th Jul 2022, 1:51 PM
SoloProg
SoloProg - avatar
0
SoloProg why and co? It need to be to any sentence
30th Jul 2022, 1:56 PM
k gready
k gready - avatar
0
const longestWord = (sentence)=>{ let sEntenceSplit = sentence.split(' '); let longestWord=""; let ln=0; for(let i=0; i<sEntenceSplit.length; i++){ if(sEntenceSplit[i].length > ln){ ln = sEntenceSplit[i].length; longestWord = sEntenceSplit[i]; } } return longestWord; } // To display the longest word in a sentence. console.log( longestWord("code sololearn com") ); document.write ( longestWord("code sololearn com") );
30th Jul 2022, 2:03 PM
SoloProg
SoloProg - avatar
0
SoloProg didn't work well...
30th Jul 2022, 2:08 PM
k gready
k gready - avatar
0
PanicS both I think and you have to use the Return and not console.log
30th Jul 2022, 2:49 PM
k gready
k gready - avatar
0
PanicS it has to ignore special signs and numbers but still count the word as if it hasn't have special signs and numbers
30th Jul 2022, 3:19 PM
k gready
k gready - avatar