also its a general better practice to use let as its a smallerscoped than var
function findLongestWord(str) {
//Convert to array to seperate words
let Arr = str.split(' ');
//Store first word as current longest
let curLongest = Arr[0];
//Compare word lengths
for (let i=0; i<Arr.length; i++){
if (Arr[i].length >= curLongest.length){
curLongest = Arr[i];
}
}
return curLongest.length;
}
findLongestWord("The quick brown fox jumped over the lazy dog");
Hello, i wrote this code it permits me to put the longest word if i want it
function findLongestWord(str) {
var arr = str.split(" ");
var taille = arr[0];
for(var i = 1;i < arr.length;i++){
if(taille.length > arr[i].length){
taille = taille;
}else{
taille = arr[i];
}
}
return taille.length;
}
Here is other version that i wrote
function findLongestWord(str) {
var arr = str.split(" ");
var taille = arr[0];
for(var i = 1;i < arr.length;i++){
if(taille.length < arr[i].length){
taille = arr[i];
}
}
return taille.length;
}
And this
function findLongestWord(str) {
var arr= [];
var chaine = str.split(' ');
for(var i = 0; i < chaine.length;i++){
arr.push(chaine[i]);
}
var sortie = arr[0];
for(var i = 1; i < arr.length;i++){
if(sortie.length < arr[i].length){
sortie = arr[i];
}
}
return sortie.length;
}
function findLongestWord(str) {
return str.split(' ').map(function (x){return x.length;}).reduce(function(p,c){return p>c ? p :c;});
}
findLongestWord("The quick brown fox jumped over the lazy dog");
function findLongestWord(str) {
var splitStr = str.split(" ");
word = " ";
for (var i in splitStr){
if (splitStr[i].length >= word.length){
word = splitStr[i];
}
}
return word.length;
}
findLongestWord("The quick brown fox jumped over the lazy dog");
I’m not understanding why this code does not work as a solution. the console keeps telling me “cannot read property length of undefined”. Help would be greatly appreciated!
function findLongestWord(str) {
var wordArray = [];
var lengthArray = [];
wordArray = str.split(’ ');
for (var i = 0; i <= wordArray.length; i++) {
lengthArray.push(wordArray[i].length);
}
lengthArray.sort(function(a,b) {
return a - b;
});
return lengthArray[lengthArray.length - 1];
}
findLongestWord(“The quick brown fox jumped over the lazy dog”);
Hello, I managed to make a solution without using .split() or .reduce() or any operation that modifies the entire string. Instead, the logic works by going through the string and checking whether it is a word character or not. If it’s not, it considers it a new word and changes maxCount. Here’s my code:
function findLongestWord(str) {
var curCount = 0;
var maxCount = 0;
var i = 0;
while (i < str.length) {
if (str[i].match(/\w/)) {//If it's a word character, it adds 1 to curCount
curCount++;
if (i === str.length - 1) { //If it's a word and we're at the end as well:
if (maxCount < curCount) {//If the current length is the longest
maxCount = curCount;//maxCount is changed
return maxCount;//Finally, return maxCount
} else {
return maxCount; //If maxCount is still the biggest, just return it.
}
} else {
i++; //If we're not at the end, we just increment i after incrementing curCount
}
} else { //If it isn't a word character (e.g. if it was a whitespace):
if (i === str.length - 1) {//If we're at the end:
if (maxCount < curCount) {
maxCount = curCount;
return maxCount;
} else {
return maxCount;
}
} else {//If we're not at the end:
if (maxCount < curCount) {//Compare maxCount and curCount
maxCount = curCount;
curCount = 0;
i++;//Increment, since we're not at the end
} else {
curCount = 0;//Reset
i++;//Increment
}
}
}//End of final else
}
return maxCount;//Just in case
}
findLongestWord(“What if we try a super-long word such as otorhinolaryngology”);
I know it’s quite long, and there’s a whole mess of if statements… I wanted to ask whether this was any more efficient than the other solutions, or if it’s the same or potentially worse. Not sure how to check that.
My code -
Feedback appreciated. The only problem I had was the callback function in the sort method. Otherwise, it went smoothly. Is this the most efficient way?
function findLongestWord(str) {
var arrLength = [];
arr = str.split(" ");
for (var i = 0 ; i < arr.length; i++) {
arrLength.push(arr[i].length);
var sorted = arrLength.sort(function (a,b){
return a-b;
});
}
return arrLength[arrLength.length - 1] ;
}
findLongestWord(“The quick brown fox jumped over the lazy dog”);