When I run this, it doesn't run the function when I click the button, however it is run once when the website is loaded/reloaded even though (as far as I can tell) the code doesn't do that at any point. I am using the DuckDuckGo browser. I don't have much experience in HTML or JS. This is just for personal use, and doesn't need to be supported on anything except DuckDuckGo.
Here is the HTML (filepath is index.html):
<html lang="en">
<link rel="stylesheet" href="CSS/index.css">
<body>
<script src="Scripts/scrambleGen.js"></script>
<div>
<p id="scramble"></p>
<button id="nextScramble">Next</button>
<script>
document.getElementById("nextScramble").addEventListener("click", scrambleGen(document.getElementById("scramble")));
</script>
</div>
<div>
<p id="timeInput"></p>
</div>
</body>
</html>
Here is the JS (filepath is "Scripts/scrambleGen.js"):
function scrambleGen(textBox)
{
scramble = "";
const moves = ["R","R2","R3","L","L2","L3","U","U2","U3","D","D2","D3","F","F2","F3","B","B2","B3"];
lastMove = "";
for (let i = 0; i < 30; i++)
{
move = moves[randomInt(0,17)]
if (lastMove.substring(0,1) == move.substring(0,1))
{
move = move.substring(0,1) + (parseInt(move.substring(1), 10) + parseInt(lastMove.substring(1), 10)).toString();
}
else
{
if (lastMove[1] != undefined)
{
if (((parseInt(lastMove.substring(1), 10) % 4).toString(10)) == 1)
{
scramble += lastMove.substring(0,1);
scramble += " ";
}
else if (((parseInt(lastMove.substring(1), 10) % 4).toString(10)) == 2)
{
scramble += lastMove.substring(0,1) + "2";
scramble += " ";
}
else if (((parseInt(lastMove.substring(1), 10) % 4).toString(10)) == 3)
{
scramble += lastMove.substring(0,1) + "'";
scramble += " ";
}
}
else
{
scramble += lastMove;
scramble += " ";
}
}
lastMove = move;
}
textBox.innerText = scramble;
}
function randomInt(min, max)
{
return Math.floor(Math.random() * (max - min + 1) ) + min;
}