Home > other >  Find how many times a char is included into an array of names
Find how many times a char is included into an array of names

Time:07-07

I'm trying to do the following exercise in JavaScript: I need to return the number of times the letter 'l' is used inside an array of names.

This is an example of the array I have to use:

["earth (c-137)","abadango","citadel of ricks","worldender's lair","anatomy park","interdimensional cable","immortality field resort","post-apocalyptic earth","purge planet","venzenulon 7","bepis 9","cronenberg earth","nuptia 4","giant's town","bird world","st. gloopy noops hospital","earth (5-126)","mr. goldenfold's dream","gromflom prime","earth (replacement dimension)","testicle monster dimension","signus 5 expanse","earth (c-500a)","rick's battery microverse","the menagerie","earth (k-83)","hideout planet","unity's planet","dorian 5","earth (unknown dimension)","earth (j19ζ7)","roy: a life well lived","eric stoltz mask earth","earth (evil rick's target dimension)","planet squanch","glaagablaaga","resort planet","interdimensional customs","galactic federation prison","gazorpazorp"]

Note that the letter 'l' might be repeted inside a single name.

Is there any JS method I could use to do this ? I've tried with indexOf but couldn't make it work

CodePudding user response:

Try this:

myStr.split('<my_char>').length - 1. 

This splits the string everytime you see the char you want and returns the number of strings after splitting at the delimiter minus 1 which is exactly what you want. Do this after converting the array of strings to a single string. myStr = myStrArray.toString();

CodePudding user response:

Here's a way using the array method forEach and a for...of loop

const stringArr = ["earth (c-137)","abadango","citadel of ricks","worldender's lair","anatomy park","interdimensional cable","immortality field resort","post-apocalyptic earth","purge planet","venzenulon 7","bepis 9","cronenberg earth","nuptia 4","giant's town","bird world","st. gloopy noops hospital","earth (5-126)","mr. goldenfold's dream","gromflom prime","earth (replacement dimension)","testicle monster dimension","signus 5 expanse","earth (c-500a)","rick's battery microverse","the menagerie","earth (k-83)","hideout planet","unity's planet","dorian 5","earth (unknown dimension)","earth (j19ζ7)","roy: a life well lived","eric stoltz mask earth","earth (evil rick's target dimension)","planet squanch","glaagablaaga","resort planet","interdimensional customs","galactic federation prison","gazorpazorp"]


let count = 0;

stringArr.forEach((string) => {
  for (let letter of string) {
    if (letter.toLowerCase() === "l") count  = 1;
  }
});

console.log(count);

CodePudding user response:

You can use a simple regex that matches the character that you want to match with while iterating over the array items.

const arrayToSearch = ["earth (c-137)", "abadango", "citadel of ricks", "worldender's lair", "anatomy park", "interdimensional cable", "immortality field resort", "post-apocalyptic earth", "purge planet", "venzenulon 7", "bepis 9", "cronenberg earth", "nuptia 4", "giant's town", "bird world", "st. gloopy noops hospital", "earth (5-126)", "mr. goldenfold's dream", "gromflom prime", "earth (replacement dimension)", "testicle monster dimension", "signus 5 expanse", "earth (c-500a)", "rick's battery microverse", "the menagerie", "earth (k-83)", "hideout planet", "unity's planet", "dorian 5", "earth (unknown dimension)", "earth (j19ζ7)", "roy: a life well lived", "eric stoltz mask earth", "earth (evil rick's target dimension)", "planet squanch", "glaagablaaga", "resort planet", "interdimensional customs", "galactic federation prison", "gazorpazorp"];

const REGEX_MATCH = /l/ig;

function getCount(array, regex) {
  const matchCount = array.reduce((memo, item) => {
    const matches = item.match(REGEX_MATCH);

    if (matches && matches.length > 0) {
      memo  = matches.length;
    }

    return memo;
  }, 0);

  return matchCount;
}

const count = getCount(arrayToSearch, REGEX_MATCH);

console.log(`count is ${count}`);

CodePudding user response:

const data = ["earth (c-137)", "abadango", "citadel of ricks", "worldender's lair", "anatomy park", "interdimensional cable", "immortality field resort", "post-apocalyptic earth", "purge planet", "venzenulon 7", "bepis 9", "cronenberg earth", "nuptia 4", "giant's town", "bird world", "st. gloopy noops hospital", "earth (5-126)", "mr. goldenfold's dream", "gromflom prime", "earth (replacement dimension)", "testicle monster dimension", "signus 5 expanse", "earth (c-500a)", "rick's battery microverse", "the menagerie", "earth (k-83)", "hideout planet", "unity's planet", "dorian 5", "earth (unknown dimension)", "earth (j19ζ7)", "roy: a life well lived", "eric stoltz mask earth", "earth (evil rick's target dimension)", "planet squanch", "glaagablaaga", "resort planet", "interdimensional customs", "galactic federation prison", "gazorpazorp"]

const howMany = (data, char) => data.join('').match(new RegExp(char, 'g')).length

console.log(howMany(data, 'p'))

CodePudding user response:

const stringArr = [
  'earth (c-137)', 'abadango', 'citadel of ricks', 'worldender\'s lair', 'anatomy park', 'interdimensional cable',
  'immortality field resort', 'post-apocalyptic earth', 'purge planet', 'venzenulon 7', 'bepis 9', 'cronenberg earth',
  'nuptia 4', 'giant\'s town', 'bird world', 'st. gloopy noops hospital', 'earth (5-126)', 'mr. goldenfold\'s dream',
  'gromflom prime', 'earth (replacement dimension)', 'testicle monster dimension', 'signus 5 expanse', 'earth (c-500a)',
  'rick\'s battery microverse', 'the menagerie', 'earth (k-83)', 'hideout planet', 'unity\'s planet', 'dorian 5',
  'earth (unknown dimension)', 'earth (j19ζ7)', 'roy: a life well lived', 'eric stoltz mask earth',
  'earth (evil rick\'s target dimension)', 'planet squanch', 'glaagablaaga', 'resort planet',
  'interdimensional customs', 'galactic federation prison', 'gazorpazorp'
]

const result = stringArr
  .map(item => item.split('l').length - 1)
  .reduce((acc, value) => acc   value, 0)

console.log(result)

  • Related