1
function reachOne(integer) {
//deal with errors in input
var array = [];
//initialize multiply by putting it equal to 1
var multiply = 1;
var count = 0;
if (integer < 0) {
document.write('Do it again, Input should be a positive Integer')
} else if (integer < 10 && integer >= 0) {
document.write(count);
} else {
do {
for (var i = 0; i < integer.length; i++) {
multiply = multiply * integer.charAt(i);
//if i try to write document.write(multiply), nothing appears
console.log("HELLO");
}
count++
}
while (multiply >= 10)
}
//if I write document.write(multiply) here, the result is 1 and not the product of all the numbers
}
reachSingle(254)
----------
上面的代碼應該是一個正整數,並返回該整數內部的數字必須乘以一起得到一個數字的次數。例如:254會給2:2 * 5 * 4 = 40和4 * 0 = 0變量似乎沒有承擔新的價值
你的'integer'參數看起來應該是一個數字,但'for'循環把它看作是一個字符串。因此'integer.length'將會是'undefined',所以'for'循環永遠不會發生。 – Pointy