简体   繁体   中英

having an issue w/ .split in a JavaScript function that gets the max # of repeating chars per word in a string

I'm writing a JavaScript function that has to take in a string argument & determine the word or words with the maximum number or repeated (or most frequent) non sequential characters and return that word or words.

The way that I went about solving this problem was to first find the maximum number of times a character was repeated per word and record that number to use later in a function to test against every word in the string (or the array of strings as I later split it); if the word met the conditions, it's pushed into an array that I return.

My maxCount function seemed to work fine on its own but when I try to make it work together with my other function to get the words with max repeated chars returned, it's not working in JS Fiddle - it keeps telling me that "string.split is not a function" - I'll admit that the way I'm using it ( string.split(string[i]).length ) to analyze words in the string letter by letter is a bit unconventional - I hope there's some way to salvage some of my logic to make this work in the functions that can work together to get the results that I want.

Also, I don't know if I'm using Math.max correctly/in a "legal" way, I hope so. I've tried switching my variable name to "string" thinking that would make a difference but it did not even though my arguments are of the string variety and it's a string that's being represented.

Here's a link to my Fiddle: https://jsfiddle.net/Tamara6666/rdwxqoh6/

Here's my code:

var maxCount = function (word) {
  /// var maxRepeats = 0;
  var numArray = [];
  var string = word;
  for (var i = 0, len = string.length; i < len; i++) {
  //split the word('string') into letters at the index of i
    numArray.push((string.split(string[i]).length) -1);
  }
  var max = Math.max(...numArray);
  return max;
}
///console.log(maxCount("xxxxxxxxxxxxx"));

var LetterCount = function(string){
  var repeatedChars = 0;
  var wordArray=[];
  var stringArray = string.split(" ");
  for (var i = 0; i < stringArray.length; i++){
  var eachWord = stringArray[i];
  var maxRepeats = maxCount(stringArray);

  if (repeatedChars < maxRepeats) {
    repeatedChars = maxRepeats;
    wordArray = [eachWord];
  }else if (repeatedChars == maxRepeats) {
    wordArray.push(eachWord);
  }
}
return wordArray;  
};

console.log(LetterCount("I attribute my success to cats"));
//should return ["attribute", "success"]

*** I've tried to map this first function onto the array formed when I split my string at the spaces but it is just returned me an empty array (I also might not have been using map correctly in this example); I also have tried using valueOf to extract the primitive value out of the array from the first function which also didn't work. I'm not really sure what to do at this point or what angle to take- I feel if I understood more what was going wrong I could more easily go about fixing it. Any help would be much appreciated. Thanks!

You are passing an array to maxCount at line 20, while it expects a string:

var maxRepeats = maxCount(stringArray);

You should use:

var maxRepeats = maxCount(eachWord);

If you are getting split is not a function error then first make sure that your string isn't null by printing it on console. If it isn't null then confirm that its a string not an array or some other thing.

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