Why would this code not be accepted?

Why would this code not be accepted?

const subLength = (str, char) => {
let charCount = 0;
let len = -1;
for (let i = 0; i < str.length; i++) {
if (str[i] === char) {
charCount ++;
if (charCount > 2) {
len = 0
} else if (len === -1) {
len = i;
} else {
len = i - len + 1;
}
}
}
if (charCount < 2) {
len = 0;
}
console.log(len);
}

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

We don’t always know what test may have failed, so don’t be blinded by what correct results you see. Find possible causes of the incorrect results you don’t see.
Your approach is reasonable, but could use some refinement of the logic. Could be in the code bloat there is an edge case that we’re not seeing. After analyzing and arriving at the basic gist of it, I’ve come down to this…

const subLength = (str, char) => {
  const indices = []
  for (let i = 0; i < str.length; i++) {
    const c = str[i]
    if (c === char) {
      indices.push(i)
    }
  }
  if (indices.length !== 2) return 0
  let [a, b] = indices
  return str.slice(a, b + 1).length
}

console.log(subLength('Saturday', 'a'))  //  6

Now compare and see if you find the edge case in your code. This is not a trick question. I did not search for it.

1 Like

This is a really creative way to code the function, but I could not understand how this works:
let [a, b] = indices
return str.slice(a, b + 1).length

1 Like

I suspected as much, in an afterthought. In truth it is overkill, sort of. We don’t need the slice. We have the indices which are numerically aligned.

const [a, b] = indices

That is a deconstruction of the array we know has only two elements, which we assign respectively to a and b. The syntax uses two reference variables in an array that will now take the corresponding values from the assigned array. We can now refer to the variables individually. Read up on this as it is a powerful tool to use. Forget that it hasn’t been taught yet. You learned it here.

return 1 + b - a

Subscripts are so messy. It’s also why I wrote,

c = str[i]

so it made the comparison easier to read.

1 Like

The length we have computed without the need of this expression, but focus in on the principal object… The slice.

Slicing is a virtual snapshot of a segment of a sequence. The virtual segment will be the same type as the original, so a slice of a string will be a string; and, a slice of an array will be an array. Even if empty in both cases.

'aturda'

was the slice we obtained from the source string.

Neither source object is affected since this is a photo taken from the air, so to speak. Besides, strings are immutable, anyway.


Let’s apply what we know…

  return [str.slice(a, b + 1), 1 + b - a]

Above we’re upgraded the return value to an array with two elements.

[str, len] = subLength('Saturday', 'a')
console.log(`String: ${str}\nLength: ${len}`)

We’re able to deconstruct the return value using the variable names we would normally expect to describe their values. A suitable and justifiable place for verbose names. But still, I lean to the skimpy, slightly symbol oriented for the reduced number of typos. String formatting with interpolation made breaking down the results a breeze.

String: aturda
Length: 6