Jump to content

Maiskolben

Members
  • Posts

    2
  • Joined

  • Last visited

Profile Information

  • Location
    Berlin

Maiskolben's Achievements

Newbie

Newbie (1/7)

0

Reputation

  1. 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!
  2. 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!
×
×
  • Create New...