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
4 Answers
Reset to default 6Change 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
评论列表(0条)