denismm778 Posted December 8, 2012 Share Posted December 8, 2012 Hey guys, I've created a script that generate a random word of many words. For example I click the mix button and I receive the word: "book" ------------ I would like to create another button that, when I click it to show me the translation of the word (which is added in the script manually) When I click mix again, to remove the old translation "book" and the english word "book", and to randomly appear another word, for ex. "green", when I click the second button to show again a box with translation and so on... --------- I've created only the english word mixer, and I don't know how to create the translation. I'll be very happy and pleased if you can help me with making this, cuz will help me a lot. <script language="Javascript"> <!-- // Use the following variable to specify the number of random words var NumberOfWords = 28 var words = new BuildArray(NumberOfWords) // Use the following variables to define your random words: words[1] = "czarevitch" words[2] = "brightwork" words[3] = "verkrampte" words[4] = "protectrix" words[5] = "nudibranch" words[6] = "grandchild" words[7] = "newfangled" words[8] = "flugelhorn" words[9] = "mythologer" words[10] = "pluperfect" words[11] = "jellygraph" words[12] = "quickthorn" words[13] = "rottweiler" words[14] = "technician" words[15] = "cowpuncher" words[16] = "middlebrow" words[17] = "jackhammer" words[18] = "triphthong" words[19] = "wunderkind" words[20] = "dazzlement" words[21] = "jabberwock" words[22] = "witchcraft" words[23] = "pawnbroker" words[24] = "thumbprint" words[25] = "motorcycle" words[26] = "cryptogram" words[27] = "torchlight" words[28] = "bankruptcy" function BuildArray(size){ this.length = size for (var i = 1; i <= size; i++){ this[i] = null} return this } function PickRandomWord(frm) { // Generate a random number between 1 and NumberOfWords var rnd = Math.ceil(Math.random() * NumberOfWords) // Display the word inside the text box frm.WordBox.value = words[rnd] } //--> </script> <form name="WordForm" align="center"> <input type="TEXT" size="40" name="WordBox"><br><br> <input type="BUTTON" onclick="PickRandomWord(document.WordForm)" value="Разбъркай!"> </form> Kind Regards, D.Saidov Link to comment https://forums.phpfreaks.com/topic/271758-help-with-random-words-code/ Share on other sites More sharing options...
codefossa Posted December 8, 2012 Share Posted December 8, 2012 I'm gonna play Santa, just because I was bored. Demo Page: http://xaotique.no-ip.org/tmp/26/ HTML <table> <tr> <td class="link">New Word</td> <td class="desc"></td> </tr> <tr> <td class="link">Translation</td> <td class="desc"></td> </tr> </table> CSS table { text-align: center; } .link { color: #000077; } .link:hover { cursor: pointer; } .desc { color: #252525; text-align: left; } Javascript // Allow the page to load first. window.addEventListener("load", function() { // Function for generating random numbers. function random(from, to) { return Math.floor(Math.random() * (to - from + 1)) + from; } // Array of words and their translation. var words = { 'king': 'kong', 'ele': 'phant', 'drag': 'on', 'php': 'freaks' }; // Array of our link class. var links = window.document.querySelectorAll(".link"); // Cycle through the array. for (var i in links) { // Set an onclick function. links[i].addEventListener("click", function() { // New word or translation? if (this.innerHTML == "New Word") { // Clear and set new word. window.document.querySelectorAll(".desc")[1].innerHTML = ""; window.document.querySelectorAll(".desc")[0].innerHTML = Object.keys(words)[random(0, Object.keys(words).length - 1)]; } else { // Show translation. window.document.querySelectorAll(".desc")[1].innerHTML = words[window.document.querySelectorAll(".desc")[0].innerHTML]; } }, false); } }, false); Link to comment https://forums.phpfreaks.com/topic/271758-help-with-random-words-code/#findComment-1398278 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.