subLength code challenge not answer not accepted

Below is my code to the subLength challenge
it works fine in the browser console and vscode console
but codecademy tests returns
Tested subLength('funny', 'n') and it did not return 2.
What am I missing??

// Write function below
function subLength(givenString, char){
    let string = givenString;
    let singleChar = char;

    let count = 0;

    let index = string.indexOf(singleChar);

    while(index !== -1){
        index = string.indexOf(singleChar, index+1);

    if (count > 2 || count < 2){
        return console.log(0);
    let charIndex0 = string.indexOf(singleChar);
    let charIndex1 = string.indexOf(singleChar, charIndex0 + 1);
    return console.log(charIndex1 - charIndex0 + 1);


.log() and return are not the same thing, the log method simple logs the output, return hands back data to the caller/function call

combining return and .log() is a bad idea, the log method does not return anything (result in undefined), so your subLength currently always return undefined

Ok that clarifies it.
Thank you
I got confused because the browser’s console did not complain.

displaying output to the user using your program and passing data around (internally in your application) are two very different things

1 Like

yes - i am learning this.
thank you