FunnyGirl
FunnyGirl

Reputation: 27

Jquery functions with variable arrays

I have a function that produces random number for a game. I am running this function 8 times. Instead of copying the function 8 times and renaming the variables, I want to streamline it.

    function card1Nums() {
            card1Array = [];
            var iteration1=0;
            while (iteration1 < 25) {
                num1=getCardNum();  
                //Look to see if number exists
                if($.inArray(num1,card1Array) == -1){
                    //Number not found so add it to array
                    card1Array.push(num1);
                    iteration1++;
                }
            }return card1Array;
        }

        function card2Nums() {
            card2Array = [];
            var iteration2=0;
            while (iteration2 < 25) {
                num2=getCardNum();  
                //Look to see if number exists
                if($.inArray(num2,card2Array) == -1){
                    //Number not found so add it to array
                    card2Array.push(num2);
                    iteration2++;
                }
            }return card2Array;
        }


        $("#draw").click(function() {
            drawNumbers();
            card1Nums();
            card2Nums();

            var jsonDealerArray = JSON.stringify(drawArray);
            var jsonCard1Array = JSON.stringify(card1Array);
            var jsonCard2Array = JSON.stringify(card2Array);

            $.ajax({
                url: "includes/dealerpicks.php",
                type: 'POST',
                data: {dealer:jsonDealerArray, 
                        card1:jsonCard1Array, 
                        card2:jsonCard2Array, 
                        },
                dataType: "json",
                async: false,
                cache: false,
            });



        });

In the code above I am creating numbers for card1. If I do this eight times, then I will copy this function and rename the variable respectively as card2Nums, card3Nums.....

I assumed that i could alter this code so that when I call the function cardNums() i could add a parameter of x and thus call it like this cardNums(x); and then use the x in the array names as the array is stored in the database. so card1Array will be stored in card1 in the database and so forth.

I was reading about objects but am not quite sure that is the way to go. Since this is a repeatative function I know there is a better way to do it. What do you think?

edit: SO you see where this is going if I do this 8 times. I will call 8 separate function that essentially all do the exact same thing but just rename the arrays and pick new numbers.

Upvotes: 0

Views: 32

Answers (1)

CoderPi
CoderPi

Reputation: 13211

Please do it like this:

As it only concerns basic JavaScript-Knowleage please ask if you don't understand something in a comment below.

  function cardNums() {
    var cardArray = [];
    var iteration1 = 0;
    while (iteration1 < 25) {
      num1 = getCardNum();
      //Look to see if number exists
      if ($.inArray(num1, cardArray) == -1) {
        //Number not found so add it to array
        cardArray.push(num1);
        iteration1++;
      }
    }
    return cardArray;
  }


  $("#draw").click(function() {
    drawNumbers();
    var card1Array = cardNums();
    var card2Array = cardNums();
    //var card3Array = cardNums();
    //var card4Array = cardNums();
    //var card5Array = cardNums();
    //var card6Array = cardNums();
    //var card7Array = cardNums();
    //var card8Array = cardNums();

    var jsonDealerArray = JSON.stringify(drawArray);
    var jsonCard1Array = JSON.stringify(card1Array);
    var jsonCard2Array = JSON.stringify(card2Array);

    $.ajax({
      url: "includes/dealerpicks.php",
      type: 'POST',
      data: {
        dealer: jsonDealerArray,
        card1: jsonCard1Array,
        card2: jsonCard2Array,
      },
      dataType: "json",
      async: false,
      cache: false,
    });



  });

Upvotes: 1

Related Questions