Jump to content

associative array?


Recommended Posts

Hello Folks,


I just wondering why JS cannot handle associative arrays like php does. Ok, after a while of investigation I stuck with a little code now. Once I've written a simple q&a spamcheck for my contact form in php, hence, I try to adapt it to a js contact form: It's a beautiful pop-up form that costs me 7 dollars, I was just missing that in this script I bought one week ago comes with NO spamcheck functionality, so I would like to build it on my own, maybe with your help?


I know that there are no associative arrays in js. My basement is this code:

  var questions = {       'The color of the sky?': 'blue',       'Abraham...': 'lincoln',       '10 minus 2, plus 3..': '11',       'Rupert the rednose...': 'reindeer'   };

So, next I just need to count the "keys" or array-entries, object parameters, you name it.

Finally I want to shuffle the indexies and return one single question.


To count the "array" I tried this:

var count = 0; // initializefor (var i in questions) { if (questions.hasOwnProperty(i)) { ++count; } }var min = 0; var max = count;alert(count);

...and it works like a charm! My problem is now the indexies e.g. the KEYS of this Object... I don't know how can I shuffle them, and even put out then. Any suggestions to this?


Maybe something in this direction:

var qsingle = shuffle(min, max);alert(qsingle);

But the output is nothing, the script even breaks... But works fine in php! :) Any help would be greatly appreciated!

Edited by Maiskolben
Link to comment
Share on other sites

Those are properties of an object, and you can't shuffle properties of an object. It would probably be better to have an array of question objects, I don't see a reason to put the question text as a property name either:

var questions = [  {q: 'question 1', a: 'answer 1'},  {q: 'question 2', a: 'answer 2'},  ...];
Now questions.length will give you the number of questions, and there are several ways to shuffle an array that you can find. You would access the questions as questions.q or questions.a.
Link to comment
Share on other sites

JSG, is right in that you don't really want to use an object if you need an array. You can do something like this...

var questions = {'The color of the sky?': 'blue','Abraham...': 'Lincoln','10 minus 2, plus 3..': '11','Rupert the rednose...': 'reindeer'};var akeys = Object.keys(questions);

...where akeys is an array of the keys. You might then randomize an index into this array. Each key in the array can obviously access the value (answer) in the questions object. It is more efficient to randomize an index than to randomize an array.

Link to comment
Share on other sites

  • 2 weeks later...

Hey guys,


thank you for your response! After a while of try & error and your help, I just have my spamcheck running! :)


I've found this and use it to shuffle my array (thank you JSG!):

function shuffle(array) {  var m = array.length, t, i;  while (m) {    // Pick a remaining element…    i = Math.floor(Math.random() * m--);    // And swap it with the current element.    t = array[m];    array[m] = array[i];    array[i] = t;  } return array;}  var spamcheck = [       {f: 'Question A',        a: 'ANSWER A'},       {f: 'Question B',        a: 'ANSWER B'},       {f: 'Question C',        a: 'ANSWER C'}   ];   shuffle(spamcheck);

next I get the anwser in an hidden field of my formular, base64 decoded:

<input class="kissmebaby" name="chk-hidden" id="chk-hidden" type="hidden" value="'+Base64.encode(spamcheck[1].a)+'" />

And finally I check, if the typed answer matches the right answer, all lowercase:

var rightanswer = document.getElementById("chk-hidden").value;var x = document.getElementById("answerfield").value;var givenanswer = Base64.encode(x.toLowerCase()); // note, BASE64.encode (references to a function)if (givenanswer !== rightanswer) {  document.getElementById('answerfield').style.background = "#f9ef9e"; // tell the user there is something wrong...   } else {   //go to the serverside php-script viá ajax}

and this is it. Thanks for putting me on the right way! :)


PS: this works for me like a charm, may it be there is something "too much" in this shuffle-function, but I'm glad it works now and wouldn't change anything, the performance(s) of the entire script(s) on my website is ok..


PPS: @davej: thank you too, I gladly abandoned the idea to solve this viá the indexies! :)

Edited by Maiskolben
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...