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

94
Vistas
Compare two words spelled backwards and forwards is same way in JS

var str = ("level");

arr = Array.from(str);

function repeat(arr) {

  let forward = arr.slice(0, arr.length);
  let backward = arr.reverse();

  console.log(forward);
  console.log(backward);

  if (forward === backward) {
    return true;
  } else {
    return false;
  }
};


var result = repeat(arr);

console.log(result);

I keep getting results as false. And also if you have a better solution please share, thanks

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

This is because you are comparing to different objects.

Convert them to string before compare:

var str = ("level");

arr = Array.from(str);

function repeat(arr) {

  let forward = arr.slice(0, arr.length).join("");
  let backward = arr.reverse().join("");

  console.log(forward);
  console.log(backward);

  if (forward === backward) {
    return true;
  } else {
    return false;
  }
};


var result = repeat(arr);

console.log(result);

The method of reversing the string is good, but you have many unnecesary steps. Here is a more optimized version:

var str = "level";

function repeat(forward) {

  const backward = forward.split("").reverse().join("");

  console.log(forward);
  console.log(backward);

  return forward === backward;
}


var result = repeat(str);

console.log(result);

about 4 years ago · Juan Pablo Isaza Denunciar

0

When you compare two arrays, you are not comparing their contents, but instead the array objects themselves. Because forward and backward are different arrays, they are not equal to each-other.

I'd suggest either looping through each element in both arrays and comparing them to each-other, or joining all the elements into strings and comparing those.

about 4 years ago · Juan Pablo Isaza Denunciar

0

There are certain things that you can do with this

1) You can spread string in an array because str is a string and strings are iterable in JS.

let arr = [...str];

2) You can use spread in-place of slice to make it more readable

  let forward = [...arr];

3) Since reverse is in place, so you can spread it into a new array.

let backward = [...arr.reverse()];

4) You can have multiple options here first to use join and then compare because strings are primitive and compared with the value that is already answered. So you can also use every here:

forward.every((s, i) => s === backward[i]);

var str = "level";

let arr = [...str];

function repeat(arr) {
  let forward = [...arr];
  let backward = [...arr.reverse()];

  console.log(forward);
  console.log(backward);
  return forward.every((s, i) => s === backward[i]);
}

var result = repeat(arr);

console.log(result);

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