if (x != 33 && x != 44) {
while (x > 9) {
var parts = ('' + x).split('');
x = parseInt(parts[0]) + parseInt(parts[1]);
}
return x;
} else {
return x;
}
It only works if the input really has max 2 digits, as you say, otherwise you will need to add numbers in the loop foron top parts.length. For instance:.
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;
}
source
share