Tested subLength('funny', 'n') and it did not return 2

hello, can anyone tell me what’s wrong with my code:
i get the right answers, but it says " Tested subLength('funny', 'n') and it did not return 2 ."
yet when i test subLength(‘funny’, ‘n’) i get 2.
// Write function below

const subLength = (str, charact) =>{
const indexOfFirst = str.indexOf(charact);
const indexOfLast = str.lastIndexOf(charact);
let result = indexOfLast - indexOfFirst + 1;

let count = 0;
let position = str.indexOf(charact);

while (position !== -1) {
count++;
position = str.indexOf(charact, position + 1)
};
if(count> 2 || count < 2){
result = 0;
};
console.log(result);
};

subLength(‘Saturday’, ‘a’);
subLength(‘summer’, ‘m’);
subLength(‘digitize’, ‘i’);
subLength(‘cheesecake’, ‘k’);
subLength(‘funny’, ‘n’);

thank you

The SCT is expecting a return value, but is instead getting, undefined. Suggest log at the caller and return result (or log as you are but also return result).

Yes!! thank you so much, it was driving me crazy

1 Like