角位数差-JavaScript

我们需要编写一个接受数字的JavaScript函数,从该数字的第一个和最后一个数字构造一个新数字,并返回原始数字与由此形成的数字之间的差。

例如:如果输入为34567

然后,角位数为-

37

输出将是-

34530

示例

以下是代码-

const num = 34567;
const cornerDifference = num => {
   let temp = Math.abs(num);
   let corner = temp % 10;
   if(temp < 100){
      corner = temp;
   }else{
      while(temp >= 10){
         temp = Math.floor(temp / 10);
      };
      corner = (temp*10) + corner;
   };
   return num - corner;
};
console.log(cornerDifference(num));

输出结果

以下是控制台中的输出-

34530