简体   繁体   English

返回字符串中最长单词的长度(JavaScript)

[英]Return length of longest word in string (Javascript)

Desired outcome is a the length of the longest word in a given string (numerical). 期望的结果是给定字符串中最长单词的长度(数字)。

Ex: findLongestWord('I like tacos'); 例如:findLongestWord('I like tacos'); => 5 => 5

I get the 'cannot read length of undefined' error message when I start the loop from the end, and I only get the first new value of 'longest' when I start the loop forward. 从结尾开始循环时,出现“无法读取未定义的长度”错误消息,而在向前循环时,我仅得到第一个新值“最长”。 What am I missing? 我想念什么?

function findLongestWord(string) {
  var longest = 0;
  var array = string.split(' ');
  for (var i = array.length; i >= 0; i--) {
    if (array[i].length > longest) {
        longest = array[i].length;
    }
  }
  return longest;
}

change your for to for (var i = array.length - 1; i >= 0; i--) { 将for更改为for (var i = array.length - 1; i >= 0; i--) {

if there 4 elements in array index start from 0 to 3 hence in that case array[4] is undefined 如果数组索引中有4元素从03开始,则在这种情况下array[4] undefined

Your for loop first runs on i index which points to array.length that you're trying to access with the value of undefined . 您的for循环首先在i索引上运行,该索引指向您要尝试使用undefined值访问的array.length

Taken from MDN , Array length returns a 32-bit integer that is always numerically gerater than the highest index of the array. MDN中获取 ,数组长度返回一个32位整数,该整数在数值上总是比数组的最高索引大。

In short, array.length is zero-based index. 简而言之, array.length是从零开始的索引。

So modifying your code to this will do, 因此修改您的代码即可

function findLongestWord(string) {
  var longest = 0;
  var array = string.split(' ');
  for (var i = array.length - 1; i >= 0; i--) {
    if (array[i].length > longest) {
        longest = array[i].length;
    }
  }
  return longest;
}

But, here's another way you can achieve this... 但是,这是您可以实现这一目标的另一种方式...

function findLongestWord(str){
  var longestWord = '';
  str.split(' ').forEach(function (v) {
    longestWord = v.length > longestWord.length ? v : longestWord
  });
  return longestWord;
}

var word = findLongestWord('I like tacos');
console.log(longestWord, longestWord.length);
// tacos 5

array.length returns ,n, the number of elements in the array and when we fetch elements from the array using index it is from 0 to n-1. array.length返回n,数组中的元素数,当我们使用索引从数组中获取元素时,它的范围是0到n-1。 So you will have to use array.length -1 instead of array.length. 因此,您将必须使用array.length -1而不是array.length。

Using .sort will be easy .When you sorted array in DESC order ,get longest string by just index 0. 使用.sort会很容易。当您按DESC顺序对数组进行排序时,仅通过索引0获得最长的字符串。

function findLongestWord(string) {       
  var array = string.split(' ');
  longest = array.sort(function(a,b){
      return a.length < b.length;
  });
  return longest[0].length;
  }

Thanks everybody for the great suggestions! 谢谢大家的宝贵建议! I now see there are cleaner/shorter ways to handle this toy problem but I went with adding the 'length -1' to my loop, as that's the closest to what I had come up on my own. 我现在看到有更清洁/更短的方法来处理此玩具问题,但是我在循环中添加了“长度-1”,因为这是我自己尝试的最接近的方法。

Thanks! 谢谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM