JavaScript non repeating random letters on table

I am trying to create a table with non repeating random letters (uppercase only) using JavaScript. Someone posted an answer, but it was deleted before I got a chance to ask: 1) Why is there a var x and a var y instead of having only a var x? 2)Why do you put i-1 in return letters[i-1]?

HTML5:

<table class="bingo">
    <tr>
        <td id="square0"></td>
        <td id="square1"></td>
        <td id="square2"></td>
    </tr>
    <tr>
        <td id="square3"></td>
        <td id="square4"></td>
        <td id="square5"></td>
    </tr>
    <tr>
        <td id="square6"></td>
        <td id="square7"></td>
        <td id="square8"></td>
   </tr>
 </table>

JS

//Create capital letters A - Z array
var letters = [];
    for (var i = 65; i < 91; i++) {
      letters.push(String.fromCharCode(i));
}

//Confuse about why there is a var x and a var y?
    for (var i = 0; i < 26; i++) {
       var x = Math.floor(Math.random() * 26);
       var y = Math.floor(Math.random() * 26);
       var temp = letters[x];
       letters[x] = letters[y];
       letters[y] = temp;
 }

 var rLetter = function(i) {
  return letters[i-1];
};

window.onload = function () {
  //calls the function  
  $("square0").innerHTML = rLetter(1);
  $("square1").innerHTML = rLetter(2);
  $("square2").innerHTML = rLetter(3);
  $("square3").innerHTML = rLetter(4);
  $("square4").innerHTML = rLetter(5);
  $("square5").innerHTML = rLetter(6);
  $("square6").innerHTML = rLetter(7);
  $("square7").innerHTML = rLetter(8);
  $("square8").innerHTML = rLetter(9);

}

Answer:1

1) Why is there a var x and a var y instead of having only a var x?

It looks like this code creates an array of all capital letters, then "randomizes" them by switching the location of 2 letters 26 times. 2 variables need to be created to hold the position of each of the 2 letters that are being switched.

2)Why do you put i-1 in return letters[i-1]?

The array is zero-indexed meaning the first value in the array is at letters[0]. Using i-1 allows you to get the 1st value in the array by using rLetter(1), rather than getting the 2nd value.

I would have personally done it like this:

var usedLetters = [];

var rLetter = function(i) {
  var alreadyUsed = 0;
  var letter;
  while(alreadyUsed >= 0) {
    var x = Math.floor(Math.random() * 26);
    letter = String.fromCharCode(65 + x);
    alreadyUsed = usedLetters.indexOf(letter);
  }
  usedLetters.push(letter);
  return letter;
};


window.onload = function () {
  //calls the function  
  $("square0").innerHTML = rLetter(1);
  $("square1").innerHTML = rLetter(2);
  $("square2").innerHTML = rLetter(3);
  $("square3").innerHTML = rLetter(4);
  $("square4").innerHTML = rLetter(5);
  $("square5").innerHTML = rLetter(6);
  $("square6").innerHTML = rLetter(7);
  $("square7").innerHTML = rLetter(8);
  $("square8").innerHTML = rLetter(9);

}

Sorry for any code errors. I have very little experience in JS.

Answer:2

If you actually want a random shuffle (at least algorithmically), you can't just do 26 random swaps of letters (as another answer did).

The result of that is biased - see Implementation errors in Fisher-Yates shuffle & also Jeff Atwood's post on naïveté.

Below, I've used Mike Bostock's implementation of the Fisher-Yates shuffle to produce an unbiased random ordering of letters (up to the limits of javascript's random number generator anyway).

//  create array of uppercase letters
var letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.split('');

// implentation of the Fischer-Yeates shuffle
// as written by Mike Bostock (http://bost.ocks.org/mike/shuffle/)
function shuffle(array) {
  var m = array.length, t, i;

  // While there remain elements to shuffle…
  while (m) {

    // Pick a remaining element…
    i = Math.floor(Math.random() * m--);

    // And swap it with the current element.
    t = array[m];
    array[m] = array[i];
    array[i] = t;
  }

  return array;
}

window.onload = function () {

    var shuffled = shuffle(letters);

    for (var i=0; i<9; i++) {
        $("#square" + i).html(shuffled[i]);
    }

}

Codepen here.

Answer:3

Probably not the best question ever, but I have a doubt here. Is there any difference writing this: var myFunction = function myFunction () {}; myFunction.instance = null; and this: var myFunction ...

Probably not the best question ever, but I have a doubt here. Is there any difference writing this: var myFunction = function myFunction () {}; myFunction.instance = null; and this: var myFunction ...

  1. javascript anonymous function with parameters
  2. javascript anonymous function this
  3. javascript anonymous function vs named
  4. javascript anonymous function return
  5. javascript anonymous function arrow
  6. javascript anonymous function example
  7. javascript anonymous function call
  8. javascript anonymous function scope
  9. javascript anonymous function return object
  10. javascript anonymous function callback
  11. javascript anonymous function syntax
  12. javascript anonymous function async
  13. javascript anonymous function arrow syntax
  14. javascript anonymous function es6
  15. javascript anonymous function closure
  16. javascript anonymous function object
  17. javascript anonymous function as argument
  18. javascript anonymous function onclick
  19. javascript anonymous function use
  20. javascript anonymous function foreach

I'm simply trying to append a json created dynamically into an array that lives inside the state. The only way that I've found till now is to use concat, because push doesn't work. constructor() {...

I'm simply trying to append a json created dynamically into an array that lives inside the state. The only way that I've found till now is to use concat, because push doesn't work. constructor() {...

  1. push json reactjs

I am using an AJAX script to submit a basic contact form at the bottom of my HTML that uses Google reCAPTCHA. The html code is: <div class="col-md-6"> <form role="form" id="form" ...

I am using an AJAX script to submit a basic contact form at the bottom of my HTML that uses Google reCAPTCHA. The html code is: <div class="col-md-6"> <form role="form" id="form" ...

Here is my code: var arr = [[[[[]]]]]; var c = 20; for (i=0;i<5;i++) arr[i][0][0][0] = c; alert(arr[2][0][0][0]); It doesn't work, but how can I do this?

Here is my code: var arr = [[[[[]]]]]; var c = 20; for (i=0;i<5;i++) arr[i][0][0][0] = c; alert(arr[2][0][0][0]); It doesn't work, but how can I do this?

  1. initialize array javascript with values
  2. initialize array javascript with size
  3. initialize array javascript with 0
  4. initialize array javascript empty
  5. initialize array javascript es6
  6. initialize array javascript object
  7. initialize 2d array javascript
  8. initialize multidimensional array javascript
  9. initialize new array javascript
  10. initialize array length javascript
  11. initialize json array javascript
  12. initialize string array javascript
  13. initialize associative array javascript
  14. initialize blank array javascript
  15. initialize boolean array javascript
  16. initialize null array javascript
  17. initialize double array javascript
  18. initialize dynamic array javascript
  19. initialize 3d array javascript
  20. re initialize array javascript