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

243
Views
The only word that is not passing the test is "almostomla."

I am learning JS on freecodecamp and am stuck on a problem. I am creating a palindrome checker. My code almost works. However, there is only one word that is not passing the test and that is almostomla. It is not a palindrome yet my code returns true. I have tried several things. Even rewrote the code and used the while loop but nothing seems to help. There is a solution at the freeCodecamp web but I have written the code in a different way and am unable to figure my mistake out.

Here is my code.

let reversedStr = [];

function palindrome(str) {
  let d = str.replace(/[^a-zA-Z0-9]/g, "").toLowerCase();
  for (let i = d.length - 1; i >= 0; i--) {
    reversedStr.push(d[i]);
  }
  for (let j = 0; j < str.length; j++) {
    if (reversedStr[j] == d[j]) {
      return true;
    } else {
      return false;
    }
  }
}
console.log(palindrome("almostomla"));

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

0

This line if (reversedStr[j] == d[j]) {return true; returns as soon as the character matches at both the index. It does not check rest of the characters.

In fact you can just return as soon as the character in both index does not match.

Also note the reversedStr has to be inside the function. Else it will contain previous values

function palindrome(str) {
  let reversedStr = [];
  let d = str.replace(/[^a-zA-Z0-9]/g, "").toLowerCase();
  for (let i = d.length - 1; i >= 0; i--) {
    reversedStr.push(d[i]);
  }
  
  for (let j = 0; j < str.length; j++) {
    if (reversedStr[j] !== d[j]) {
      return false;
    }
  }
  return true
}
console.log(palindrome("almostomla"));
console.log(palindrome("1221"));

about 4 years ago · Juan Pablo Isaza Report

0

Your code returns "true" or "false" after first match - it checks that 'a' is equal to 'a' and returns true.

You can return "false" from inside of cycle only if you found duplicated letter else return true after the cycle ends.

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!