Skip to main content
Strip punctuation.
Source Link
rolfl
  • 98k
  • 17
  • 219
  • 419

In many cases, the simple solutions are... simpler.

In this case, your solution of splitting the data on space , and locating the longest word, is accurate, but a simpler solution was probably intended. As an up-side, the simpler solution is actually better in terms of time-complexity (it is \$O(n)\$, which means the performance takes twice as long if the string is twice as long. Other solutions take more than twice as long if the input data is twice the size).

Bug: You have a bug in your string clean-up too, because you strip all punctuation, and replace it with the empty string "", you are vulnerable to input like This.is.a.long.string, and you will identify that as one word: Thisisalongstring. If you set the replacement as a space " ", you will be OK.

Note, this solution scans the data only once. Other solutions require that the data is scanned multiple times (once to split, once to sort).

function longestWord(str){
    str = str.replace(/[^A-Za-z\s]/g, " ");
    var maxoffset = 0, maxlen=0, wordoffset = 0;
    for (var i = 0; i < str.length; i++) {
        if (str[i] == " ") {
            wordoffset = i + 1;
        }
        if (i - wordoffset + 1> maxlen) {
            maxoffset = wordoffset;
            maxlen = i - wordoffset + 1;
        }
    }
    return str.substring(maxoffset, maxoffset + maxlen);
}

function textUpdated(source) {
    document.getElementById("outword").value = longestWord(source.value);
}
Enter text: <input type="Text" name="Input" id="data" onInput="textUpdated(this);"/>
<hr>
Longest Word: <input type="Text" id="outword" name="OutWord" value="none" />

In many cases, the simple solutions are... simpler.

In this case, your solution of splitting the data on space , and locating the longest word, is accurate, but a simpler solution was probably intended. As an up-side, the simpler solution is actually better in terms of time-complexity (it is \$O(n)\$, which means the performance takes twice as long if the string is twice as long. Other solutions take more than twice as long if the input data is twice the size).

Note, this solution scans the data only once. Other solutions require that the data is scanned multiple times (once to split, once to sort).

function longestWord(str){
    var maxoffset = 0, maxlen=0, wordoffset = 0;
    for (var i = 0; i < str.length; i++) {
        if (str[i] == " ") {
            wordoffset = i + 1;
        }
        if (i - wordoffset + 1> maxlen) {
            maxoffset = wordoffset;
            maxlen = i - wordoffset + 1;
        }
    }
    return str.substring(maxoffset, maxoffset + maxlen);
}

function textUpdated(source) {
    document.getElementById("outword").value = longestWord(source.value);
}
Enter text: <input type="Text" name="Input" id="data" onInput="textUpdated(this);"/>
<hr>
Longest Word: <input type="Text" id="outword" name="OutWord" value="none" />

In many cases, the simple solutions are... simpler.

In this case, your solution of splitting the data on space , and locating the longest word, is accurate, but a simpler solution was probably intended. As an up-side, the simpler solution is actually better in terms of time-complexity (it is \$O(n)\$, which means the performance takes twice as long if the string is twice as long. Other solutions take more than twice as long if the input data is twice the size).

Bug: You have a bug in your string clean-up too, because you strip all punctuation, and replace it with the empty string "", you are vulnerable to input like This.is.a.long.string, and you will identify that as one word: Thisisalongstring. If you set the replacement as a space " ", you will be OK.

Note, this solution scans the data only once. Other solutions require that the data is scanned multiple times (once to split, once to sort).

function longestWord(str){
    str = str.replace(/[^A-Za-z\s]/g, " ");
    var maxoffset = 0, maxlen=0, wordoffset = 0;
    for (var i = 0; i < str.length; i++) {
        if (str[i] == " ") {
            wordoffset = i + 1;
        }
        if (i - wordoffset + 1> maxlen) {
            maxoffset = wordoffset;
            maxlen = i - wordoffset + 1;
        }
    }
    return str.substring(maxoffset, maxoffset + maxlen);
}

function textUpdated(source) {
    document.getElementById("outword").value = longestWord(source.value);
}
Enter text: <input type="Text" name="Input" id="data" onInput="textUpdated(this);"/>
<hr>
Longest Word: <input type="Text" id="outword" name="OutWord" value="none" />

Source Link
rolfl
  • 98k
  • 17
  • 219
  • 419

In many cases, the simple solutions are... simpler.

In this case, your solution of splitting the data on space , and locating the longest word, is accurate, but a simpler solution was probably intended. As an up-side, the simpler solution is actually better in terms of time-complexity (it is \$O(n)\$, which means the performance takes twice as long if the string is twice as long. Other solutions take more than twice as long if the input data is twice the size).

Note, this solution scans the data only once. Other solutions require that the data is scanned multiple times (once to split, once to sort).

function longestWord(str){
    var maxoffset = 0, maxlen=0, wordoffset = 0;
    for (var i = 0; i < str.length; i++) {
        if (str[i] == " ") {
            wordoffset = i + 1;
        }
        if (i - wordoffset + 1> maxlen) {
            maxoffset = wordoffset;
            maxlen = i - wordoffset + 1;
        }
    }
    return str.substring(maxoffset, maxoffset + maxlen);
}

function textUpdated(source) {
    document.getElementById("outword").value = longestWord(source.value);
}
Enter text: <input type="Text" name="Input" id="data" onInput="textUpdated(this);"/>
<hr>
Longest Word: <input type="Text" id="outword" name="OutWord" value="none" />