How to reverse a negative Number in JavaScript?

  • A+
Category:Languages

I am trying to get the output in this way, unfortunately, it isn't return expected value...

function reverseInt(int){     let intRev ="";     for(let i= 0; i<int.length; i++){         intRev = int[i]+intRev ;     }     return intRev ; } console.log(reverseInt("-12345"));

 


Use

function reverseInt(n) {     return Math.sign(n)*parseInt(n.toString().split('').reverse().join('')); } 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: