javascript - Splitting sentence into array of words - Stack Overflow

folks, I'm trying to find out what word is the longest in an entered sentence but the code is not

folks, I'm trying to find out what word is the longest in an entered sentence but the code is not outputting anything. Could smb please help me with that?

<!DOCTYPE html>
<html>
<head lang = "en">
    <meta charset = "UTF-8">
    <title>LongestWord</title>
</head>
<body>
<script language = "Javascript" type = "text/javascript">
    var sentence = prompt("Enter sentence: ");
    var splitted = sentence.split("\\s+");
    var longestWord = splitted[0];

    for(var i = 0; i < splitted.length; i++){
        for(var j = 0; j < splitted[i].length - 1; j++){
            if(longestWord.length < splitted[i].length){
                longestWord = splitted[i];
            }

        }
    }
    document.write("The longest word in the sentence is " + longestWord);  
</script>
</body>
</html>

folks, I'm trying to find out what word is the longest in an entered sentence but the code is not outputting anything. Could smb please help me with that?

<!DOCTYPE html>
<html>
<head lang = "en">
    <meta charset = "UTF-8">
    <title>LongestWord</title>
</head>
<body>
<script language = "Javascript" type = "text/javascript">
    var sentence = prompt("Enter sentence: ");
    var splitted = sentence.split("\\s+");
    var longestWord = splitted[0];

    for(var i = 0; i < splitted.length; i++){
        for(var j = 0; j < splitted[i].length - 1; j++){
            if(longestWord.length < splitted[i].length){
                longestWord = splitted[i];
            }

        }
    }
    document.write("The longest word in the sentence is " + longestWord);  
</script>
</body>
</html>
Share edited Jun 28, 2015 at 22:42 PM 77-1 13.4k21 gold badges71 silver badges115 bronze badges asked Jun 28, 2015 at 22:35 user4833678user4833678 2
  • It should be var i = 1 instead since you already taken splitted[0] in variable. – Rahul Commented Jun 28, 2015 at 22:39
  • look here stackoverflow./questions/12216758/… – banksy Commented Jun 28, 2015 at 22:48
Add a ment  | 

4 Answers 4

Reset to default 6

Change your regexp code to:

var splitted = sentence.split(/\s+/);

EDIT: below is a slightly different take on the function:

function longestWord(str) {
    return str.split(/\s+/).sort(function(w1, w2) {return w2.length - w1.length;})[0];    
}

var phrase = "dmitriy nesterkin drd";
console.log(longestWord(phrase));
var length = 0;
var longestWord = "";
sentence.split(" ").forEach(function(word){
  if(word.length > length) {
    length = word.length;
    longestWord = word;
  }
});
document.write("The longest word in the sentence is " + longestWord);

Here's a simplified version, using split and reduce. First spotted word prevails.

var longestWord = sentence.split(' ').reduce(function(longestWord, word){
  return word.length > longestWord.length ? word : longestWord;
}, '');

You could make a pare function for the sort() function easily. This way you can check if there is more than one word that matches the length of the longest word, by popping the resulting sorted array.

function pare_words(a, b) { return a.length - b.length; }

var words = ["Hello", "here", "are", "some", "words", "of", "variable", "length"];

console.log("Longest word is: " + words.sort(pare_words).pop());

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742290546a4416103.html

相关推荐

  • javascript - Splitting sentence into array of words - Stack Overflow

    folks, I'm trying to find out what word is the longest in an entered sentence but the code is not

    18小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信