<script> var bestResult = ''; var bestResultCount = 0; var bestResultStartInteger = 0; // I made an assumption here, that only odd numbers are of interest // reason being, that, given task conditions, any even number will // generate a smaller number, than the same number minus 1 // e.g.: 502 -> 251 -> 754 but 501 -> 1504 // not mentioning even numbers which produce more even numbers when divided by 2 // like 500 -> 250 -> 125 -> 376 // and we want as big numbers as possible, to make the string longer // hence starting from the top of the "stack" for (var i = 999999; i > 1; i = i - 2) { let tmp = i; let currentResult = i; let currentResultCount = 0; while (tmp > 1) { if (tmp % 2 === 0) { // even tmp = tmp / 2; } else { tmp = (3 * tmp) + 1; } currentResult = currentResult +'-'+ tmp; currentResultCount = currentResultCount + 1; } if (currentResultCount > bestResultCount) { bestResult = currentResult; bestResultCount = currentResultCount; bestResultStartInteger = i; } } console.log(bestResult); console.log(bestResultCount); console.log(bestResultStartInteger); alert('Answer: '+ bestResultStartInteger); </script>