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:

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,


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

Then, on line 4, add a let to your declaration.
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

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.