How to create an array of loop values which is dedicated to another function?
Hi,
I am new to this, and will be grateful for your help.
I am doing a random sort of values according to the Fisher Yates method, and to explore the inside of this process I tried to out the random numbers (arsam1 is the number fixed set, iValue1 and jValue1 are empty arrays which are supposed to be filled with serial number i and the randomized number j).
I would like to print the arrays iValue1 and jValue1 on the screen.
When using the following code, outside of seeing the headers and button, I see no number at all. Where am I wrong?
Thanks!
<!DOCTYPE html>
<html>
<body>
<h1>string Templates</h1>
<p>check up string templates</p>
<p id="demo"></p>
<button onclick="myFunction()">Randomize</button>
<p id="iValue"></p>
<p id="jValue"></p>
<script>
const arsam1 = [5,10,15,18,6,3,9,23,29,17,12,40];
let iValue1 = [];
let jValue1 = [];
document.getElementById("demo").innerHTML = arsam2;
function myFunction() {
for (let i=arsam1.length-1; i>0; i--) {
let iValue1[i]=i;
let j = Math.floor (Math.random() * (i+1));
let jValue1[i]=j;
let k = arsam1[i];
arsam1[i]=arsam1[j];
arsam1[j]=k;
}
}
document.getElementById("iValue").innerHTML = iValue1;
document.getElementById("jValue").innerHTML = jValue1;
</script>
</body>
</html>
Posted Jun 27, 2024, 3:16 PM UTC
112