簡體   English   中英

在字符串中找到最長的單詞不起作用

[英]Finding the longest word in a string is not working

我應該在字符串中找到最長的單詞,這是我到目前為止提出的代碼。 不幸的是,這似乎不起作用,我的問題是為什么?

function findLongestWordLength(str) { 
  str.split("");
  let longest = 1;
  for(let i = 0; i < str.length; i++){
    if (str[i].length > longest){
       longest = str[i].length;
    }
  }
  return longest;
}

如果我沒有正確理解,則有兩個主要問題:

1)您沒有在任何地方存儲String.split()的結果。

2)如果需要拆分不同的單詞,則需要按space拆分

我還將以longest = 0而不是1

例:

 function findLongestWordLength(str) { str = str.split(" "); let longest = 0; for (let i = 0; i < str.length; i++) { if (str[i].length > longest) longest = str[i].length; } return longest; } console.log(findLongestWordLength("Hello World")); console.log(findLongestWordLength("")); console.log(findLongestWordLength("123 1234567 12345")); 
 .as-console {background-color:black !important; color:lime;} .as-console-wrapper {max-height:100% !important; top:0;} 

另外,您可以使用Array.map()將每個單詞map到其長度,然后在Math.max()spread此長度數組以得到所需的結果:

 function findLongestWordLength(str) { let wordLengths = str.split(" ").map(word => word.length); return Math.max(...wordLengths); } console.log(findLongestWordLength("Hello World")); console.log(findLongestWordLength("")); console.log(findLongestWordLength("123 1234567 12345")); 
 .as-console {background-color:black !important; color:lime;} .as-console-wrapper {max-height:100% !important; top:0;} 

問題是第二行需要更改為str = str.split(" "); 由於字符串是不可變的,因此無法更改,因此需要重新分配。

 function findLongestWordLength(str) { str = str.split(" "); let longest = 1; console.log(str); for(let i = 0; i < str.length; i++){ if (str[i].length > longest){ longest = str[i].length; } } return longest; } var result = findLongestWordLength("Joan Ala Valeron") console.log(result); 

您需要用" "分隔字符串。 然后遍歷單詞並返回最大的長度。

 function findLongestWordLength(str) { const words = str.split(" "); return words.reduce( (max, word) => (word.length > max ? word.length : max), 0 ); } console.log(findLongestWordLength("hello world")); 

由於使用reduce因此該解決方案更短,更清潔。

您可以嘗試此代碼。

function findLongestWordLength(str) {
  var strSplit = str.split(' ');
  var longest = 0;
  for(var i = 0; i < strSplit.length; i++){
    if(strSplit[i].length > longest){
    longest = strSplit[i].length;
     }
  }
  return longest;
}
findLongestWordLength("The quick brown fox jumped over the lazy dog");
function findLongestWordLength(str) { 
  var otherStr = str.split(" ");
  let longest = 0;
  for(let i = 1; i < otherStr.length; i++){
    if (otherStr[i].length > otherStr[longest].length){
       longest = i;
    }
  }
  return otherStr[longest];
}

findLongestWordLength("This is the String Data")

這是行不通的,因為您正在遍歷長度為1的字符串中的每個字符。您沒有比較單詞的長度。

function findLongestWordLength(str) {
    let words = str.split(" ");  // Delemiter to separate words from sentence is space
    let longest = 1;
    for(let i = 0; i < words.length; i++){
    if (words[i].length > longest){
       longest = words[i].length;
    }
  }
  return longest;
}

 function spacer(m) { var space = ""; for(var inst = 0; inst < m;inst++) space += "\\n"; console.log(space); } function findLongestWordLength(str) { //Checking that what you are sending is a string... if(typeof str !== "string") throw "This function requires you to pass in a string..."; //Ghetto Class...lol function CreateWord(word) { this.word = word; this.length = word.length; } // Getting all the words...but taking out the words that are "" var AllWords = str.split(" ").filter(word => word != ""), // This is how many words you have that are not "" wordCount = AllWords.length, // Defaulting the longest word to the first word... longest = new CreateWord(AllWords[0]); // if we only have one, we return the one word... if(wordCount === 1) return longest; for(let i = 0; i < wordCount; i++){ if (AllWords[i].length > longest.length){ longest = new CreateWord(AllWords[i]); } } return longest; } //Simple Test... var Implementation = findLongestWordLength("ONE TWO THREE FOUR TESTINGLONGWORD"); console.log(Implementation); console.log(`Word: ${Implementation.word}`); console.log(`Length: ${Implementation.length}`); spacer(3); //Single word... var Implementation2 = findLongestWordLength("Only-One-Word"); console.log(Implementation2); console.log(`Word: ${Implementation2.word}`); console.log(`Length: ${Implementation2.length}`); spacer(3); //Exception...because I dont want want you to misUse this function.... var Implementation3 = findLongestWordLength(null); console.log(Implementation3); console.log(`Word: ${Implementation3.word}`); console.log(`Length: ${Implementation3.length}`); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM