Palindrome check function

4

I have the challenge of writing a code to check if a text is a palindrome or not. I have not finished the code at the moment, it's like this:

function checkPalindrome(str) {     
var direita = [];
for(var i = 0; i<str.length; i++){      
    direita.unshift(str.charAt(i)); 
    }
    console.log(direita);
var esquerda = [];
for (var j=str.length-1; j>=0; j--){
    esquerda.push(str.charAt(j));
}
    console.log(esquerda);
    };

So far I wanted to see if I could read a string normally and the other way around. It would play the two readings in 1 vector each for later, compare them and say if it is palindrome or not. Anyway, it happens that when we test in the Google Chrome console or another, it executes only esquerda.push(str.charAt(j)); for the two vectors, not reading the necessary two-way expression. What could be wrong?

A palindrome is a text that can be read normally, or inverted, which will have the same meaning. Example: "revive", "blue light", "radar".

    
asked by anonymous 15.07.2018 / 20:40

2 answers

5

If the function is to check, and has this even in the name, it is highly likely that it should return if it is green or false. And even if it were not for that, only if she explicitly said she should print is that she should do it.

I do not understand what you are trying to do in this code, but if it is what is described in the question is simpler, just go checking the first with the last, the second with the penultimate, and so on until arriving in the middle , so you only have to go halfway. Anyone who performs the condition already guarantees that it is not a palindrome and does not have to continue, it will only be a palindrome if it passes through all without fail.

function checkPalindrome(str) {     
    for(var i = 0; i < str.length / 2; i++) if (str[i] != str[str.length - i - 1]) return false;
    return true;
}
console.log(checkPalindrome("radar"));
console.log(checkPalindrome("reviver"));
console.log(checkPalindrome("palindromo"));

The question does not make it clear whether to accept phrases, if necessary you would have to delete the spaces before. There you can add a previous step, which is less efficient, or you will have to do a more complex algorithm that will ignore the space, which is significantly more complex and I do not even know if it will be that much more efficient.     

15.07.2018 / 21:22
5

You can reverse the string and compare like this: str === str.split('').reverse().join('')

const testes = ["reviver", "luz azul", "radar", "manhã"];
const checkPalindrome = str => str === str.split('').reverse().join('');
const resultados = testes.map(checkPalindrome);
console.log(resultados);
// dá: true, false, true, false

To ignore blanks you can do:

const testes = ["reviver", "luz azul", "radar", "manhã"];
const ignoreSpaces = str => str.split(' ').join('');
const checkPalindrome = str => ignoreSpaces(str) === ignoreSpaces(str.split('').reverse().join(''));
const resultados = testes.map(checkPalindrome);
console.log(resultados);
// dá: true, true, true, false
    
15.07.2018 / 21:35