Ric*_*yte 5 javascript string jquery numbers
这些是我需要用jquery做的逻辑步骤:
x
是一个从input.value()派生的2位数字(整数);
If var x is **not** 33 or 44
Convert this 2 digit number to string;
split the string in 2 parts as number;
Add these 2 values until they reduce to single digit;
Return var x value as this value;
Else
Return var x value literally as 33 or 44 whatever is the case;
Run Code Online (Sandbox Code Playgroud)
谢谢!
if (x != 33 && x != 44) {
while (x > 9) {
var parts = ('' + x).split('');
x = parseInt(parts[0]) + parseInt(parts[1]);
}
return x;
} else {
return x;
}
Run Code Online (Sandbox Code Playgroud)
仅当输入确实如您所说的最大 2 位数字时才有效,否则您需要在循环中添加for
数字parts.length
。例如:
if (x != 33 && x != 44) {
while (x > 9) {
var parts = ('' + x).split('');
for (var x = 0, i = 0; i < parts.length; i++) {
x += parseInt(parts[i]);
}
}
return x;
} else {
return x;
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
26635 次 |
最近记录: |