Help with function and scope

Hi folks.

I try to go deep in the FCC curriculum and I use books. In one exercise in Eloquent Javascript I have to code a function that counts the times a character is in a given string, that is what I coded:

// Strig String -> Int
// Return the number of occurrences of a character(char) on a given string(str)

function countChar(str, char) {
let counter = 0;
for (let x = 0; x < str.lenght; x++) {
if (str[x] == char) {
counter += 1;
}
}
return counter;
}

console.log(countChar(“Palabra”, “a”)); // 3
console.log(countChar(“Palabra”, “P”)); // 1

But the tests in console return zero always, I think is something with scope but the solution on the book it is almost the same as mine (only variable names are changed)

I am using Firefox 61.0.1 and Scratchpad to test the code

Can someone help me, What I am coding wrong?

You have typo in lenght. Nothing to do with scope.

And because of this, str.lenght is undefined and x < str.lenght always is false and the for loop does not run.

Thanks so much both.

I think is better to use a code editor with auto-completation in order to avoid this mistakes.