Stuck on Parallel Letter Frequency Exercise

code :

export const parallelLetterFrequency = (texts) => {
  
  const letterFrequency = {};

  
  const updateFrequency = (text) => {
    for (const char of text.replace(/[^a-zA-Z]/g, '').toLowerCase()) {
      letterFrequency[char] = (letterFrequency[char] || 0) + 1;
    }
  };

  
  texts.forEach((text) => {
    updateFrequency(text);
  });

  
  for (const prop in letterFrequency) {
    if (!/^[a-zA-Z]$/.test(prop)) {
      delete letterFrequency[prop];
    }
  }

  return letterFrequency;
};

My code worked good up until the test “Parallel Letter Frequency > Unicode letters”.
In that test my code outputted “Object {}” after outputting the expected output, I don’t know why it happens, I don’t know how to fix it. Please help me fix this.

Here’s the error:

const texts = ['本', 'φ', 'ほ', 'ø'];
const expected = {
      本: 1,
      φ: 1,
      ほ: 1,
      ø: 1,
    };
const actual = parallelLetterFrequency(texts);
expect(actual).toEqual(expected);


### TEST FAILURE

Error: expect(received).toEqual(expected) // deep equality - Expected - 6 + Received + 1 - Object { - "ø": 1, - "φ": 1, - "ほ": 1, - "本": 1, - } + Object {}

You want to check this regular expression. It seems to match all characters that are not between a and z or A and Z. Those unicode characters definitely are not in that range, thus are removed when calling:

text.replace(/[^a-zA-Z]/g, '').toLowerCase()
1 Like

@SleeplessByte Thanks! I’ve modified the code a bit and now it does remove the “Object {}” thing from the output. But now it outputs what looks like parts of the expected output. Can you please explain to me why this happens?

The code:

export const parallelLetterFrequency = (texts) => {
  
  const letterFrequency = {};

  const updateFrequency = (text) => {
    if (text.includes('本')){
      for (const char of text.replace(/[\p{Letter}\p{Mark}]+/g, '').toLowerCase()) {
      letterFrequency[char] = (letterFrequency[char] || 0) + 1;
    }
    } else {
      for (const char of text.replace(/[^a-zA-Z]/g, '').toLowerCase()) {
      letterFrequency[char] = (letterFrequency[char] || 0) + 1;
    }
    }
    
  };

  
  texts.forEach((text) => {
    updateFrequency(text);
  });

  
  return letterFrequency;
}; 



The Error:

CODE RUN
const texts = ['本', 'φ', 'ほ', 'ø'];
const expected = {
      本: 1,
      φ: 1,
      ほ: 1,
      ø: 1,
    };
const actual = parallelLetterFrequency(texts);
expect(actual).toEqual(expected);
TEST FAILURE
Error: expect(received).toEqual(expected) // deep equality

- Expected  - 3
+ Received  + 0

  Object {
-   "ø": 1,
-   "φ": 1,
-   "ほ": 1,
    "本": 1,
  }

You should not have the conditional branch.

Only one of the texts includes that exact character. You don’t want different behaviour if that character is present. You want the same behaviour, always.

1 Like