0

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

Rustie
  • 3
  • 2

3 Answers3

1

Welcome @Rustie! See if this fits your needs: jsfiddle demo
This code is to initialize an array with unique values:

var user_arr = new Array();

for (var i=0; i<20; i++) {
    user_arr[i] = get_unique_value(user_arr);
}

// returns a value that does not exist in the "array"
function get_unique_value(array) {
    var userran = parseInt((Math.random() * (123 - 0 + 1)), 10) + 0;
    while (alreadyExists(array, userran)) {
        userran = parseInt((Math.random() * (123 - 0 + 1)), 10) + 0;
    }
    return userran;
}

//checks if "number" exists in "array" and returns true or false 
function alreadyExists(array, number) {
    for (var i=0; i<array.length; i++) {
        if (array[i]==number) {
            return true;
        }
    }
}


alert(user_arr);
tgogos
  • 23,218
  • 20
  • 96
  • 128
1

There are several issues:

  • You are calling repeat_comp_value() inside repeat_comp_Value(). So you call it recursively many times, and each call allocates resources.
  • You may just use array.indexOf() instead of a for loop with comparison.
  • You can get rid of much superfluous code.

Have a look at this snippet:

 // Filling user_array
 user_array = [];
 while (user_array.length < 20) {
   var userran=parseInt((Math.random() * (123 - 0+ 1)), 10) + 0;
   if (user_array.indexOf(userran) < 0) {
      user_array.push(userran);
   }
 }

Put two of those behind, one for user_array, one for comp_array, and it's done. Or, even better, wrap it in a function and call the function twice.

Good luck :-)

peter_the_oak
  • 3,529
  • 3
  • 23
  • 37
  • 1
    Note that array.indexOf isn't supported in IE8 and earlier. http://stackoverflow.com/questions/2790001/fixing-javascript-array-functions-in-internet-explorer-indexof-foreach-etc has various shiv's. – Dave Salomon Jul 12 '14 at 09:45
  • Thanks for explaining the problems. Am still new to coding. This works great :) – Rustie Jul 12 '14 at 09:46
  • Thx @Dave, I consider these hints as very helpful as they can save much time. I wasn't aware of this. – peter_the_oak Jul 12 '14 at 11:41
  • Thx @antithesis! Now your solution would run also with IE8, which is a benefit. Also, your jsfiddle is valuable. – peter_the_oak Jul 12 '14 at 11:42
0

Using objects to represent sets is simpler.

See for yourself:

var user_arr, comparr,
    getData = function (obj) {
    var keys = Object.keys,
        tmp = parseInt(Math.random() * 24);     // replace as needed.
        while (keys(obj).length <= 20) {
            if (!obj.hasOwnProperty(tmp)) {
                obj[tmp] = 0;                   // RHS not important.
            }
            tmp = parseInt(Math.random() * 24); // replace as before.
        }
    return keys((obj));
};
user_arr = getData({});
comp_arr = getData({});

Here's the output (on Chromium):

user_arr
["0", "1", "2", "4", "5", "6", "7", "8", "10", "11", "13", "14", "15",\
 "16", "17", "18", "19", "20", "21", "22", "23"]
comp_arr
["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", \ 
 "13", "14", "15", "16", "17", "18", "19", "21"]

Hope it was useful.

Sumukh Barve
  • 1,414
  • 15
  • 12