Home > Enterprise >  Finding both characters and putting them in a new array
Finding both characters and putting them in a new array

Time:01-08

I've created a function that passes in a string and a character. The string is saturday and the character is a.

I want the result to be an array that contains all the index numbers of where the letter 'a' sits in saturday.

Then array ends up with only [1]. So it finds the first a sitting at the second index. I tested this by changing saturday to soturday and the console prints the array with [6].

I want the result to be [1, 6]

I've tried putting the return result outside the next set of {} braces but no joy.

const subLength = (str, cha) => {
  let result = [];

  for (let i = 0; i < str.length; i  ) {

    if (str.charAt(i) === cha) {
      result.push(str.indexOf(cha));

      return result;
    }
  }

};
console.log(subLength('Saturday', 'a'));

CodePudding user response:

2 small problems with your code

  1. The return statement is in the for loop. The first time the loop hits that your loop will stop and the function will return. This is why you are only getting 1 result. Move the return outside the loop.

  2. Once the above is fixed you will realize that your array will now return [1, 1]. This is because str.indexOf(cha) will always return 1 since it's returning the index of the first a. To fix this, you should be appending the index i to your array instead since it represents the index of the current char.

const subLength = (str, cha) => {
  let result = [];

  for (let i = 0; i < str.length; i  ) {

    if (str.charAt(i) === cha) {
      result.push(i);
    }
  }

  return result;
};
console.log(subLength('Saturday', 'a'));

CodePudding user response:

You are pretty close.

In your code the return is being executed as soon as a match is found. You want to return after you've checked every char - so I've moved it after the foreach. indexof has a second parm, which defines the char to start looking from. If you omit it, you will get the index of the first match every time - which is not what you want.

const subLength = (str, cha) => {
  let result = [];
  
  for(let i = 0; i < str.length; i  ){
    if(str[i] === cha) {
      result.push(str.indexOf(cha, i));
    }
  }
  return result;
};
console.log(subLength('Saturday', 'a'));

Room for improvement

Since you are iterating over every char anyways, you can simply store every i where str[i] matches cha.

So optimized:

const subLength = (str, cha) => {
  let result = [];
  
  for(let i = 0; i < str.length; i  ){
    if(str[i] === cha) {
      result.push(i);
    }
  }
  return result;
};
console.log(subLength('Saturday', 'a'));

An even simpler version using regex:

const subLength = (str, cha) => {
  return [...str.matchAll(new RegExp(cha, 'g'))].map(e => e.index);
};
console.log(subLength('Saturday', 'a'));

CodePudding user response:

How about putting the return result; outside of the for loop?

CodePudding user response:

Something like this should work, if it is case-sensitive

const subLength = (str, cha) => {
    const chaArr = str.split('');
    const result = [];
    chaArr.forEach((v, i) => {
        if (v === cha) result.push(i);
    })
    return result

};
console.log(subLength('Saturday', 'a'));
  •  Tags:  
  • Related