[CodeKata] Week 1 - Day 2


質問する



に答える

const reverse = x => {
	  const xStr = x.toString();
    let result = [];
    for(let i = xStr.length-1; i >= 0; i--){
      if(xStr[i] === '0') {
        continue;
      }
      result.push(xStr[i]);
      
      if(result[result.length-1] === '-') {
      result.pop();
      result.unshift('-');
      }
    }
return Number(result.join(''));
};

console.log(reverse(1234)); // 4321
console.log(reverse(-1234)); // -4321
console.log(reverse(1230)); // 321