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
10 Answers
+ 3
Please post your code, so we can see where you make mistake and help you to dont repeat it.
+ 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
+ 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
+ 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
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") );
0
SoloProg why and co? It need to be to any sentence
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") );
0
SoloProg didn't work well...
0
PanicS both I think and you have to use the Return and not console.log
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