简体   繁体   中英

Comparing .length values of strings in a dynamic table in JavaScript

I am still new to JavaScript so bear with me... I have a series of sentences... Each word needs split into an array, the length of each word converted to a numeric value, and the values compared to the numeric value of the other words in the sentence to determine the larges number of character and should return that number

So far I have:

 function findLongestWord(str) { var a = str.split(" "); //array for each word in str var b = a.length - 1; //number of cells in array a var c = 0; //counter for number of itterations var d = []; //array to hold the numberic length of each word per cell var e ; //compares cells and returns highest numberic value var f = []; //just in case it is needed for ( a ; c < b ; c++) { //while c is less than b run code and add 1 to c d[c].push(a[c].length) ; //should push the value of the length of a[c] into d[] } for (c = 0 ; d[c] < d.length ; c++) { e = [d[c]].Math.max();//should return the larges value in d[] } return e; } findLongestWord("The quick brown fox jumped over the lazy dog"); 

For example in the sentence above the longest word is 'jumped' and should return a value of 6... I have been working on this for hours and trying to find the correct code... at one point the code returned a '1','3',or '19' which the '19' passed one of the sentences but not the others... now I am either getting blank output or var.push() undefined....

function findLongestWord(str) {
  var words = str.split(" "),
      word_lengths = [];

  for (var i=0; i < words.length - 1; i++) {
    word_lengths[i] = words[i].length;
  }

  return Math.max.apply(null, word_lengths);
}


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

Split the str into words and then use Math max to find the longest.

function getLongest(str)
    var maxSize = 0;
    str.replace(/\w+/g, word => maxSize = Math.max(word.length, maxSize));
    return maxSize;
}

You get d[c] is undefined",... error when you run your code because d is an empty array. So d[0] is not defined and you cannot invoke push() on undefined . Your code can work with a few corrections.

 function findLongestWord(str) { var a = str.split(" "); //array for each word in str var b = a.length; //number of cells in array a var d = []; //array to hold the numberic length of each word per cell while(c < b) { d.push(a[c].length) ; //should push the value of the length of a[c] into d[] c++; } return Math.max(...d); } var longest = findLongestWord("The quick brown fox jumped over the lazy dog"); console.log(longest); 

  • I also replaced the first for loop with a while construct.
  • There is not need for b to be equal to a.length-1. It should be equal to a.length because that's the array length you want to iterate over.
  • For the second loop (finding the maximum value in array), you can use spread operator . But if you want to do it with a for loop, you should do something along the lines of:

     var max=d[0]; for(var i=1; i<d.length; i++) if(d[i]>max) max=d[i]; 

In general, you can define the loop variable inside the for loop and you don't need to declare it. Also, I don't think it is good practice to create variables just in case .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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