SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    Scary's On The Wall
    Join Date
    Apr 2003
    Location
    PA
    Posts
    518
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    JavaScript Multidimensional Arrays

    I'm having trouble with JavaScript's Multidimensional arrays...

    Take a gander at this code:

    Code:
    1        var fightData = new Array();
    2        var fightData[0] = new Array();
    3        fightData[0]["round"] = "5";
    4        fightData[0]["attacker"] = "Attacker";
    5        fightData[0]["defender"] = "Defender";
    6        fightData[0]["attacker_possessive"] = "Your";
    7        fightData[0]["defender_possessive"] = "your";
    8        fightData[0]["min_roll"] = "334";
    9        fightData[0]["this_roll"] = "122";
    10      fightData[0]["attack_type"] = "casting";
    11      fightData[0]["attack_damage"] = "-1";
    12      fightData[1] = new Array();
    13      fightData[1]["round"] = "5";
             ...
    A similar structure would be used for fightData[1] and fightData[2] etc.

    But I'm getting the error "Expected ';'" on the second line. I can't figure out what I'm doing wrong, I've done so many Google searches, I just had to resort to Site Point. Thanks in advance for the help.

  2. #2
    Scary's On The Wall
    Join Date
    Apr 2003
    Location
    PA
    Posts
    518
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Actually, I think I may have fixed the above. But the script as a whole doesn't work now (even though I'm not getting any errors).

    Code:
    <html><head><title>Test Game</title></head><body><h1>Attacker vs. Defender</h1><br><script language="JavaScript">
            var fightData = new Array();
            fightData[0] = new Array();
            fightData[0]["round"] = 5;
                fightData[0]["attacker"] = "Attacker";
                fightData[0]["defender"] = "Defender";
                fightData[0]["attacker_possessive"] = "Your";
                fightData[0]["defender_possessive"] = "your";
                fightData[0]["min_roll"] = "334";
                fightData[0]["this_roll"] = "234";
                fightData[0]["attack_type"] = "casting";
                fightData[0]["attack_damage"] = -1;
                fightData[1] = new Array();
            fightData[1]["round"] = 8;
                fightData[1]["attacker"] = "Attacker";
                fightData[1]["defender"] = "Defender";
                fightData[1]["attacker_possessive"] = "Your";
                fightData[1]["defender_possessive"] = "your";
                fightData[1]["min_roll"] = "334";
                fightData[1]["this_roll"] = "453";
                fightData[1]["attack_type"] = "melee";
                fightData[1]["attack_damage"] = 30;
                fightData[1]["defender_health"] = 70;
                fightData[2] = new Array();
            fightData[2]["round"] = 8;
                fightData[2]["attacker"] = "Defender";
                fightData[2]["defender"] = "Attacker";
                fightData[2]["attacker_possessive"] = "Defender's";
                fightData[2]["defender_possessive"] = "Attacker's";
                fightData[2]["min_roll"] = "334";
                fightData[2]["this_roll"] = "215";
                fightData[2]["attack_type"] = "melee";
                fightData[2]["attack_damage"] = -1;
                fightData[3] = new Array();
            fightData[3]["round"] = 8;
                fightData[3]["attacker"] = "Defender";
                fightData[3]["defender"] = "Attacker";
                fightData[3]["attacker_possessive"] = "Defender's";
                fightData[3]["defender_possessive"] = "Attacker's";
                fightData[3]["min_roll"] = "334";
                fightData[3]["this_roll"] = "894";
                fightData[3]["attack_type"] = "casting";
                fightData[3]["attack_damage"] = 23;
                fightData[3]["defender_health"] = 77;
                fightData[4] = new Array();
            fightData[4]["round"] = 10;
                fightData[4]["attacker"] = "Attacker";
                fightData[4]["defender"] = "Defender";
                fightData[4]["attacker_possessive"] = "Your";
                fightData[4]["defender_possessive"] = "your";
                fightData[4]["min_roll"] = "334";
                fightData[4]["this_roll"] = "770";
                fightData[4]["attack_type"] = "casting";
                fightData[4]["attack_damage"] = 12;
                fightData[4]["defender_health"] = 58;
                fightData[5] = new Array();
            fightData[5]["round"] = 15;
                fightData[5]["attacker"] = "Attacker";
                fightData[5]["defender"] = "Defender";
                fightData[5]["attacker_possessive"] = "Your";
                fightData[5]["defender_possessive"] = "your";
                fightData[5]["min_roll"] = "334";
                fightData[5]["this_roll"] = "53";
                fightData[5]["attack_type"] = "casting";
                fightData[5]["attack_damage"] = -1;
                fightData[6] = new Array();
            fightData[6]["round"] = 16;
                fightData[6]["attacker"] = "Attacker";
                fightData[6]["defender"] = "Defender";
                fightData[6]["attacker_possessive"] = "Your";
                fightData[6]["defender_possessive"] = "your";
                fightData[6]["min_roll"] = "334";
                fightData[6]["this_roll"] = "888";
                fightData[6]["attack_type"] = "melee";
                fightData[6]["attack_damage"] = 44;
                fightData[6]["defender_health"] = 14;
                fightData[7] = new Array();
            fightData[7]["round"] = 16;
                fightData[7]["attacker"] = "Defender";
                fightData[7]["defender"] = "Attacker";
                fightData[7]["attacker_possessive"] = "Defender's";
                fightData[7]["defender_possessive"] = "Attacker's";
                fightData[7]["min_roll"] = "334";
                fightData[7]["this_roll"] = "646";
                fightData[7]["attack_type"] = "melee";
                fightData[7]["attack_damage"] = 24;
                fightData[7]["defender_health"] = 53;
                fightData[8] = new Array();
            fightData[8]["round"] = 16;
                fightData[8]["attacker"] = "Defender";
                fightData[8]["defender"] = "Attacker";
                fightData[8]["attacker_possessive"] = "Defender's";
                fightData[8]["defender_possessive"] = "Attacker's";
                fightData[8]["min_roll"] = "334";
                fightData[8]["this_roll"] = "105";
                fightData[8]["attack_type"] = "casting";
                fightData[8]["attack_damage"] = -1;
                fightData[9] = new Array();
            fightData[9]["round"] = 20;
                fightData[9]["attacker"] = "Attacker";
                fightData[9]["defender"] = "Defender";
                fightData[9]["attacker_possessive"] = "Your";
                fightData[9]["defender_possessive"] = "your";
                fightData[9]["min_roll"] = "334";
                fightData[9]["this_roll"] = "940";
                fightData[9]["attack_type"] = "casting";
                fightData[9]["attack_damage"] = 17;
                fightData[9]["defender_health"] = 0;
                setTimeout("results.innerHTML += 'You win! ';", 21000);
                    
    
                // Loop through all of the events
                for (var i = 0; i < fightData.length; i++) {
    
                    // Construct the roll_string
                    roll_string = "[Roll: " + fightData[i]["this_roll"] + "/" + fightData[i]["min_roll"] + "]";
    
                        // Construct the attack_string
                        switch (fightData[i]["attack_type"]) {
    
                    		case "melee":
    
                    			if (fightData[i]["attack_damage"] >= 0) {
    
                    			    attack_string = "<font color=\"red\"><b>" + fightData[i]["attacker"] + " hit " + fightData[i]["defender"] + " for " + fightData[i]["attack_damage"] + " damage!</b></font>";
    
                    			} else {
    
                    				attack_string = "<font color=\"red\">" + fightData[i]["attacker"] + " swung at " + fightData[i]["defender"] + " but <b>missed!</b></font>";
    
                    			}
    
                    			break;
    
                            case "casting":
    
                                if (fightData[i]["attack_damage"] >= 0) {
    
                    			    attack_string = "<font color=\"blue\"><b>" + fightData[i]["attacker"] + " cast on " + fightData[i]["defender"] + " for " + fightData[i]["attack_damage"] + " damage!</b></font>";
    
                                } else {
    
                                    attack_string = "<font color=\"blue\">" + fightData[i]["attacker_possessive"] + " spell <b>fizzled!</b></font>";
    
                                }
    
                                break;
    
                        }
    
                    // Construct the result_string, which will be echoed in "results"
                    result_string = roll_string + " " + attack_string;
    
    
                    delay = fightData[i]["round"] * 1000;
    
                    result_string = i;
    
                    delay = 1000;
    
                // Add the string to "results"
                setTimeout("results.innerHTML += result_string + \"<br><br>\";", delay);
    
                }
    
            </script><span id="results"></span><span id="results"></span></body></html>

  3. #3
    SitePoint Addict
    Join Date
    May 2004
    Location
    Europe
    Posts
    216
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    result_string = i;
    Here you are overwriting the result_string you just constructed.
    Code:
    setTimeout("results.innerHTML += result_string + \"<br><br>\";", delay);
    During this timeout the for loop continues to execute and overwrites result_string again.

  4. #4
    Scary's On The Wall
    Join Date
    Apr 2003
    Location
    PA
    Posts
    518
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Fixed that... but still isn't working. I have a strong hunch that its because there's something wrong with my setTimeout()...

    Code:
    <html><head><title>Test Game</title></head><body><h1>Attacker vs. Defender</h1><br><script language="JavaScript">
            var fightData = new Array();
            fightData[0] = new Array();
            fightData[0]["round"] = 5;
                fightData[0]["attacker"] = "Attacker";
                fightData[0]["defender"] = "Defender";
                fightData[0]["attacker_possessive"] = "Your";
                fightData[0]["defender_possessive"] = "your";
                fightData[0]["min_roll"] = "334";
                fightData[0]["this_roll"] = "104";
                fightData[0]["attack_type"] = "casting";
                fightData[0]["attack_damage"] = -1;
                fightData[1] = new Array();
            fightData[1]["round"] = 8;
                fightData[1]["attacker"] = "Attacker";
                fightData[1]["defender"] = "Defender";
                fightData[1]["attacker_possessive"] = "Your";
                fightData[1]["defender_possessive"] = "your";
                fightData[1]["min_roll"] = "334";
                fightData[1]["this_roll"] = "575";
                fightData[1]["attack_type"] = "melee";
                fightData[1]["attack_damage"] = 28;
                fightData[1]["defender_health"] = 72;
                fightData[2] = new Array();
            fightData[2]["round"] = 8;
                fightData[2]["attacker"] = "Defender";
                fightData[2]["defender"] = "Attacker";
                fightData[2]["attacker_possessive"] = "Defender's";
                fightData[2]["defender_possessive"] = "Attacker's";
                fightData[2]["min_roll"] = "334";
                fightData[2]["this_roll"] = "564";
                fightData[2]["attack_type"] = "melee";
                fightData[2]["attack_damage"] = 29;
                fightData[2]["defender_health"] = 71;
                fightData[3] = new Array();
            fightData[3]["round"] = 8;
                fightData[3]["attacker"] = "Defender";
                fightData[3]["defender"] = "Attacker";
                fightData[3]["attacker_possessive"] = "Defender's";
                fightData[3]["defender_possessive"] = "Attacker's";
                fightData[3]["min_roll"] = "334";
                fightData[3]["this_roll"] = "873";
                fightData[3]["attack_type"] = "casting";
                fightData[3]["attack_damage"] = 35;
                fightData[3]["defender_health"] = 36;
                fightData[4] = new Array();
            fightData[4]["round"] = 10;
                fightData[4]["attacker"] = "Attacker";
                fightData[4]["defender"] = "Defender";
                fightData[4]["attacker_possessive"] = "Your";
                fightData[4]["defender_possessive"] = "your";
                fightData[4]["min_roll"] = "334";
                fightData[4]["this_roll"] = "716";
                fightData[4]["attack_type"] = "casting";
                fightData[4]["attack_damage"] = 17;
                fightData[4]["defender_health"] = 55;
                fightData[5] = new Array();
            fightData[5]["round"] = 15;
                fightData[5]["attacker"] = "Attacker";
                fightData[5]["defender"] = "Defender";
                fightData[5]["attacker_possessive"] = "Your";
                fightData[5]["defender_possessive"] = "your";
                fightData[5]["min_roll"] = "334";
                fightData[5]["this_roll"] = "888";
                fightData[5]["attack_type"] = "casting";
                fightData[5]["attack_damage"] = 15;
                fightData[5]["defender_health"] = 40;
                fightData[6] = new Array();
            fightData[6]["round"] = 16;
                fightData[6]["attacker"] = "Defender";
                fightData[6]["defender"] = "Attacker";
                fightData[6]["attacker_possessive"] = "Defender's";
                fightData[6]["defender_possessive"] = "Attacker's";
                fightData[6]["min_roll"] = "334";
                fightData[6]["this_roll"] = "36";
                fightData[6]["attack_type"] = "melee";
                fightData[6]["attack_damage"] = -1;
                fightData[7] = new Array();
            fightData[7]["round"] = 16;
                fightData[7]["attacker"] = "Defender";
                fightData[7]["defender"] = "Attacker";
                fightData[7]["attacker_possessive"] = "Defender's";
                fightData[7]["defender_possessive"] = "Attacker's";
                fightData[7]["min_roll"] = "334";
                fightData[7]["this_roll"] = "76";
                fightData[7]["attack_type"] = "casting";
                fightData[7]["attack_damage"] = -1;
                fightData[8] = new Array();
            fightData[8]["round"] = 16;
                fightData[8]["attacker"] = "Attacker";
                fightData[8]["defender"] = "Defender";
                fightData[8]["attacker_possessive"] = "Your";
                fightData[8]["defender_possessive"] = "your";
                fightData[8]["min_roll"] = "334";
                fightData[8]["this_roll"] = "201";
                fightData[8]["attack_type"] = "melee";
                fightData[8]["attack_damage"] = -1;
                fightData[9] = new Array();
            fightData[9]["round"] = 20;
                fightData[9]["attacker"] = "Attacker";
                fightData[9]["defender"] = "Defender";
                fightData[9]["attacker_possessive"] = "Your";
                fightData[9]["defender_possessive"] = "your";
                fightData[9]["min_roll"] = "334";
                fightData[9]["this_roll"] = "570";
                fightData[9]["attack_type"] = "casting";
                fightData[9]["attack_damage"] = 15;
                fightData[9]["defender_health"] = 25;
                fightData[10] = new Array();
            fightData[10]["round"] = 24;
                fightData[10]["attacker"] = "Defender";
                fightData[10]["defender"] = "Attacker";
                fightData[10]["attacker_possessive"] = "Defender's";
                fightData[10]["defender_possessive"] = "Attacker's";
                fightData[10]["min_roll"] = "334";
                fightData[10]["this_roll"] = "987";
                fightData[10]["attack_type"] = "melee";
                fightData[10]["attack_damage"] = 31;
                fightData[10]["defender_health"] = 5;
                fightData[11] = new Array();
            fightData[11]["round"] = 24;
                fightData[11]["attacker"] = "Defender";
                fightData[11]["defender"] = "Attacker";
                fightData[11]["attacker_possessive"] = "Defender's";
                fightData[11]["defender_possessive"] = "Attacker's";
                fightData[11]["min_roll"] = "334";
                fightData[11]["this_roll"] = "629";
                fightData[11]["attack_type"] = "casting";
                fightData[11]["attack_damage"] = 31;
                fightData[11]["defender_health"] = 0;
                setTimeout("results.innerHTML += 'You lose. ';", 25000);
                    
    
                // Loop through all of the events
                for (var i = 0; i < fightData.length; i++) {
    
                    // Construct the roll_string
                    roll_string = "[Roll: " + fightData[i]["this_roll"] + "/" + fightData[i]["min_roll"] + "]";
    
                        // Construct the attack_string
                        switch (fightData[i]["attack_type"]) {
    
                    		case "melee":
    
                    			if (fightData[i]["attack_damage"] >= 0) {
    
                    			    attack_string = "<font color=\"red\"><b>" + fightData[i]["attacker"] + " hit " + fightData[i]["defender"] + " for " + fightData[i]["attack_damage"] + " damage!</b></font>";
    
                    			} else {
    
                    				attack_string = "<font color=\"red\">" + fightData[i]["attacker"] + " swung at " + fightData[i]["defender"] + " but <b>missed!</b></font>";
    
                    			}
    
                    			break;
    
                            case "casting":
    
                                if (fightData[i]["attack_damage"] >= 0) {
    
                    			    attack_string = "<font color=\"blue\"><b>" + fightData[i]["attacker"] + " cast on " + fightData[i]["defender"] + " for " + fightData[i]["attack_damage"] + " damage!</b></font>";
    
                                } else {
    
                                    attack_string = "<font color=\"blue\">" + fightData[i]["attacker_possessive"] + " spell <b>fizzled!</b></font>";
    
                                }
    
                                break;
    
                        }
    
                    // Construct the result_string, which will be echoed in "results"
                    result_string = roll_string + " " + attack_string + "<br><br>";
    
                    delay = fightData[i]["round"] * 1000;
    
                // Add the string to "results"
                setTimeout("results.innerHTML += result_string;",delay);
    
                }
    
            </script><span id="results"></span><span id="results"></span></body></html>
    If I replace my setTimeout with a document.write, it works. I can't figure out why.

  5. #5
    Scary's On The Wall
    Join Date
    Apr 2003
    Location
    PA
    Posts
    518
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Disreguard this topic, I've found the problem, and its no longer related to the current name of this thread.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •