这是我的片段。我觉得这段代码满足算法,但它没有通过。可能的原因是什么?
function findLongestWord(str, separator) {
var splitString = str.split(separator);
for (var i = 1; i <= splitString.length; i++) {
for (j = 1; j < splitString[i].length; j++) {
while (j === ' ') {
return j;
}
}
}
var greater;
if (j > greater) {
greater = j;
}
return greater;
}
findLongestWord("The quick brown fox jumped over the lazy dog", ' ');
答案 0 :(得分:3)
以下是您的代码,其中包含解释和一些更改以使其正常工作
function findLongestWord(str, separator) {
var splitString = str.split(separator);
// Define it as empty string
var greater = '';
// Start looping from 0th index upto the length
for (var i = 0; i < splitString.length; i++) {
for (j = 0; j < splitString[i].length; j++) {
// Don't know what this is
// `j` is the index, i.e. number, it cannot be equal to ` `
// I guess this is confused with the separator
// This can be removed without any problem
while (j === ' ') {
return j;
}
// No use code
}
// Here `j` is the length of the word
// Compare it with the length of greater word
if (j > greater.length) {
// Update the greater to current `i` string
greater = splitString[i];
}
}
// Return greater string.
return greater;
}
var longestWord = findLongestWord("The quick brown fox jumped over the lazy dog", ' ');
document.body.innerHTML = 'Longest Word: ' + longestWord;
little 优化的相同代码可以重写为
function findLongestWord(str, separator) {
var splitString = str.split(separator);
// Define it as empty string
var greater = '';
for (var i = 0, len = splitString.length; i < len; i++) {
if (splitString[i].length > greater.length) {
// Update the greater to current `i` string
greater = splitString[i];
}
}
return greater;
}
var longestWord = findLongestWord("The quick brown fox jumped over the lazy dog", ' ');
document.body.innerHTML = 'Longest Word: ' + longestWord;
console.log(longestWord);
使用ES6的解决方案
您可以使用Array#reduce
和Arrow functions。对于ES5中的相同代码,请this answer
str.split(' ').reduce((x, y) => x.length > y.length ? x : y);
function findLongestWord(str, separator) {
return str.split(' ').reduce((x, y) => x.length > y.length ? x : y);
}
var longestWord = findLongestWord("The quick brown fox jumped over the lazy dog", ' ');
document.body.innerHTML = 'Longest Word: ' + longestWord;
console.log(longestWord);
答案 1 :(得分:2)
试试这个fiddle
function findLongestWord(str, separator) {
return str.split(separator).reduce(function (previousValue, currentValue) {
return previousValue.length < currentValue.length ? currentValue : previousValue;
});
}
var longestWord = findLongestWord("The quick brown fox jumped over the lazy dog", ' ');
console.log(longestWord);
document.body.innerHTML = longestWord;
答案 2 :(得分:1)
请试试这个:
function findLongestWord(str, separator) {
var splitString = str.split(separator);
var greater = splitString[0];
for (var i = 1; i < splitString.length; i++) {
if(greater.length < splitString[i].length){
greater = splitString[i]
}
}
return greater;
}
findLongestWord("The quick brown fox jumped over the lazy dog", ' ');
答案 3 :(得分:0)
Javascript索引从0开始到(length-1)。在“separator”参数上调用split方法后,单词将存储在 splitString 中,并从0到(length-1)索引。为了找到最长的单词,在开始循环之前,我们可以安全地假设第一个单词是最长的。然后我们可以迭代这些单词来检查是否有任何一个比这长。如果我们找到一个,我们可以更新我们最长的单词。你的内部循环是多余的,代码可以这样写 -
function findLongestWord(str, separator) {
var splitString = str.split(separator);
var greater = splitString[0];
for (var i = 1; i < splitString.length; i++) {
if(greater.length < splitString[i].length)
{
greater = splitString[i];
}
}
return greater;
}
findLongestWord("The quick brown fox jumped over the lazy dog", ' ');
N.B。:如果有多个长度最长的单词,使用此算法,您将始终找到第一个从左向右搜索的单词。如果你想要最后一个改变,如果循环中的条件如下 -
if(greater.length <= splitString[i].length)