I am trying to compare a random value with elements of an array. The logic is to reload the value if that particular value already exists in the array in order to create an array containing all unique elements. The is done for a user array and a comp array.
Once a unique element is verified, that is pushed to the array and repeated for another 20 times.
My emulator says there may be too much processing going on.This is my code
var user_arr=new Array();
var comp_arr=new Array();
function getData()
{
for(var i=1;i<=20;i++)
{
repeat_user_value();
function repeat_user_value()
{
var userran=parseInt((Math.random() * (123 - 0+ 1)), 10) + 0;
for(var j=0;j<20;j++)
{
if(userran==user_arr[j])
{
repeat_user_value();
}
}
}
user_arr.push(userran);
repeat_comp_value();
function repeat_comp_value()
{
var compran=parseInt((Math.random() * (123 - 0+ 1)), 10) + 0;
for(var j=0;j<20;j++)
{
if(compran==comp_arr[j])
{
repeat_comp_value();
}
}
}
comp_arr.push(compran);
}
localStorage['userCards']=JSON.stringify(user_arr);
localStorage['compCards']=JSON.stringify(comp_arr);
window.location = 'file:///android_asset/www/game.html'; // PSEUDO LOAD USER CARD
}
The code worked without the comparison. It was
var userran=parseInt((Math.random() * (123 - 0+ 1)), 10) + 0;
user_arr.push(userran);
var compran=parseInt((Math.random() * (123 - 0+ 1)), 10) + 0;
comp_arr.push(compran);
Thank you