Javascript String Question 15

Longest Word

Create a function that returns the longest word in a sentence.

function longest(str) {
    //your code here
   //from   ww  w . java2 s .  c  o m
    return result;
}

// Output
console.log(longest("this is a test"));
console.log(longest("hi hey"));
console.log(longest("abc defg"));
function longest(str) {
    var words = [""],
        result = [],//  ww w  . j a  va 2s.  c o  m
        obj = {};
    
    // Remove punctuation
    str = str.replace(/[^\w\s]|_/g, "")
             .replace(/\s+/g, " ");

    // Find longest words   
    str = str.split(' ');
    for (var i = 0; i < str.length; i++) {
        if (str[i].length === words[0].length) {
            words.push(str[i]);
        } else if (str[i].length > words[0].length) {
            words = [str[i]];
        }
    }

    // Remove duplicates
    for (var i = 0; i < words.length; i++) {
        obj[words[i]] = 0;
    }
    for (i in obj) {
        result.push(i);
    }
   
    return result;
}

console.log(longest("this is a test"));
console.log(longest("hi hey"));
console.log(longest("abc defg"));

Notes

Remove Punctuation

.replace(/[^\w\s]|_/g, "") - Replace anything that is not an alphanumeric character or whitespace.

.replace(/\s+/g, " ") - Collapse multiple spaces to single spaces.

Remove Duplicate Code

Only one instance of a key can exist within an object.

Object keys are always unique.

When a duplicated word (key) is added to the object, it replaces any previous keys of the same name.

Object key act as a filter, leaving only one instance of each duplicate word.




PreviousNext

Related