Code is not being marked as correct. 2nd code challenge in Arrays, Loops, Objects, Iterators article

Hi all, I’m doing one of the the second code challenge on this page: https://www.codecademy.com/paths/full-stack-engineer-career-path/tracks/fscp-javascript-syntax-part-ii/modules/fecp-practice-javascript-syntax-arrays-loops-objects-iterators/articles/fecp-javascript-practice-arrays-loops-objects-iterators

This is my solution, which seems to return all correct results, however, I get ‘check code for syntax error’ when I hit ‘check answer’. I’ve been debugging for more hours than I care to admit, can anyone shed some light on why it’s not being flagged as a correct solution?

const subLength = (str, char) => { function letterCounts(str) { result = {}; str.toLowerCase().split('').forEach(char => { if (!result[char]) { result[char] = 0; } result[char]++; }) return result } if (letterCounts(str)[char] !== 2) { return 0; } var firstChrIndex = str.indexOf(char); var lastChrIndex = str.lastIndexOf(char); return str.substring(firstChrIndex, lastChrIndex + 1).length } console.log(subLength('Saturday', 'a')); // prints 6 console.log(subLength('summer', 'm')); // prints 2 console.log(subLength('digitize', 'i')); // prints 0 console.log(subLength('cheesecake', 'k')); // prints 0

Thanks All,
Steve

Howdy.

Now, take a deep breath. Be very calm. Think nice thoughts.

Then, on line 4, add a let to your declaration.
i.e.
let result = {};

Took me a while, as it works fine without, but it’s obviously flagging something up somewhere.

1 Like

HAHA amazing thanks so much. I got so bogged down in the fact that that it was outputting I guess.

1 Like