Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

149
Views
I'm getting this error: expected [ '' ] to deeply equal [], but it works fine on vs code

This was the question from codewars:

Complete the solution so that it splits the string into pairs of two characters. If the string contains an odd number of characters then it should replace the missing second character of the final pair with an underscore ('_').

Examples:

  • 'abc' => ['ab', 'c_']
  • 'abcdef' => ['ab', 'cd', 'ef']

And this is what i have achieved:

function solution(str) {
      const letters = str.split('');
      let text = '';
      for (const letter of letters) {
        text += letter;
        if (text.replace(/ /g, '').length % 2 === 0)
          letter !== letters[letters.length - 1] && (text += ' ');
      }
      if (str.length % 2 !== 0) text += '_';
      return text.split(' ');
    }
    
    console.log(solution('abcd'));

This is the error, i'm getting: expected [ '' ] to deeply equal []

A screenshot from codewars

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

function solution(str) {
  const letters = str.split('');
  let text = '';
  for (const letter of letters) {
    text += letter;
    if (text.replace(/ /g, '').length % 2 === 0)
      letter !== letters[letters.length - 1] && (text += ' ');
  }
  if (str.length % 2 !== 0) text += '_';
  console.log(text);
  return (text === '' && []) || text.split(' ');
}

console.log(solution(''));

Now its working as expected when solution function is called as solution(' ') and now it returns an empty array [ ] instead of an array with an empty string[' ']

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!