JSFiddle - React, Tailwind, and code Playground
HTML
<h1>Max sequence for 1 million</h1>
<input id="btnCalc" type="button" value="Calculate" onclick="solveCollatz()" />
<p id="pMaxNum">Number:</p>
<p id="pTotal">Total number of sequences:</p>
JavaScript
function solveCollatz() {
let num = 2;
let max = 1000000;
let total = 0;
let finish = 0;
while (num < max) {
let n = parseInt(num);
let array = [];
while (n > 1) {
n = (n % 2 == 0) ? n = n / 2 : n = 3 * n + 1;
array.push(n);
}
if (array.length > total) {
total = array.length;
finish = num;
}
num++;
}
document.getElementById("pMaxNum").innerHTML = `Number: ${finish}`;
document.getElementById("pTotal").innerHTML = `Total number of sequences: ${total}`;
}