0

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.

5
  • what issue you are facing? Commented Jan 28, 2016 at 12:40
  • I can't see how a "card2Nums" would be different? Where would you change it? Commented Jan 28, 2016 at 12:41
  • I added more code so you can see where this is going. If I do it 8 times, it will get quite long. I am sure there is no need for it. Commented Jan 28, 2016 at 12:47
  • ah ok, I see. You got confused with global scope. I'll write it how you should use it properly Commented Jan 28, 2016 at 12:48
  • with Global scope you are writing an object to the window? I thought about that but simply can not get it. Commented Jan 28, 2016 at 12:50

1 Answer 1

1

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,
    });



  });

Sign up to request clarification or add additional context in comments.

2 Comments

Holy smokes, isn't it funny how we make things so complicated sometimes !! Thanks guy for pointing that out.
Yes we all do this sometimes ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.