从句子中获取一个包含长度单词的数组-javascript

时间:2017-06-20 21:59:55

标签: javascript

我试图创建一个函数,告诉我句子中最长的单词有多长。我的方法是将句子分成单词串。我现在有一个字符串数组。我的问题是我想使用这个数组来获得另一个数字数组,即每个单词的长度。我该怎么做呢?我的代码如下,但我一直都是空的。

function findLongestWord(str) {
  var split = str.split(" ");
  for (j = 0; j < split.length; j++)
  var wordCount = split[j].length;
  var lengths = [];
  for (var i = 0; i < wordCount.length; i++) {
    lengths.push(i);
  }

  return Math.max(...lengths);
}

4 个答案:

答案 0 :(得分:0)

如果您要遍历所有单词,则可以在输入数组中找到最长(最长)单词。

<div class="wrap">
  <p class='p'>this is a simple paragraph that is meant to be nice and easy to type which</p>
  <span>test</span>
</div>
<div class="wrap">
  <p class='p'>this is a simple paragraph that is meant to be nice and easy to type whichthis is a simple paragraph that is meant to be nice and easy to type which </p>
  <span>test</span>
</div>
<div class="wrap">
  <p class='p'>this is a simple paragraph that is h</p>
  <span>test</span>
</div>
<div class="wrap">
  <p class='p'>this is a simple paragraph that is meant to be nice and this is a simple paragraph that is meant to be nice and easy to type whichh</p>
  <span>test</span>
</div>

返回值<ext:SplitButton runat="server" ID="btnExcel" Icon="PageExcel" ToolTip="Something Text" Cls="ReportesButton" OverCls="ReportesButtonOver"> <Bin> <ext:Menu runat="server" ID="Menu3"> <Items> <ext:CheckMenuItem ID="chkbox" runat="server" Text="Something Text" Checked="false"> <Listeners> <Click Handler="#{btnExcel}.bin[0].hide();"></Click> </Listeners> </ext:CheckMenuItem> </Items> </ext:Menu> </Bin> <Listeners> <ArrowClick Handler="this.bin[0].show(); this.bin[0].alignTo(this.el);"></ArrowClick> </Listeners> </ext:SplitButton> 以获取长度(或function findLongestWord(str) { var split = str.split(" "); var maxLength = 0; var longestWord = ""; // If no word is found "".length will return 0 var len = split.length; for (j = 0; j < len; j++) { if (split[j].length > maxLength) { longestWord = split[j]; maxLength = split[j].length; } } return longestWord; } 如果您愿意)。

注意根据您的应用,标点符号可能会干扰您的算法。

答案 1 :(得分:0)

我对你的代码中的错误做了一些评论

function findLongestWord(str) {

  // better use .split(/\s+/) instead to remove trailing space in the middle of sentence
  var split = str.split(" ");
  
  // this for loop is redundant, you have to wrap the code that you want to loop with curly brackets.
  for (j = 0; j < split.length; j++)
  
  // the value of j would be the length of split array.
  var wordCount = split[j].length;
  var lengths = [];
  
  // since wordCount.length is undefined, so loop never gets excuted and your lengths array would be empty. 
  for (var i = 0; i < wordCount.length; i++) {
    lengths.push(i);
  }
  
  // doing Math.max on empty array will return -Infinity
  return Math.max(...lengths);
}

findLongestWord('hello there mate')

以下是我的解决方案。还有更多方法可以做你想做的事。

function findLongestWord(str) {
  // trim trailing white space.
  var split = str.trim().split(/\s+/);
  var lengths = [];
  
  // loop through array of words
  for (j = 0; j < split.length; j++) {
  
    // check the length of current words
    var wordCount = split[j].length;
    lengths.push(wordCount);
  }
  
  return Math.max(...lengths);
}

const sentence = 'hello its a me mariooooooo';

console.log(findLongestWord(sentence))

// one liner - using reduce function
const findLongestWord2 = (str) => str.trim().split(/\s+/).reduce((a, b) => a.length > b.length ? a.length : b.length, -Infinity);

console.log(findLongestWord2(sentence))

// less efficient but shorter - using sort
const findLongestWord3 = (str) => str.trim().split(/\s+/).sort((a, b) => a.length - b.length).pop().length;

console.log(findLongestWord3(sentence))

答案 2 :(得分:-1)

你需要使用var在第一个for循环中创建j,就像你在第二个循环中使用i一样。

答案 3 :(得分:-1)

这可以使用.map()方法完成。您将字符串数组映射为字长数组,然后返回长度数组的Math.max(),如下所示:

function findLongestWord(str) {
  // map words into array of each word's length, grab highest #
  return Math.max(...str.split(" ").map(str => str.length));
}

console.log(findLongestWord("The quick brown fox jumped over the lazy dog"));