Hi,

I'm writing a function to process the output of the Runescape Hiscores (Lite version). The idea is that it is fed the raw hiscore data, it removes the rank and xp columns and evaluates each level - if the level is -1 it turns the number into a text input field, otherwise it turns it into a hidden input field. It then returns a string of the input fields.

Basically, it isn't. I don't know where the problem is or what it is. Here's the code:

Spoiler: Click to Toggle the Spoiler.

CODE
function processRawHiscores(i) {

     function combineArray(a,b) {
    
         //same result as PHP's array_combine();
    
         var combinedArray = new Array();
                    
         if(a.length != b.length) {
             return "Error - a and b are of different lengths.";
         }
         else {
                    
             for(k = 0; k < a.length; k++) {
                 var combinedArray[a[k]] = b[k];
             }
            
             return combinedArray;
                        
         }
     }
                
     var arrayOfKeys[0] = "Combat"; //Add Combat to the list of skills
     var arrayOfKeys[1] = "Overall"; //RS Hiscores give levels from here
     var arrayOfKeys[3] = "Attack";
     var arrayOfKeys[4] = "Defence";
     var arrayOfKeys[5] = "Strength";
     var arrayOfKeys[6] = "Hitpoints";
     var arrayOfKeys[7] = "Ranged";
     var arrayOfKeys[8] = "Prayer";
     var arrayOfKeys[9] = "Magic";
     var arrayOfKeys[10] = "Cooking";
     var arrayOfKeys[11] = "Woodcutting";
     var arrayOfKeys[12] = "Fletching";
     var arrayOfKeys[13] = "Fishing";
     var arrayOfKeys[14] = "Firemaking";
     var arrayOfKeys[15] = "Crafting";
     var arrayOfKeys[16] = "Smithing";
     var arrayOfKeys[17] = "Mining";
     var arrayOfKeys[18] = "Herblore";
     var arrayOfKeys[19] = "Agility";
     var arrayOfKeys[20] = "Thieving";
     var arrayOfKeys[21] = "Slayer";
     var arrayOfKeys[22] = "Farming";
     var arrayOfKeys[23] = "Runecrafting";
     var arrayOfKeys[24] = "Hunter";
     var arrayOfKeys[25] = "Construction";
     var arrayOfKeys[26] = "Summoning";
    
     var arrayOfVals = i.split("\n"); //split raw hiscores into chunks by line
     var arrayOfVals.unshift("-1,-1,-1"); //prepend numbers to treat Combat (the first element in both arrays) as an unknown level
     var arrayOfVals.pop(); //--|
     var arrayOfVals.pop(); //  |
     var arrayOfVals.pop(); // remove lines associated with minigames
     var arrayOfVals.pop(); //  |
     var arrayOfVals.pop(); //  |
     var arrayOfVals.pop(); //--|
    
     for(j in arrayOfVals) {
    
         var arrayOfVals[j] = arrayOfVals[j].split(","); //split each line into its constitutional parts
         delete arrayOfVals[j][0]; //remove rank
         delete arrayOfVals[j][2]; //remove xp
         var arrayOfVals[j].join(""); //turn back from array to string
        
     }

     var newArray = combineArray(arrayOfKeys,arrayOfVals); //the keys are defined in that long list and values are the levels of each skill
    
     for(key in newArray) {
                
         var value = newArray[key]; //replaces PHP's "=>"

         if(value == -1) {
             var newArray[key] = "<div>" + key + ": <input type=\"text\" name=\"" + key + "\" /></div>"; //input field if no level number found
         }
         else {
             var newArray[key] = "<input type=\"hidden\" name=\"" + key + "\" value=\"" + value + "\" />"; //hidden input if level number found
         }
     }
                
     return newArray.join(""); //transform from array to string
                
}



[Close]


The JS code is translated from my working PHP implementation - I'm definitely missing something, no clue as to what it is. Here is the PHP code:

Spoiler: Click to Toggle the Spoiler.

CODE
<?php

function get_hiscores($username) {

     $hiscores = file_get_contents("http://hiscore.runescape.com/index_lite.ws?player=" . $username);
    
     $arrayOfVals = explode("\n", $hiscores);
     unset($arrayOfVals[25], $arrayOfVals[26], $arrayOfVals[27], $arrayOfVals[28], $arrayOfVals[29], $arrayOfVals[30]);
    
     foreach($arrayOfVals as $key => $value) {
         $arrayOfVals[$key] = explode(",", $value);
         unset($arrayOfVals[$key][0], $arrayOfVals[$key][2]);
     }

     $arrayOfKeys = array("Overall", "Attack", "Defence", "Strength", "Hitpoints", "Ranged", "Prayer", "Magic", "Cooking", "Woodcutting", "Fletching", "Fishing", "Firemaking", "Crafting", "Smithing", "Mining", "Herblore", "Agility", "Thieving", "Slayer", "Farming", "Runecrafting", "Hunter", "Construction", "Summoning");

     $array = array_combine($arrayOfKeys, $arrayOfVals);

     foreach($array as $key => $value) {
         if($value[1] == -1) {
             echo "<div><label for=\"" . $key . "Input\">" . $key . ":</label> <input type=\"text\" name=\"" . $key . "\" id=\"" . $key . "Input\" /> <button type=\"button\" onclick=\"document.getElementById('" . $key . "Input').disabled='true';\">Confirm</button></div>\n";
         }
         else {
             echo "<input type=\"hidden\" name=\"" . $key . "\" value=\"" . $value[1] . "\" />\n";
         }
     }
    
}



[Close]


Help please! One day I'll finally grasp Javascript, but, until then, I'm a coding noob happy.gif

Thanks in advance smile.gif