📜  leetcode 逆整数解决方案 - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:03:11.835000             🧑  作者: Mango

代码示例3
/**
 * @param {number} x
 * @return {number}
 */
var reverse = function(x) {
  const toArr = String(x).split('')
  const store = []
  
  if(toArr.length > 0) {
    toArr.forEach((val) => {
        if(val !== 0 && val !== '-') {
           store.push(val)
        }
      
       if(val === 0) {
           store.push(val)
        }
      
        if(val !== 0 && val == '-') {
           store.push('-')
        }
    })
  }

 let reverse = 0
  
 if(store.includes('-')) {
   reverse = - + store.reverse().join('').replace('-', '')
 }
  
  if(!store.includes('-')) {
   reverse = store.reverse().join('').replace('-', '')
 }
  
 return parseInt(reverse)
};