Collatz conjecture in JavaScript
Solution 1:
That's right. The return was out of place. It is supposed to be outside while loop.
function steps(n: number) {
let counter: number = 0;
if (n > 0 && Number.isInteger(n)) {
while (n !== 1) {
if (n % 2 === 0) {
n = n / 2;
counter++;
} else if (n % 2 !== 0) {
n = (n * 3) + 1;
counter++;
}
}
return counter;
} else {
throw new Error('Only positive numbers are allowed');
}
}