JavaScript Practice: Arrays, Loops, Objects, Iterators - subLength Exercise

This exercise’s challenge is the following: “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.

I don’t understand why the following code does not work. I clicked “Check Answer” and it says there might be a syntax error. However, I tried logging the examples given, the function ran without error, and they all logged correctly.

const subLength = (word, char) => {
  charCount = word.split(char).length - 1;

  if (charCount !== 2) {
    return 0;
  } else {
    return word.lastIndexOf(char) - word.indexOf(char) + 1;
  }
} 

console.log(subLength('Saturday', 'a')); // returns 6
console.log(subLength('summer', 'm')); // returns 2
console.log(subLength('digitize', 'i')); // returns 0
console.log(subLength('cheesecake', 'k')); // returns 0

I’ve found that the generic “syntax error” message in the interactive articles is usually caused by using a variable that wasn’t declared with let, const, or var.

In this case, you set the value of charCount that way. JavaScript allows this so it’s working when you run the code, but the test behind the scenes that Codecademy uses is stricter. It’s a best practice too, though it would be nice if the error message indicated what was happening for seemingly working code.

1 Like

Hi, I don’t get how the following line of code in the example above works to find out the number of instances of the ‘char’ in the string… can someone help?:

> charCount = word.split(char).length - 1;

Edit: I have kind of figured out what’s going on, but now drilling down into .split even further, I have encountered some problems.

>  let animal = 'zaz'
> 
>   let count = animal.split('z').length
> 
>   console.log(count)` // Output: 3

The above gives me three elements, which is what I expect as the ‘z’ character is the separator.

So how come the following doesn’t give me FOUR elements? very confusing

> let animal = 'zaza'
> 
>   let count = animal.split('z').length
> 
>   console.log(count) // Output: 3

Because there is still the same number of zs. With the first one, the array looks like ['','a','']. With the second one, the array looks like ['','a','a']. So, there are different elements, but the same length, because of the number of zs.

Thanks for the reply neutrino, I don’t get that at all though…

why is ‘zaza’ not

> ['', 'a', '', 'a']

When there is a z (or any character(s) used as the argument of .split(), it replaces that space with a comma in the list (if you will):

let x = "aza";
let y = x.split("z");
//y is now ["a", "a"]

Since the z was used to split the string, anything before and after a z is turned into its own element in the array. But with:

let x = "zaz";
let y = x.split("z");
//y = ["","a",""]

You can’t just ignore the fact there were characters before and after the a, but there isn’t any character to split the string into apart from a (both zs are used in the split). So the empty strings "" are used almost as placeholders. Does this make sense?

1 Like