Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

173
Visualizações
How to reverse number with minus sign

As I mentioned before i want to reverse a number with minus sign

expected output

141-

my output is

-141
/**
 * @param {number} x
 * @return {boolean}
 */
var isPalindrome = function (x) {

    var reversedNum = parseFloat(x.toString().split('').reverse().join('')) * Math.sign(x);

    return reversedNum;

};

console.log(isPalindrome(-141));
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

const isPalindrome = (x) => {
    const str = new String(x);
    return Array.from(str).reverse().join('');
}
about 4 years ago · Juan Pablo Isaza Relatório

0

Taking it step by step:

x = -141
-141.toString() = "-141"
"-141".split('') = ["-","1","4","1"]
["-","1","4","1"].reverse() = ["1","4","1","-"]
["1","4","1","-"].join('') = "141-"
parseFloat("141-") = 141
Math.sign(-141) = -1
141 * -1 = -141

If you are attempting to output a number type that is serialized as 141-, that is impossible. If you want a string output, there is no reason to parse as a float as the trailing - is ignored. So simply remove the parseFloat() and * Math.sign(x).

However, your JSdoc dictate that the function should return a boolean, and the function is named isPalindrome, indicating it should return if the input is the same reversed. In which case I would expect something like:

var isPalindrome = function (x) {

  return x.toString() === [...x.toString()].reverse().join('');

};
about 4 years ago · Juan Pablo Isaza Relatório

0

const isPalindrome = number => number.toString().split("").reverse().join("")

number = -142

toString() method converts the number to string. // "-141"

split("") method converts the string to array. // ["-", "1", "4", "1"]

reverse() method reverse the array. // ["1", "4", "1", "-"]

join("") method returns the array as a string. // "141-"

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda