These are the logical steps which I need to do with jquery:
x
is a 2 digit number(integer) derived from an input.value();
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;
Thanks!
if (x != 33 && x != 44) {
while (x > 9) {
var parts = ('' + x).split('');
x = parseInt(parts[0]) + parseInt(parts[1]);
}
return x;
} else {
return x;
}
Works only if the input is really max 2 digits long as you say, else you'll need to add the numbers in a for
loop over parts.length
. E.g.:
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;
}
I'd try:
function process (x) {
if ((x != 33) && (x != 44)) {
while (x > 9) {
x = Math.floor (x / 10) + (x % 10);
}
}
return x;
}
I see little reason to convert it to a string when you can use arithmetic operations.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With