Jump to content

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


brooke_theperson
 Share

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
Link to comment
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>
Link to comment
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
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
 Share

×
×
  • Create New...