Home > Software design >  Function always return True during an If/Else condition, what is wrong with this conditional?
Function always return True during an If/Else condition, what is wrong with this conditional?

Time:06-13

Those are palindromes problems from The Odin Project, I did everything works perfect, but in the last challenge, when the program needs to return false, It not happens. I've tried to change str with that complete string, but the function continues to return true.

A palindrome is a string that is spelled the same both forwards and backwards, usually without considering punctuation or word breaks:

some palindromes:

  • A car, a man, a maraca.
  • Rats live on no evil star.
  • Lid off a daffodil.
  • Animal loots foliated detail of stool lamina.
  • A nut for a jar of tuna.

const palindromes = function (str) {
    str = str.replace('/[áàãâä]/ui', 'a');
    str = str.replace('/[éèêë]/ui', 'e');
    str = str.replace('/[íìîï]/ui', 'i');
    str = str.replace('/[óòõôö]/ui', 'o');
    str = str.replace('/[úùûü]/ui', 'u');
    str = str.replace('/[ç]/ui', 'c');
    str = str.replace('/[^a-z0-9]/i', '');
    str = str.replace('/_ /', '');
    str = str.replace("!", "");
    let reverseString = str.split().reverse().join();
    let polishedString = reverseString;


    if (polishedString === str) {
        return true;
    }
    else {
        return false;
    }
  
};
palindromes("ZZZZ car, a man, a maracaz.");
// Do not edit below this line
module.exports = palindromes;

What is wrong with my If/Else condition?

CodePudding user response:

The empty string argument were missing here in split and join method.

const palindromes = function (str) {
let polishedStr = str;
    polishedStr = polishedStr.replace('/[áàãâä]/ui', 'a');
    polishedStr = polishedStr.replace('/[éèêë]/ui', 'e');
    polishedStr = polishedStr.replace('/[íìîï]/ui', 'i');
    polishedStr = polishedStr.replace('/[óòõôö]/ui', 'o');
    polishedStr = polishedStr.replace('/[úùûü]/ui', 'u');
    polishedStr = polishedStr.replace('/[ç]/ui', 'c');
    polishedStr = polishedStr.replace('/[^a-z0-9]/i', '');
    polishedStr = polishedStr.replace('/_ /', '');
    polishedStr = polishedStr.replace("!", "");
    let reverseString = polishedStr.split("").reverse().join(""); //the empty string were missing here in split and join method.

    console.log(reverseString, polishedStr);
    if (reverseString === polishedStr) {
        return true;
    }
    return false;
};
console.log(palindromes("ZZZZ car, a man, a maracaz."));
// Do not edit below this line
//module.exports = palindromes;

CodePudding user response:

You should use .split('') that splits by letters. And .join('') instead of .join().

const palindromes = function (str) {
    str = str.replace('/[áàãâä]/ui', 'a');
    str = str.replace('/[éèêë]/ui', 'e');
    str = str.replace('/[íìîï]/ui', 'i');
    str = str.replace('/[óòõôö]/ui', 'o');
    str = str.replace('/[úùûü]/ui', 'u');
    str = str.replace('/[ç]/ui', 'c');
    str = str.replace('/[^a-z0-9]/i', '');
    str = str.replace('/_ /', '');
    str = str.replace("!", "");
    
    let reverseString = str.split('').reverse().join('');

    return reverseString === str;
};

console.log(palindromes("ZZZZ"));  // true
console.log(palindromes("ZZZZ car, a man, a maracaz."));  // false

CodePudding user response:

You can try with a regex like this:

const palindromes = function (str) {
    const compareStr = str.toLowerCase().replace(/[^\w]/g, '');
    const reverseStr = compareStr.split('').reverse().join('');
    return (compareStr === reverseStr)
};
  • Related