Code Challenges: Intermediate JavaScript

Hi guys so I’m through the code challenges in intermediate JavaScript and in exercise 2 i coded this:
function subLength(word, letter) {
length = 0
counter = 0
const split = word.split(’’)
const fullWord = word

for (let i = 0; i < split.length; i++ ) {

if (split[i] === letter) {

  counter += 1; 

}
}
if (counter === 2) {

const firstIndex = split.indexOf(letter)

const lastIndex = split.lastIndexOf(letter)

for (let j = firstIndex; j <= lastIndex; j++){

length += 1

}
} else {
return 0
}
return length
}

And I runned the tests and its returning the expected results but when i checked my answer it said that i likely have some syntax error. I can’t find it tho, can someone help me on this?

The only syntax error I get is due to the use of these characters , but you should be getting an error message pointing to line 4 if that is the problem. I’m going to take a guess here and wager the test result is trying to be helpful and isn’t actually giving accurate information.

What is your program actually supposed to do? Could you post a link to the lesson?
It appears your trying to find the length of the characters in a string between the first and last occurrence of a particular character. If this is the case it’s probably failing on some of the test cases do to a logic error. For example what happens if you run this:

console.log(subLength('ttt', 't'));

If I understand what it should be doing, it should log 3, but it logs 0.

As I recall from that exercise, 0 is correct. If there are more than 2 occurrences of the letter, 0 should be returned. A link to the exercise would be most helpful. I don’t see any issues with the code, but the SCT for the exercise may be complaining about something.

this is what the exercise asks:

Write a function subLength() that takes 2 parameters, a string and a single character. The function should search the string for the two occurrences of the character and return the length between them including the 2 characters. If there are less than 2 or more than 2 occurrences of the character the function should return 0.

Examples:
subLength(‘Saturday’, ‘a’); // returns 6
subLength(‘summer’, ‘m’); // returns 2
subLength(‘digitize’, ‘i’); // returns 0
subLength(‘cheesecake’, ‘k’); // returns 0

1 Like

Your code looks fine and works as expected. I haven’t been able to find the exercise (don’t remember where it is). If you post a link to it, we can look into why your code may generate the error message.

Also, when you post code, you should include it between sets of 3 backticks to preserve the formatting:

    ```
    code goes here
    ```
1 Like

i found it here: https://www.codecademy.com/paths/back-end-engineer-career-path/tracks/becp-javascript-syntax-part-ii/modules/fecp-practice-javascript-syntax-arrays-loops-objects-iterators/articles/fecp-javascript-practice-arrays-loops-objects-iterators

1 Like

Okay. I looked at it, ran your code, and received the same message. The issue is here:

In strict environments, variables declared without a keyword (let, const or var) aren’t allowed. Add an appropriate keyword, and you’ll pass the exercise.

1 Like

I can’t believe i forgot that! thank you so much, it passed now :)))

1 Like