Hey guys,
although I already have solved this algorithm. using this solution:
this solution passed:
let stringArr = str.split(' '); //transform str into an arr;
let longestStr = 1; //default value for longestStr;
for(let i=0; i<stringArr.length; i++){
console.log(stringArr[i].length);
if(longestStr < stringArr[i].length) {
longestStr = stringArr[i].length;
}
}
return longestStr;
But my first solution was usng ES6 & .sort() which looked like this:
this didn’t pass
function findLongestWordLength(str) {
// //transform string into an array of strings
let stringArr = str.split(' ');
// //sort array in descending order and return the length first array in the order which is the longest string in the array
const longestWord = stringArr.sort((a,b) => a.length < b.length);
return longestWord[0].length;
}
findLongestWordLength("What if we try a super-long word such as otorhinolaryngology");
my logic for this challenge is like this:
- transform the string into arr hence the .split(" ");
- sort the array into descending order so that the first string array is the longest.
- return the length of the first array in the order.
I tested it in repl.it and it returns the expected value being asked in the challenge which is the length of the longest word in the sentence, so why is it not being accepted as a solution?
here is the link to the challenge: link
I hope someone can shed some light on this.