subLength(). JavaScript Practice: Arrays, Loops, Objects, Iterators

Hello to everybody!
I got stuck with 2th exercise in JavaScript Practice: Arrays, Loops, Objects, Iterators. I wrote a function, it works pretty well on tests, but CodeCademy doesn’t pass my answer.
What can be wrong with my code? Thank you in advance for reply :slight_smile:

const subLength = (str,char) => {
  let pattern = new RegExp(char,'g');
  let checker = str.match(pattern).length;
  if(checker !== 2){
    return 0;
  }else{
    let arr = str.split('');
    let indexAll = arr
    .map((elem,i) => elem === char ? i : -1)
    .filter(index => index !== -1);
    return indexAll.reduce((a,b) => Math.abs(a-b)+1);
  }
};

SOLVED!
Here is my updated way to solve this task:

const subLength = (str,char) => {
  let pattern = new RegExp(char,'g');
  let reg = str.match(pattern);
  if(reg !== null){
    let checker = reg.length;
    if(checker !== 2){
      return 0;
    }else{
      let arr = str.split('');
      let indexAll = arr
      .map((elem,i) => elem === char ? i : -1)
      .filter(index => index !== -1);
      return indexAll.reduce((a,b) => Math.abs(a-b)+1);
     }
  }else{
    return 0;
  }
};
1 Like

Hello, and welcome to the forums!

The interactive article tests for more than just the examples they provided in the challenge description. One example they didn’t provide and you didn’t account for is if there are no occurrences of the single character in the string.

Try your code with:

subLength('anastasiamazhid', 'e');

Oh, thanks! This is the key :slight_smile:

1 Like

Hi, i am also having trouble getting my code verified by the check answer system on this exercise. What is going wrong? All my tests are working, but i am getting ’ Double check your code. It likely has a syntax error.’ as a response. Here is my code:

const subLength = (string, char) => {
let stringLower = string.toLowerCase();
let splitString = stringLower.split("");
console.log(splitString);

let charCount = 0;
for (i = 0; i < splitString.length; i++)
if (splitString[i] === char) {charCount++}
console.log(charCount);

if (charCount < 2) {return 0}
if (charCount === 2) {
let c1 = splitString.indexOf(char);
let c2 = splitString.lastIndexOf(char);
console.log(c1, c2)
let answer = (c2 - c1);
return answer += 1;
}
if (charCount > 2) {return 0}
}

console.log(subLength(‘summer’, ‘m’));

Hello :slightly_smiling_face:

I tried to use your code and slightly edited it, but mine version doesn’t work too. Here is my code, may be it could help you find best solution:

const subLength = (string, char) => {
	let stringLower = string.toLowerCase();
	let splitString = stringLower.split("");
	let charCount = 0;
	for (i = 0; i < splitString.length; i++){
		if (splitString[i] === char) {
			charCount++;
		}	
	}
	if (charCount === 2) {
		let c1 = splitString.indexOf(char);
		let c2 = splitString.lastIndexOf(char);
		return Math.abs(c2 - c1)+1;
	}else{
		return 0;
	}
}

Hi :slight_smile:.

New to the forums … I have a similar but different issue: my code works but isn’t accepted. I didn’t want to create a new post so will post my code here:

function subLength(str, char) {
    let firstInd = 0;
    let secondInd = 0;
    let thirdInd = 0;

    firstInd = str.indexOf(char);
    secondInd = str.indexOf(char, firstInd + 1);
    thirdInd = str.indexOf(char, secondInd + 1);

    if (firstInd === -1 || secondInd === -1 || thirdInd > -1) {
        return 0;
    } else {
        return secondInd - firstInd + 1;
    }   
};

This works for me and I believe is correct however the strict testing fails it for a reason I’m not seeing. I can see that the successful submissions use a for loop however I thought it would be quicker to slice the string.

I suspect the issue may be the if condition on line 10 (if (firstInd === -1 || secondInd === -1 || thirdInd > -1)) however I can’t see any issues with the logic so hoping for a second pair of eyes.

Any thoughts?

Many thanks.

SOLVED!

I managed to resolve the issue: though my OP shows the thirdInd deceleration ( let thirdInd = 0; ), it was missing from the code I’d tested. Once I added it the code was passed.

Comments / peer review welcome!

The full successful submission was:

function subLength(str, char) {
    let firstInd = 0;
    let secondInd = 0;
    let thirdInd = 0;

    firstInd = str.indexOf(char);
    secondInd = str.indexOf(char, firstInd + 1);
    thirdInd = str.indexOf(char, secondInd + 1);

    if (firstInd === -1 || secondInd === -1 || thirdInd > -1) {
        return 0;
    } else {
        return secondInd - firstInd + 1;
    }   
};

I am having the same issue. All my codes in this section work but I keep getting “Double check your code. It likely has a syntax error.” If it has a syntax error, why am I not getting a red Syntax Error from the compiler?

1 Like

Make sure that all your variables are being declared with let, const, or var (though let and const are preferred).

I’ve found that’s a common problem people run into with the interactive article code checkers, even if the code runs fine otherwise.

1 Like

That was absolutely it! Thank you. In my for loops, I did not declare the index, i with ‘let’. Once I put that in, the Codecademy accepted it:

for (let i = 0; i < numOfItems - 2; i++){
str = str + grocObjArr[i].item + ', ';
}

1 Like