Jump to content

[SOLVED] Why isn't this array_count_values script not working? array help


simpli

Recommended Posts

Hi,

I downladed a js script that is supposed to replicate the array_count_values php function. It's my first time using arrays in php and I am stuck. I don't seem to be getting the data in the array. Obviously the array_count_values doesnt work but I think it's my implementation of arrays that doesnt work. I tried running the script I have no error in my browser so I don't know where to look now. Can anyone troubleshoot me?

 

Thanks

 

 
<script type='text/javascript'>

function formValidator(){
// Make quick references to our fields

var p1 = document.frmchoixronde1.player_1;
var p2 = document.frmchoixronde1.player_2;
var p3 = document.frmchoixronde1.player_3;
var p4 = document.frmchoixronde1.player_4;
var p5 = document.frmchoixronde1.player_5;
var p6 = document.frmchoixronde1.player_6;

var arrayPlayers = new Array();

arrayPlayers[0] = p1;
arrayPlayers[1] = p2;
arrayPlayers[2] = p3;
arrayPlayers[3] = p4;
arrayPlayers[4] = p5;
arrayPlayers[5] = p6;
alert(arrayPlayers[0]);	


// Validation of the text fields!
if(PasDeDoublons(arrayPlayers)){
return true;	
}

return false;

}



function PasDeDoublons( array)
{
var tmpArray= new Array();
var i = 0;

tmpArray = array_count_values(array);


for (i=0; i<=tmpArray.length;i++) 
{
    	if(tmpArray[i] >= 1)
    	{
    		alert("Le joueur ne peut etre choisi deux fois");
    		return false;
    	}
}
return true;
}

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;
}
</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 don't understand what you mean. What variable am I using as a function. The switch may be screwed up. I didnt get to troubleshoot it. But it's not getting there ok so I need to solve this first. Can you elaborate on your first statement?

 

Thanks,

J-R

Here's the javascript I copied at jslint

 
function PasDeDoublons( array)
{
var tmpArray= new Array();
var i = 0;

tmpArray = array_count_values(array);


for (i=0; i<=tmpArray.length;i++) 
{
    	if(tmpArray[i] >= 1)
    	{
    		alert("Le joueur ne peut etre choisi deux fois");
    		return false;
    	}
}
return true;
}

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(){
// Make quick references to our fields

var p1 = document.frmchoixronde1.player_1;
var p2 = document.frmchoixronde1.player_2;
var p3 = document.frmchoixronde1.player_3;
var p4 = document.frmchoixronde1.player_4;
var p5 = document.frmchoixronde1.player_5;
var p6 = document.frmchoixronde1.player_6;

var arrayPlayers = new Array();

arrayPlayers[0] = p1;
arrayPlayers[1] = p2;
arrayPlayers[2] = p3;
arrayPlayers[3] = p4;
arrayPlayers[4] = p5;
arrayPlayers[5] = p6;
alert(arrayPlayers[0]);	


// Validation of the text fields!
if(PasDeDoublons(arrayPlayers)){
return true;	
}

return false;

}

 

And here is the first error message I get:

Problem at line 3 character 23: Use the array literal notation [].

Well that's chinese to me. Even after I googled it I don't see what's wrong with the way I declare my array. That's how it's declared on most tutorial sites. so I'm at a loss.

J-R

I don't think the switch is the problem because my alert that is placed much before it doesn't return anything good. I think it's the assignment of the text inputs that is not working for some reason. To verify this I removed the whole function that is bothering you. I'm concentrating on retrieving the information from the textboxes. But it's not working. I can troubleshoot the switch if there's a problem in it but since I have a more pressing issue, I'd like to solve it first. Anyway, thanks for your effort.

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.