Escoute
Escoute

Reputation: 396

Generate random identical object

function getRandomInt(min, max) {
    return Math.floor(Math.random() * (max - min + 1)) + min;
}

function init(){
  var resArr = [];
  for (var i = 0; i < 10; i++) {
    var obj = new Object();
    obj.q = getRandomInt(3,5);
    obj.r = getRandomInt(1,10);
    resArr.push(obj);
  }

  var str = '<table border=1><th>Multiplication</th>';
  for (var i = 0; i < resArr.length; i++) {
    str+='<tr><td>'+resArr[i].q+' * '+resArr[i].r+' = '+(resArr[i].q *resArr[i].r)+'</td></tr>';
  }
  str += '</table>';
  document.getElementById('multiTable').innerHTML = str;
}
init();
<button type="button" name="button" onclick="init()">Refresh</button>
<div id='multiTable'></div>

Here I am generating random object and pushing to an array. I am really stuck how to check that created object is present or not in array.

I want to generate random number to show in multiplication table.

Upvotes: 3

Views: 109

Answers (3)

Mathiasfc
Mathiasfc

Reputation: 1697

I created a function simply to check for duplicate objects in the array

function getRandomInt(min, max) {
  return Math.floor(Math.random() * (max - min + 1)) + min;
}

function checkDuplicate(arr, obj) {
  for (var i = 0; i < arr.length; i++) {
    if (arr[i].q == obj.q && arr[i].r == obj.r) {
      return true;
      break;
    }
  }
}

function init() {
  var resArr = [];
  for (var i = 0; i < 10; i++) {
    var obj = new Object();
    obj.q = getRandomInt(3, 5);
    obj.r = getRandomInt(1, 10);
    if (!checkDuplicate(resArr, obj)) {
      resArr.push(obj);
    }
    else i--;
  }

  var str = '<table border=1><th>Multiplication</th>';
  for (var i = 0; i < resArr.length; i++) {
    str += '<tr><td>' + resArr[i].q + ' * ' + resArr[i].r + ' = ' + (resArr[i].q * resArr[i].r) + '</td></tr>';
  }
  str += '</table>';
  document.getElementById('multiTable').innerHTML = str;
}
init();
<button type="button" name="button" onclick="init()">Refresh</button>
<div id='multiTable'></div>

Upvotes: 4

prasanth
prasanth

Reputation: 22490

Try with Array#filter() method .check the matched object length .And change with while loop for always give a 10 result's

updated

function getRandomInt(min, max) {
  return Math.floor(Math.random() * (max - min + 1)) + min;
}

function init() {
  var resArr = [];
  var i=0;
  while (true) {
    var obj = new Object();
    obj.q = getRandomInt(3, 5);
    obj.r = getRandomInt(1, 10);
    if(!resArr.filter(a=> a.q == obj.q && a.r == obj.r).length>0){
       resArr.push(obj);
        i++;
    }
   
    if(i == 10){
    break;
    }
  }

  var str = '<table border=1><th>Multiplication</th>';
  for (var i = 0; i < resArr.length; i++) {
    str += '<tr><td>' + resArr[i].q + ' * ' + resArr[i].r + ' = ' + (resArr[i].q * resArr[i].r) + '</td></tr>';
  }
  str += '</table>';
  document.getElementById('multiTable').innerHTML = str;
}
init();
<button type="button" name="button" onclick="init()">Refresh</button>
<div id='multiTable'></div>

Upvotes: 1

Nina Scholz
Nina Scholz

Reputation: 386550

You could use a hash table and insert all random combination into it after checking if already inserted.

function getRandomInt(min, max) {
    return Math.floor(Math.random() * (max - min + 1)) + min;
}

function init() {
    var resArr = [],
        hash = {};

    for (var i = 0; i < 10; i++) {
        var obj = new Object();
        do {
            obj.q = getRandomInt(3, 5);
            obj.r = getRandomInt(1, 10);
        } while (hash[[obj.q, obj.r].join('|')])
        hash[[obj.q, obj.r].join('|')] = true;
        resArr.push(obj);
    }

    var str = '<table border=1><th>Multiplication</th>';
    for (var i = 0; i < resArr.length; i++) {
        str += '<tr><td>' + resArr[i].q + ' * ' + resArr[i].r + ' = ' + (resArr[i].q * resArr[i].r) + '</td></tr>';
    }
    str += '</table>';
    document.getElementById('multiTable').innerHTML = str;
}
init();
<button type="button" name="button" onclick="init()">Refresh</button>
<div id='multiTable'></div>

Upvotes: 1

Related Questions