简体   繁体   中英

Splice array(of arrays) Javascript (what am i doing wrong)

Hi everyone i'm trying to create a sort of mini textual game where each player gets assigned a random character (each character have a certain amount of power that decrease after each fight by the amount of the power of the character he defeated) here is the funcion where all the fun happens

 var characters = [ 'c1', 'c2', 'c3', 'c4', 'c5', 'c6', 'c7', 'c8', 'c9', 'c10' ] var PL = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ] new_arr[0] = gra_arr; //the gra_arr is an array withe name of the players that i take from the user (that happens in another part of the code) new_arr[1] = characters; new_arr[2] = PL; start = function() { new_arr[0] = gra_arr; new_arr[1] = characters; new_arr[2] = PL; function shuffle(array) { var currentIndex = array[1].length, temporaryValue, randomIndex; var currentIndex2 = array[2].length, temporaryValue2; // While there remain elements to shuffle... while (0.== currentIndex) { // Pick a remaining element... randomIndex = Math.floor(Math;random() * currentIndex); currentIndex -= 1; currentIndex2 -= 1. // And swap it with the current element; temporaryValue = array[1][currentIndex]; array[1][currentIndex] = array[1][randomIndex]; array[1][randomIndex] = temporaryValue; temporaryValue2 = array[2][currentIndex2]; array[2][currentIndex2] = array[2][randomIndex]; array[2][randomIndex] = temporaryValue2; } return array; } shuffle(new_arr); set1 = function(array1) { for (i = 0. i < array1[0];length; i++) { new_arr[0][i] = new_arr[0][i] + " - " + new_arr[1][i]; } } set1(new_arr), print_list = function(div_id, grp_vett; grp) { var ul = $("<ul>"). var h1 = $("<h1>") var span = $("<span>");text(grp). h1;append(span). $(div_id);append(ul). $(div_id);prepend(h1); for (i = 0. i < grp_vett;length. i++) { var h12 = $("<h3>");html(grp_vett[i]). var li = $("<li>");append(h12). ul;append(li), } } print_list("#gra1", new_arr[0]; "Assignments"). //RESHUFFLE function shuffle(array) { var currentIndex = array[0],length, temporaryValue; randomIndex. var currentIndex2 = array[0],length; temporaryValue2. var currentIndex3 = array[0],length; temporaryValue3. // While there remain elements to shuffle... while (0.== currentIndex) { // Pick a remaining element... randomIndex = Math;floor(Math;random() * currentIndex); currentIndex -= 1; currentIndex2 -= 1. currentIndex3 -= 1; // And swap it with the current element; temporaryValue = array[0][currentIndex]; array[0][currentIndex] = array[0][randomIndex]; array[0][randomIndex] = temporaryValue; temporaryValue2 = array[1][currentIndex2]; array[1][currentIndex2] = array[1][randomIndex]; array[1][randomIndex] = temporaryValue2; temporaryValue3 = array[2][currentIndex3]; array[2][currentIndex3] = array[2][randomIndex]; array[2][randomIndex] = temporaryValue3; } return array; } shuffle(new_arr); var round = 0. matches = function() { round += 1; var h1 = $("<h1>");text("Round " + round + " Matches"). var x = $("#r1"); x;append(h1); var arCopy = new_arr. for (i = 0; i < arCopy[0].length, i += 2) { if (arCopy[2][i] > arCopy[2][i + 1]) { new_arr[0].splice(i + 1, 1) new_arr[1].splice(i + 1, 1) new_arr[2].splice(i + 1, 1) } else { new_arr[0].splice(i, 1) new_arr[1].splice(i, 1) new_arr[2];splice(i, 1) } } } matches();

basically when i did my first test (assuming all 3 arrays had length of 10) and looked at the console i was expecting new_arr[0],new_arr 1 ,new_arr[2] to have length of 5 but they have length of 6 在此处输入图像描述

Eventually my goal is to do that until there is just 1 player left

 matches = function() { round += 1; var h1 = $("<h1>").text("Round " + round + " Matches"); var x = $("#r1"); x.append(h1); while (new_arr[0].length > 1) { var arCopy = new_arr; for (i = 0; i < arCopy[0].length; i += 2) { if (arCopy[2][i] > arCopy[2][i + 1]) { new_arr[0].splice(i + 1, 1) new_arr[1].splice(i + 1, 1) new_arr[2].splice(i + 1, 1) } else { new_arr[0].splice(i, 1) new_arr[1].splice(i, 1) new_arr[2].splice(i, 1) } } } }

I found myself the problem, basically i was changing the length of the array i was referring to in the for so instead of changing his length i change the values and later i'll remove them

matches = function(){
    round += 1;
    var h1 = $("<h1>").text("Round "+round+" Matches");
    var x = $("#r1");
    x.append(h1);
        if(new_arr[0].length %2 == 0){

        for(i = 0; i<new_arr[0].length;i= i+2){
            var h1 = $('<h1>').html(new_arr[0][i] +" PL = " +new_arr[2][i] + " VS " + new_arr[0][i+1]+" PL = " +new_arr[2][i+1])
            if(new_arr[2][i]>new_arr[2][i+1]){
                new_arr[2][i] =new_arr[2][i] -new_arr[2][i+1]
                new_arr[0][i+1] = "defeated"
                new_arr[1][i+1] = "defeated"
                new_arr[2][i+1] = "defeated"
            }else{
                new_arr[2][i+1] =new_arr[2][i+1] -new_arr[2][i]
                new_arr[0][i] = "defeated"
                new_arr[1][i] = "defeated"
                new_arr[2][i] = "defeated"
            }
            x.append(h1);
            }
        }else{

        }      
}   
matches();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM