The function helps us sort the words that are smallest to the longest length in string…
function findLongestWordLength(str) {
let regex = /\w+([^w\s]+\w+)|\w+/g;
let result = str.match(regex);
for (let i = 1; i < result.length; i++) {
for (let j = 0; j < result.length; j++) {
if (result[i].length < result[j].length) {
let temp = result[i];
result[i] = result[j];
result[j] = temp;
}
}
}
return result;
}
console.log(
findLongestWordLength(
"What if we try a super-long word such as otorhinolaryngology"
)
);
//Output is :[“a”, “we”, “if”, “as”, “try”, “word”, “such”, “What”, “super-long”, “otorhinolaryngology”]