Jump to content

[SOLVED] array_count_values javascript equivalent not working


simpli

Recommended Posts

Hi,

I downloaded this javascript that is supposed to be the equivalent of php's array_count_values function but it doesn't seem to be working. While I posted on the website I downloaded it from I don't know how responsive they are. I am on a mac using firefox so I don't know if that's the problem. I am using one of their example to showcase the function. Have anyone used that function on a mac/firefox? (http://phpjs.org/functions/view/308) Can anyone try it on their side to see if it works as it should? If not can anyone help me troubleshoot it? I wanted to find a solution to equal the php function and be quick on my way but I'm stuck trying to debug this.

 

Thanks in advance,

J-R

sorry I forgot to paste the code:

<script type='text/javascript'>

function array_count_values( array ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Ates Goral (http://magnetiq.com)
    // + namespaced by: Michael White (http://getsprink.com)
    // +      input by: sankai
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // *     example 1: array_count_values([ 3, 5, 3, "foo", "bar", "foo" ]);
    // *     returns 1: {3:2, 5:1, "foo":2, "bar":1}
    // *     example 2: array_count_values({ p1: 3, p2: 5, p3: 3, p4: "foo", p5: "bar", p6: "foo" });
    // *     returns 2: {3:2, 5:1, "foo":2, "bar":1}
    // *     example 3: array_count_values([ true, 4.2, 42, "fubar" ]);
    // *     returns 3: {42:1, "fubar":1}

    var tmp_arr = {}, key = '', t = '';
    
    var __getType = function(obj) {
        // Objects are php associative arrays.
        var t = typeof obj;
        t = t.toLowerCase();
        if (t == "object") {
            t = "array";
        }
        return t;
    }    

    var __countValue = function (value) {
        switch (typeof(value)) {
            case "number":
                if (Math.floor(value) != value) {
                    return;
                }
            case "string":
                if (value in this) {
                    ++this[value];
                } else {
                    this[value] = 1;
                }
        }
    };
    
    t = __getType(array);
    if (t == 'array') {
        for ( key in array ) {
            __countValue.call(tmp_arr, array[key]);
        }
    } 
    return tmp_arr;
}


function formValidator(){
var tmpArray= [];
tmpArray = array_count_values([ 3, 5, 3, "foo", "bar", "foo" ]);
document.write(tmpArray[0]  + tmpArray[1]+ tmpArray[2]);
document.write(tmpArray[3]  + tmpArray[4]+ tmpArray[5]);
//	document.write(tmpArray[0] + " " + tmpArray[1]+ " " + tmpArray[2] + " " + tmpArray[3]+ " " + tmpArray[4]+ " " + tmpArray[5]);
}

</script>

<form id="frmchoixronde1" name="frmchoixronde1"  method="POST" 
enctype="application/x-www-form-urlencoded" onsubmit='return formValidator()'>
<html><head><title>Page des poolers</title></head> <body>
<b>Entrez vos choix pour la premiere ronde</b>
</br><fieldset>

<b><label for="player_1" style="width:2em">1</label></b><input name="player_1" id="player_1" type="text" size="30"></br>
<b><label for="player_2" style="width:2em">2</label></b><input name="player_2" id="player_2" type="text" size="30"></br>
<b><label for="player_3" style="width:2em">3</label></b><input name="player_3" id="player_3" type="text" size="30"></br>

<b><label for="player_4" style="width:2em">4</label></b><input name="player_4" id="player_4" type="text" size="30"></br>
<b><label for="player_5" style="width:2em">5</label></b><input name="player_5" id="player_5" type="text" size="30"></br>
<b><label for="player_6" style="width:2em">6</label></b><input name="player_6" id="player_6" type="text" size="30"></br>
<input type="submit" name="submit" value="Soumettre vos choix">

</form>
</br> 
</body></html> 

i corrected most of the errors let me know if it works ???

 

function array_count_values( array ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Ates Goral (http://magnetiq.com)
    // + namespaced by: Michael White (http://getsprink.com)
    // +      input by: sankai
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // *     example 1: array_count_values([ 3, 5, 3, "foo", "bar", "foo" ]);
    // *     returns 1: {3:2, 5:1, "foo":2, "bar":1}
    // *     example 2: array_count_values({ p1: 3, p2: 5, p3: 3, p4: "foo", p5: "bar", p6: "foo" });
    // *     returns 2: {3:2, 5:1, "foo":2, "bar":1}
    // *     example 3: array_count_values([ true, 4.2, 42, "fubar" ]);
    // *     returns 3: {42:1, "fubar":1}

    var tmp_arr = {}, key = '', t = '';

    var __getType = function(obj) {
        // Objects are php associative arrays.
        var t = typeof obj;
        t = t.toLowerCase();
        if (t == "object") {
            t = "array";
        }
        return t;
    }

    ;var __countValue = function (values) {
        switch (typeof(values)) {
            case "number":
                if (Math.floor(values) != values) {
                    return;
                }
            case "string":
                if (values in this) {
                    ++this[svalues]; break;
                } else {
                    this[values] = 1; break;
                }
}
    }

    ;t = __getType(array);
    if (t == 'array') {
        for ( key in array ) {
            __countValue.call(tmp_arr, array[key]);
        }
    }
    return tmp_arr;
}


function formValidator(){
   var tmpArray= [];
   tmpArray = array_count_values([ 3, 5, 3, "foo", "bar", "foo" ]);
   document.write(tmpArray[0]  + tmpArray[1]+ tmpArray[2]);
   document.write(tmpArray[3]  + tmpArray[4]+ tmpArray[5]);
//   document.write(tmpArray[0] + " " + tmpArray[1]+ " " + tmpArray[2] + " " + tmpArray[3]+ " " + tmpArray[4]+ " " + tmpArray[5]);
}

Hi,

and thank you for your help. I ran the code you changed but it still doesn't work. I tried to see what change you brought to it and I couldn't see any except that you change value for values. There was a typo where you had replaced value with svalues but I corrected it. Can you tell me what other change you've done if any. Maybe by teaming up we can get it to work.

 

Thanks again,

J-R

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.