Jquery functions with variable arrays -
i have function produces random number game. running function 8 times. instead of copying function 8 times , renaming variables, want streamline it.
function card1nums() { card1array = []; var iteration1=0; while (iteration1 < 25) { num1=getcardnum(); //look see if number exists if($.inarray(num1,card1array) == -1){ //number not found add array card1array.push(num1); iteration1++; } }return card1array; } function card2nums() { card2array = []; var iteration2=0; while (iteration2 < 25) { num2=getcardnum(); //look see if number exists if($.inarray(num2,card2array) == -1){ //number not found add 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 code above creating numbers card1. if 8 times, copy function , rename variable respectively card2nums, card3nums.....
i assumed alter code when call function cardnums() add parameter of x , call cardnums(x); , use x in array names array stored in database. card1array stored in card1 in database , forth.
i reading objects not quite sure way go. since repeatative function know there better way it. think?
edit: see going if 8 times. call 8 separate function exact same thing rename arrays , pick new numbers.
please this:
as concerns basic javascript-knowleage please ask if don't understand in comment below.
function cardnums() { var cardarray = []; var iteration1 = 0; while (iteration1 < 25) { num1 = getcardnum(); //look see if number exists if ($.inarray(num1, cardarray) == -1) { //number not found add 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, }); });
Comments
Post a Comment