以展开形式表达数字 - JavaScript
javascriptweb developmentfront end technologyobject oriented programming更新于 2025/12/7 11:24:17
假设我们得到一个数字 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
相关文章
有用资源
javascript 参考教程 - 该教程包含有关 javascript 的更多信息:https://www.cainiaomax.com/javascript/

