Javascript object oriented pirate translator -


i need make object oriented pirate translator in javascript final, have idea , have code can't output correctly on page. have set type english phrase in 1 box , in other box spits out pirate version. not step through array of objects. post code. can get!!!

 var $ = function(id) { return document.getelementbyid(id); }   var dictionary = [{     greetings: {         hello:"ahoy",         hi:"yohoho",         pardon_me:"avast",         excuse_me:"arrr"     },     people: {         stranger:"scurvy",         sir:"matey",         madam:"proud beauty",         miss:"comely wench",         officer:"foul blaggart",         enemy:"scallywag"     },     questions: {         where_is:"whar be",         can_you_help_me_find:"know ye",         is_that:"be that",         how_far_is_it_to:"how many leagues to"     },     articles: {         the:"th",         a:"a briny",         any:"some godforsaken",         nearby:"broadside",         my:"me",         your:"yer"     },     adjectives: {         old:"barnacle-covered",         attractive:"comely",         happy:"grog-filled"     },     places: {         restroom:"head",         restaurant:"galley",         hotel:"fleabag inn",         mall:"market",         pub:"skull & scuppers",         bank:"buried trasure"     },     desires: {         i_would_like_to:"i needin to",         i_desire:"i have fierce fire in me belly",         i_wish_i_knew_how_to:"i hankerin to",         my_mother_told_me_to:"me dear ol mum, bless black soul, tol me to",         my_companion_would_like_to:"me mate, ol rumpot, wants to"     },     actions: {         find:"come across",         take_a_nap:"have bit of lie down",         make_a_withdrawal:"seize yer doubloons",         have_a_cocktail:"swill pint or 2 of grog",         kill_him:"blow man down",         frown:"hang jib",         take_a_hike:"walk plank"     },  }];  function translate(text)     // returns: copy of text english phrases replaced piratey equivalents      {         (var = 0; < dictionary.length; i++) {             var toreplace = new regexp("\\b"+dictionary[i][0]+"\\b", "i");              var index = text.search(toreplace);             while (index != -1) {                text = text.replace(toreplace, dictionary[x][y]);                index = text.search(toreplace);             }         }          text = text.replace(/\./g, function() {             return math.random() < 0.5 ? ". arrrrrrrrr" : "."         });         return text.charat(0).touppercase() + text.substring(1);     }      var clear_click = function() {     $("output1").value = "";     $("output2").value = ""; }  window.onload = function() {     $("clear").onclick = clear_click; }    /*for (var x in dictionary) {     (var y in dictionary[x])         console.log (y, dictionary[x][y]); }*/ 

html:

    <!doctype html>     <html>      <head>       <meta charset="utf-8">       <title> pirate translator </title>       <script src="js/test2.js"></script>       <link rel="stylesheet" href="css/normalize.css"/>       <link rel="stylesheet" href="css/styles.css"/>     </head>      <body>         <h1>jacob's pirate translator</h1>         <p>simply click on buttons translate<br />            words and/or phrases english pirate talk.         <hr />     <form name="talkform">          <table>         <tr><td align="center"><b>english</b>             <td>             <td align="center"><b>pirate</b>         <tr><td><textarea name="english" id="output1" rows=12 cols=35 wrap="virtual"></textarea> </td>             <td align="center"> <br />                 <input type="button" value="translate --->"                         onclick="document.talkform.pirate.value =                                   translate(document.talkform.english.value);"> </td>             <td><textarea name="pirate" id="output2" rows=12 cols=35 wrap="virtual"></textarea> </td>             <input type="button" id="clear" value="clear">         </tr>         </table>       </form>       </body>     </html> 

edit2: i've modified dictionary. if follow format of dictionary can go deep of arrays want. should work. i've tested examples in different kinds of orders , text not found in dictionary. feel free try out.

and add challenge, made recursive functions. :d no for/while loops.

demo

var dictionary = {     hello: "ahoy",     hi: "yohoho",     pardon: [1, {         me: "avast"     }],     excuse: [1, {         me: "arrr"     }],     stranger: "scurvy",     sir: "matey",     madam: "proud beauty",     miss: "comely wench",     officer: "foul blaggart",     enemy: "scallywag",     where: [1, {         is: "whar be"     }],     can: [4, {         you_help_me_find: "know ye"     }],     is: [1, {         that: "be that"     }],     how: [4, {         far_is_it_to: "how many leagues to"     }],     the: "th",     a: "a briny",     any: "some godforsaken",     nearby: "broadside",     my: "me",     your: "yer",     old: "barnacle-covered",     attractive: "comely",     happy: "grog-filled",     restroom: "head",     restaurant: "galley",     hotel: "fleabag inn",     mall: "market",     pub: "skull & scuppers",     bank: "buried trasure",     would: [1, {         like: "be needin"     }],     i: [         [1, {             desire: "i have fierce fire in me belly"         }],         [5, {             wish_i_knew_how_to: "i hankerin to"         }]     ],     my: [         [4, {             mother_told_me_to: "me dear ol mum, bless black soul, tol me to"         }],         [4, {             companion_would_like_to: "me mate, ol rumpot, wants to"         }]     ],     find: "come across",     take: [2, {         a_nap: "have bit of lie down"     }],     make: [2, {         a_withdrawal: "seize yer doubloons"     }],     have: [2, {         a_cocktail: "swill pint or 2 of grog"     }],     kill: [1, {         him: "blow man down"     }],     frown: "hang jib",     take: [2, {         a_hike: "walk plank"     }]  };  function translate(text) {     var hop = 1;     var texttoreturn = "";     //checking if text split, if not split     if (typeof text === 'string') {         text = text.split(' ');     }     if (text.length > 0) {         if (typeof dictionary[text[0]] == 'undefined' || typeof dictionary[text[0]] === 'string') {             texttoreturn = (dictionary[text[0]] || text[0]);             text = text.slice(hop, text.length);         } else {            var info = recursivecheck(text, dictionary[text[0]]);             texttoreturn =  (info.hop == 1) ? text[0] : info.text;             text = text.splice(info.hop, text.length);         }         if(text.length > 0)         {              texttoreturn += ' ' + translate(text);           }      }     return texttoreturn; } function recursivecheck(text, arr) {     var found = {hop:1, text: ''};     if(arr.length > 0)     {         if(typeof parseint(arr[0]) === 'number' && text.length-1 >= arr[0])         {             var phrase = text.slice(1, arr[0]+1);             if(arr[1][phrase.join('_')])             {                  found.hop = arr[0]+1;                  found.text = arr[1][phrase.join('_')];             }         }         else         {              found = recursivecheck(text, arr[0] || []);                if(found.hop == 1 && arr.length > 1)              {                   found = recursivecheck(text, arr.slice(1, arr.length));              }         }     }     return found; } var tra = document.getelementbyid('translate'); var pir = document.getelementbyid('pirate'); pir.disabled = true; var eng = document.getelementbyid('english');  eng.onkeyup = function(){      pir.value = "";    } tra.onclick = function () {     pir.value = translate(eng.value); }; 

here example of deep array if want go further dictionary:

...         i: [             [1, {                 desire: [                           [1,{ a: "i have fierce fire in me belly"}],                           [1,{ one: "i have 1 fierce fire in me belly"}]              }],             [5, {                 wish_i_knew_how_to: "i hankerin to"             }]         ], ... 

of course haven't tried yet, can if need work. luck.

edit: point of code show how access list. don't seem using categories in code, why have them?

your list looks bit complex simple translation. last checked, dictionaries don't have categories.. joke aside i've simplified list.

var dictionary = {         hello:"ahoy",         hi:"yohoho",         pardon_me:"avast",         excuse_me:"arrr",         stranger:"scurvy",         sir:"matey",         madam:"proud beauty",         miss:"comely wench",         officer:"foul blaggart",         enemy:"scallywag",         where_is:"whar be",         can_you_help_me_find:"know ye",         is_that:"be that",         how_far_is_it_to:"how many leagues to",         the:"th",         a:"a briny",         any:"some godforsaken",         nearby:"broadside",         my:"me",         your:"yer",         old:"barnacle-covered",         attractive:"comely",         happy:"grog-filled",         restroom:"head",         restaurant:"galley",         hotel:"fleabag inn",         mall:"market",         pub:"skull & scuppers",         bank:"buried trasure",         i_would_like_to:"i needin to",         i_desire:"i have fierce fire in me belly",         i_wish_i_knew_how_to:"i hankerin to",         my_mother_told_me_to:"me dear ol mum, bless black soul, tol me to",         my_companion_would_like_to:"me mate, ol rumpot, wants to",         find:"come across",         take_a_nap:"have bit of lie down",         make_a_withdrawal:"seize yer doubloons",         have_a_cocktail:"swill pint or 2 of grog",         kill_him:"blow man down",         frown:"hang jib",         take_a_hike:"walk plank"  };  function translate(text) {     pir.value = dictionary[text.split(' ').join('_')] || 'not found';  }  var tra = document.getelementbyid('translate'); var pir = document.getelementbyid('pirate'); pir.disabled = true; var eng = document.getelementbyid('english');  tra.onclick = function(){ translate(eng.value) }; 

html:

<input id="english" type="text" placeholder="english"/> <input id="pirate" placeholder="pirate"/> <button id="translate">translate</button> 

i've simplified code (by lot) simple working model.

working jsfiddle: http://jsfiddle.net/grimbode/f296h/2/


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -