“Solusi LeetCode Reverse Interger” Kode Jawaban

Solusi LeetCode Reverse Interger

var reverse = function(x) {
    
  let xStr = String(Math.abs(x)).split('').reverse().join('');
  let int = parseInt(xStr); 
  
  if(int > Math.pow(2, 31)) return 0;
  if(x < 0) return -int;
  return int;  
};
Restu Wahyu Saputra

Solusi LeetCode Reverse Interger

/**
 * @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)
};
Restu Wahyu Saputra

Solusi LeetCode Reverse Interger

var reverse = function(x) {
    
  let str = String(Math.abs(x)).split('').reverse().join('');
  let int = parseInt(str); 
  
  if(int > Math.pow(2, 31) || int > Math.pow(2, 31) - 1) return 0;

  return x < 0 ? -int : int;  
};
Restu Wahyu Saputra

Jawaban yang mirip dengan “Solusi LeetCode Reverse Interger”

Pertanyaan yang mirip dengan “Solusi LeetCode Reverse Interger”

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya