【发布时间】:2015-07-23 10:17:48
【问题描述】:
我需要将一个数字的数字相加(例如 21 是 2+1),以便数字减少到只有一位数字 (3)。我想出了如何做那部分。
然而,
1) 我可能需要在同一个变量上多次调用该函数(例如,99 是 9+9 = 18,仍然 >= 10)并且
2) 我需要从这个函数的范围中排除数字 11 和 22。
下面我哪里出错了?
var x = 123;
var y = 456;
var z = 789;
var numberMagic = function (num) {
var proc = num.toString().split("");
var total = 0;
for (var i=0; i<proc.length; i++) {
total += +proc[i];
};
};
while(x > 9 && x != 11 && x != 22) {
numberMagic(x);
};
} else {
xResult = x;
};
console.log(xResult);
//repeat while loop for y and z
【问题讨论】:
标签: javascript function while-loop numbers digits