Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

145
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda