My code is doing ok but Codecademy pop up msg saying it is not ok

Hi All learners and teachers!
Is there anyone knows what is it?

If it works fine will it be a normal practice? to do so?

On “”"Front end Engineer >> Learn JavaScript Syntax: Array, loops … >> JavaScript Practice: Arrays, Loops, Objects, Iterators >> Practice intermediate JavaScript concepts with these 3 code challenges.

SECOND CODE CHALLENGE:

I made this code but “CHECK ANSWER - click” - Tested subLength('funny', 'n') and it did not return 2 .

 const subLength = (str, char) => {
  let index = str.indexOf(char)
  let indexTwo = str.indexOf(char, (index + 1)) + 1;
  let length = indexTwo - index;
  let nChar = str.split(char).length - 1
  if (nChar < 2 || nChar > 2){
    return 0;
    } else {
      return console.log(length);
    }
};

subLength('funny', 'n'); // however it returns 2 and other given calls are correctly returning

Hi. The problem is that your function subLength does not actually return 2. It logs 2. And returns a console. Just return the variable length and log the function call.
Other than that your approach looks fine to me.

For future posts, please don’t forget to post a link to the exercise so people can read the instructions.

3 Likes

Thank you so much! That really worked and let me complete my thoughts about this issue.
After your comment and checking the code It was really returning log but not the variable. And I didn’t thought about this earlier.
And you @mirja_t have my appreciation ( I logged in my mind console.log("Genius! ".repeat(3)); for u
That was a very good note for me, will think about this part in the future properly.

1 Like