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

97
Views
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 answers
Answer question

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 Report

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 Report

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 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!