JavaScript Practice: function subLength() wrong answer but code seems to be working

Hi this is my first question on here and there are loads of instructions on how to ask so bear with me if I don’t get it all right first time :slight_smile:

I’m doing the code challenges in the section JavaScript Practice: Arrays, Loops, Objects, Iterators and finished no 2, subLength(). My code seems to be working but it is nothing like the correct answer. I can’t see what’s wrong with my code but there must be something there so thought I’d check if someone can help me spot it. This is my code:

// Write function below
function subLength(string, char) {
  let occurrenceOfChar = 0;
  for (i = 0; i < string.length; i++) {
    if (string[i] === char) {
  if (occurrenceOfChar === 2) {
    let first = string.indexOf(char);
    let last = string.lastIndexOf(char)
    return last - first +1; 
  } else {
    return 0;
console.log(subLength('cheesecake', 'k'));
//returns 0 in this case

Link to exercise:

I have read a couple of other topics on here related to the same challenge but as my code is different to those examples it didn’t answer my question.
Thank you!


For algorithm problems it’s not unusual for there to be multiple answers and within those answers there are levels of more or less optimal solutions (dealing with space and time complexity). Sometimes the solutions are just as good and just use a different method.

Your answer seems to be fine in terms of meeting the specs. It covers all 3 three scenarios, and I think it should cover for edge cases unless I’m missing something.

There could some sort of (implicit?) stipulation to avoid using specialized methods like lastIndexOf for practice. But I really don’t think so. Potentially a bug in the validation.

1 Like

Mine is a mess ( :grimacing::grimacing: :grimacing:) but also seems to be working, although am also getting errors from the automated checker:

const subLength = (str, char) => {
  let index1 = 0;
  let index2 = 0;
var lastFound = str.indexOf(char);
let counter = 0;
while (lastFound !== -1){
  //console.log("found "+ char + " at " + lastFound);
  if (index1 === 0){
    index1 = lastFound;
  else if (index2===0){
    index2 = lastFound;
lastFound = str.indexOf(char,lastFound+1);
if (index1 >=1 && index2 >=1 && counter<=2){
else {console.log(0);}

subLength('Saturday', 'a');  //returns 6
subLength('summer', 'm');   //returns 2
subLength('digitize', 'i');  //returns 0
subLength('cheesecake', 'k');  //returns 0
subLength('funny','n');  //returns 2

The autochecker actually gives me this error message: Tested subLength('funny', 'n') and it did not return 2 …but it totally does. Thoughts?

you have console.log, but you forgot the return statements in your functions

you need to put return index2-index1+1; after console.log(index2-index1+1);
and return 0; after console.log(0);

1 Like

Thank you for sharing your code! I agree with the reply you got about the return statement but I’m not sure I would have been able to spot it myself. It seems there are more than one way of coming to the right result so I will move on to the next challenge and hope for to get that one exactly right :slight_smile:

dear god. so i just misread the goal of the project clearly