以展开形式表示数字 - JavaScript
假设我们得到一个数字 124,并要求编写一个将此数字作为输入并返回其展开形式(作为字符串)的函数。
124 的展开形式为 −
'100+20+4'
示例
以下是代码 −
const num = 125;
const expandedForm = num => {
const numStr = String(num);
let res = '';
for(let i = 0; i < numStr.length; i++){
const placeValue = +(numStr[i]) * Math.pow(10, (numStr.length - 1 - i));
if(numStr.length - i > 1){
res += `${placeValue}+`
}else{
res += placeValue;
};
};
return res;
};
console.log(expandedForm(num));输出
以下是控制台中的输出 −
100+20+5
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP