OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

How to write values into empty array from a loop dedicated to another function in Javascript?

  • Thread starter Thread starter Adam Givon
  • Start date Start date
A

Adam Givon

Guest
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?

I wrote the following code, and I hoped see, on top of the first headers and button 3 lines of numbers, the randomized array, the counter array iValue1, and the randomizer array Value. Instead, I got nothing.


Code:
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;

Code:
<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>
<p>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 (<code>arsam1</code> is the number fixed set, <code>iValue1</code> and <code>jValue1</code> are empty arrays which are supposed to be filled with serial number <code>i</code> and the randomized number <code>j</code>).</p>
<p>I would like to print the arrays <code>iValue1</code> and <code>jValue1</code> on the screen.</p>
<p>When using the following code, outside of seeing the headers and button, I see no number at all. Where am I wrong?</p>
<p>I wrote the following code, and I hoped see, on top of the first headers and button 3 lines of numbers, the randomized array, the counter array <code>iValue1</code>, and the randomizer array Value. Instead, I got nothing.</p>
<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-js lang-js prettyprint-override"><code>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;
let j = Math.floor(Math.random() * (i + 1));
let jValue1 = j;
let k = arsam1;
arsam1 = arsam1[j];
arsam1[j] = k;
}
}

document.getElementById("iValue").innerHTML = iValue1;
document.getElementById("jValue").innerHTML = jValue1;</code></pre>
<pre class="snippet-code-html lang-html prettyprint-override"><code><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></code></pre>
</div>
</div>
</p>
Continue reading...
 

Latest posts

J
Replies
0
Views
1
jbowerbir
J
Top