首尾数字差异 - 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
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP