Jump to content
Sign in to follow this  
brooke_theperson

How to use a prompt to enter any object from an array

Recommended Posts

Alright, so I am making a program that when a button that says "yo" for example, will display a prompt when click. If the answer to the prompt is a verb, then the "yo" form of that verb is displayed. So far I have a button, that when clicked has a prompt. When I enter a word in the button I can't make it display the "yo" form of the verb. Please help:

 

Here is my javascript:

function verb(verb, yo) {    this.verb = verb;    this.yo = yo;}//*All verbsvar estar = new verb("estar", "estoy");//*All arraysvar verbs =  [estar];//*Verb translation present_yofunction presentYo(){    var verb = verbs;    var answer = prompt("Enter a verb: ").toLowerCase();    if (answer == verb){     document.getElementById("translation").innerHTML = "verbs.yo";    }    else{    document.getElementById("translation").innerHTML = "I do not know the translation.";    }    }

When I enter a word, all that shows up is "I do not know the translation." Please help. Thanks.

Edited by brooke_theperson

Share this post


Link to post
Share on other sites

Most of your programs seem to begin with a constructor. I think that is probably a bad idea in most cases. Here is what I think of when I consider your description...

<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"/><title>title</title><style>div {width:300px;border:1px solid #eee;</style><script>window.onerror = function(a, b, c, d){alert('Javascript Error:n'+a+'nURL: '+b+'nLine: '+c+'  Column: '+d);return true;}</script><script>'use strict';var vlist = { estar:'estoy', edog:'etoydog', ecat:'ecattoy' };function presentYo(){  var answer = prompt("Enter a verb: ").toLowerCase();  if (vlist[answer] != undefined){      document.getElementById("translation").innerHTML = answer + ' implies ' + vlist[answer];  }else{      document.getElementById("translation").innerHTML = "I do not know the translation.";  }}window.onload = function(){ document.getElementById('btn1').onclick = presentYo;}</script></head><body><h1>Translation</h1><input type="button" id="btn1" value="Enter"/><div id="translation"></div></body></html>

Share this post


Link to post
Share on other sites
<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"/><title>title</title><style>div {width:300px;border:1px solid #eee;</style><script>window.onerror = function(a, b, c, d){alert('Javascript Error:n'+a+'nURL: '+b+'nLine: '+c+'  Column: '+d);return true;}</script><script>'use strict';function Verb(verb, yo) {    this.verb = verb;    this.yo = yo;}//*All verbsvar etar = new Verb("etar", "etoy");var ecar = new Verb("ecar", "ecoy");var ebar = new Verb("ebar", "eboy");var verbs = [etar,ecar,ebar];function presentYo(){var answer = prompt("Enter a verb: ").trim().toLowerCase();var found = false;var i,j;for(i=0 ; i<verbs.length && !found ; i++){ if (verbs[i].verb === answer){  found = true;  j = i; }}  if (found === true){      document.getElementById("translation").innerHTML = answer + ' implies ' + verbs[j].yo;  }else{      document.getElementById("translation").innerHTML = "I do not know the translation.";  }}window.onload = function(){ document.getElementById('btn1').onclick = presentYo;}</script></head><body><h1>Translation</h1><input type="button" id="btn1" value="Enter"/><div id="translation"></div></body></html>

...but again something without the constructor seems simpler...

<script>'use strict';//*All verbsvar verbs = {};verbs.etar = "etoy";verbs.ecar = "ecoy";verbs.ebar = "eboy";function presentYo(){ var answer = prompt("Enter a verb: ").trim().toLowerCase(); var yo; for(var e in verbs){  if (e === answer){   yo = verbs[e];  } } if (yo != undefined){      document.getElementById("translation").innerHTML = answer +' implies '+ yo; }else{      document.getElementById("translation").innerHTML = "I do not know the translation."; }}window.onload = function(){ document.getElementById('btn1').onclick = presentYo;}</script>
  • Like 1

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...