以扩展形式表示数字-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
热门推荐
10 送礼祝福语简短有深意
11 滑稽搞笑结婚祝福语简短
12 儿童励志寄语简短祝福语
13 老师给家长祝福语简短
14 宝宝生日奶奶祝福语简短
15 自己生日祝福语段子简短
16 带马的祝福语简短
17 圣诞贺卡祝福语简短高级
18 简短深沉的生日祝福语