By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
462,205 Members | 544 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 462,205 IT Pros & Developers. It's quick & easy.

Working with global variables

P: 1
I have been working on this code for 5 days... I have read multiple tutorials and looked at various code. This is my third forum try... :) My instructor refuses to help me. I think it's because I'm a good student. They assume you know it all. :) Well, I don't and I'm getting a little frustrated and wonder if you guys can shed some light? It's an 8 ball. It works. I've tested and tested but can't find where to put the oldQuestion variable so that it will nab the txtQuestion text box value AFTER the first run, but before the value changes. This is so I can compare the two variables and make sure the question is not being asked twice in a row.

I am new so I may have another type of error I don't know about... I have worked and worked over this code, tho... Posted within is the results of all my oldQuestion/newQuestion variable tests.

Please be kind! All simple, newbie-grade suggestions and/or comments welcome. :)

Thanks in advance...
Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
  2.  
  3. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4. <html xmlns="http://www.w3.org/1999/xhtml">
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  7. <title>Project 4 - Magic 8 Ball (Javascript)</title>
  8. <link href="project04stylesheet.css" rel="stylesheet" type="text/css" />
  9.  
  10. <script type="text/javascript" language="javascript">
  11.  
  12. <!--
  13. //Declare variables - GLOBAL
  14. var oldQuestion = "";
  15. var count = "";
  16. var newQuestion = "";
  17. var testLastChar = "";
  18.  
  19.  
  20. //TEST!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  21. window.alert("oldQuestion says" + " " + oldQuestion + " " + "after script header and var oldQuestion declared")//says null value
  22. window.alert("newQuestion says" + " " + newQuestion + " " + "after script header and var oldQuestion declared")//doesn't even show
  23.  
  24. //Create function "resetAnswer" to reset the "Answer" marquee onKeyPress event of txtQuestion text box
  25. function resetAnswer()
  26. {
  27. document.getElementById('Answer').innerHTML = "Ask the 8 Ball a question...";
  28. //var newQuestion = "";
  29.  
  30. //If assigned value here, oldQuestion has a null value and newQuestion has the value it's supposed to
  31.  
  32. }
  33.  
  34. //Create the array "Message" for math object to randomly select from
  35. var Message = new Array();
  36.  
  37. Message[0] = "Ask again later...";
  38. Message[1] = "Yes!";
  39. Message[2] = "Noooo...";
  40. Message[3] = "It appears to be so!";
  41. Message[4] = "The reply is hazy.  Please try a new question.";
  42. Message[5] = "Definate affirmative!";
  43. Message[6] = "What is it you really want to know?";
  44. Message[7] = "The outlook is promising...";
  45. Message[8] = "My sources say no.";
  46. Message[9] = "All of the signs point to yes.";
  47. Message[10] = "Don't count on it.";
  48. Message[11] = "I cannot predict this now.";
  49. Message[12] = "As I see it, yes.";
  50. Message[13] = "I better not tell you now...";
  51. Message[14] = "Concentrate and ask your question in a different way...";
  52.  
  53.  
  54. //If assigned value here, oldQuestion has a null value and newQuestion has the value it's supposed to
  55.  
  56. //Create function "answerQuestion" to assign answer to "Answer" marquee, if it meets requirements, when btnAsk is clicked
  57. function answerQuestion()
  58. {
  59.  
  60. //Randomly pick a number between 0 and 14 and assign it to "count"
  61. //First line from the PowerPoints...Second and third lines are from the internet...What's "response" and ".floor" mean?
  62. var count = Math.round(Math.random() * (13)) + 1;
  63. //var count = Math.random()*response.length;
  64. //count = Math.floor(count);
  65.  
  66. //assign txtQuestion to newQuestion variable
  67. var newQuestion = document.form.txtQuestion.value;
  68.  
  69. //Get last character of question asked
  70. var testLastChar = newQuestion.charAt(newQuestion.length - 1);
  71.  
  72. //If assigned value here, oldQuestion is same as newQuestion
  73. //assign txtQuestion to oldQuestion variable
  74. //var oldQuestion = document.form.txtQuestion.value;
  75.  
  76. //If "question" variable has nothing in it, alert user to enter a question.
  77. if(newQuestion == ""){
  78. window.alert("Please enter a question.");
  79. document.form.txtQuestion.focus();
  80.  
  81. //TEST!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  82. window.alert("oldQuestion says" + " " + oldQuestion + " " + "in first if statement") //if this runs, "undefined" value, unless old def in stmt
  83. window.alert("newQuestion says" + " " + newQuestion + " " + "in first if statement") //if this runs, null value
  84.  
  85.  
  86. //If the last character of the "question" variable isn't a "?", alert user to end their question with a "?" sign.
  87. }else if(testLastChar != "?"){
  88. window.alert("All questions MUST end with a '?'.  Everyone knows this!  Quit wasting the 8 Ball's precious time and enter your question correctly, please!");
  89. document.form.txtQuestion.select();
  90.  
  91. //TEST!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  92. window.alert("oldQuestion says" + " " + oldQuestion + " " + "in 2nd if statement")
  93. window.alert("newQuestion says" + " " + newQuestion + " " + "in 2nd if statement")
  94.  
  95.  
  96. //Compare the newQuestion to the oldQuestion; if they match, give an error message.
  97. }else if(newQuestion == oldQuestion){
  98. window.alert("Please ask a NEW question and quit wasting the 8 Ball's precious time!");
  99. document.form.txtQuestion.select();
  100.  
  101. //If assigned value here, oldQuestion is undefined and newQuestion has value it's supposed to.
  102.  
  103.  
  104. }else{
  105. //document.getElementById('idname').whatever is the format for calling an element; here?
  106. //yes: document.getElementById('Answer').innerHTML instead of Answer.innerHTML
  107. document.getElementById('Answer').innerHTML = (Message[count]);
  108.  
  109.  
  110. //If assigned value here, oldQuestion is same as newQuestion
  111. }
  112.  
  113.  
  114. //TEST!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  115. window.alert("oldQuestion says" + " " + oldQuestion + " " + "at the end of 2nd function")//all of them always say what txtQuestion says by this 
  116. window.alert("newQuestion says" + " " + newQuestion + " " + "at the end of 2nd function")//point
  117.  
  118. //If assigned value here, oldQuestion is undefined and newQuestion has value it's supposed to.
  119.  
  120.  
  121. }
  122.  
  123. //If assigned value here, oldQuestion has a null value and newQuestion has value it's supposed to.
  124. //assign txtQuestion to oldQuestion variable
  125. var oldQuestion = newQuestion;
  126.  
  127. //TEST!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  128. window.alert("oldQuestion says" + " " + oldQuestion + "after 2nd function")//none of them will play at this point
  129. window.alert("newQuestion says" + " " + newQuestion + "after 2nd function")
  130.  
  131. //If assigned value here, oldQuestion is undefined and newQuestion has value it's supposed to.
  132.  
  133. //-->
  134. </script>
  135. </head>
  136.  
  137. <body onload="document.form.txtQuestion.focus()">
  138.  
  139. <h1>Magic 8 Ball</h1>
  140. <br />
  141. <h3>What would you like to know?</h3>
  142. <form id="form" name="form" method="post" action="">
  143.   <p>
  144.     <input name="txtQuestion" type="text" class="txtQuestion" id="txtQuestion" accesskey="q" tabindex="1" size="60" onkeypress="resetAnswer()" />
  145.   </p>
  146.   <br/ >
  147.   <p>
  148.     <input name="btnAsk" type="button" class="btnAsk" id="btnAsk" accesskey="a" tabindex="2" value="Ask the 8 Ball" onClick="answerQuestion()" />
  149. </p>
  150. <br/ >
  151. <br/ >
  152. <p>The 8 Ball says:  </p>
  153.  
  154. <marquee behavior="scroll" id="Answer">Ask the 8 Ball a question...</marquee>
  155.  
  156. </form>
  157. </body>
  158. </html>
  159.  
Sep 25 '07 #1
Share this Question
Share on Google+
1 Reply

iam_clint
Expert 100+
P: 1,208
You would place oldquestion = newquestion when the current question is answered. example provided. By the way never post with the thread title as Help me, most people won't even bother looking at your question without a good title.
Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
  2.  
  3. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4. <html xmlns="http://www.w3.org/1999/xhtml">
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  7. <title>Project 4 - Magic 8 Ball (Javascript)</title>
  8. <link href="project04stylesheet.css" rel="stylesheet" type="text/css" />
  9.  
  10. <script type="text/javascript" language="javascript">
  11.  
  12. <!--
  13. //Declare variables - GLOBAL
  14. var oldQuestion = "";
  15. var count = "";
  16. var newQuestion = "";
  17. var testLastChar = "";
  18.  
  19. function resetAnswer() {
  20.     document.getElementById('Answer').innerHTML = "Ask the 8 Ball a question...";
  21. }
  22. var Message = new Array();
  23.  
  24. Message[0] = "Ask again later...";
  25. Message[1] = "Yes!";
  26. Message[2] = "Noooo...";
  27. Message[3] = "It appears to be so!";
  28. Message[4] = "The reply is hazy. Please try a new question.";
  29. Message[5] = "Definate affirmative!";
  30. Message[6] = "What is it you really want to know?";
  31. Message[7] = "The outlook is promising...";
  32. Message[8] = "My sources say no.";
  33. Message[9] = "All of the signs point to yes.";
  34. Message[10] = "Don't count on it.";
  35. Message[11] = "I cannot predict this now.";
  36. Message[12] = "As I see it, yes.";
  37. Message[13] = "I better not tell you now...";
  38. Message[14] = "Concentrate and ask your question in a different way...";
  39.  
  40. function answerQuestion() {
  41.     var count = Math.round(Math.random() * (13)) + 1;
  42.     var newQuestion = document.form.txtQuestion.value;
  43.     var testLastChar = newQuestion.charAt(newQuestion.length - 1);
  44.     if (newQuestion == "") {
  45.         window.alert("Please enter a question.");
  46.         document.form.txtQuestion.focus();
  47.     } else if (testLastChar != "?") {
  48.         window.alert("All questions MUST end with a '?'. Everyone knows this! Quit wasting the 8 Ball's precious time and enter your question correctly, please!");
  49.         document.form.txtQuestion.select();
  50.     } else if (newQuestion == oldQuestion) {
  51.         window.alert("Please ask a NEW question and quit wasting the 8 Ball's precious time!");
  52.         document.form.txtQuestion.select();
  53.     } else {
  54.         document.getElementById('Answer').innerHTML = (Message[count]);
  55.         oldQuestion = newQuestion;
  56.     }
  57. }
  58.  
  59. </script>
  60. </head>
  61.  
  62. <body onload="document.form.txtQuestion.focus()">
  63.  
  64. <h1>Magic 8 Ball</h1>
  65. <br />
  66. <h3>What would you like to know?</h3>
  67. <form id="form" name="form" method="post" action="" onsubmit="answerQuestion(); return false;">
  68. <p>
  69. <input name="txtQuestion" type="text" class="txtQuestion" id="txtQuestion" accesskey="q" tabindex="1" size="60" onkeypress="resetAnswer()" />
  70. </p>
  71. <br/ >
  72. <p>
  73. <input name="btnAsk" type="submit" class="btnAsk" id="btnAsk" accesskey="a" tabindex="2" value="Ask the 8 Ball" onClick="answerQuestion()" />
  74. </p>
  75. <br/ >
  76. <br/ >
  77. <p>The 8 Ball says: </p>
  78.  
  79. <marquee behavior="scroll" id="Answer">Ask the 8 Ball a question...</marquee>
  80.  
  81. </form>
  82. </body>
  83. </html>
  84.  
  85.  
Sep 25 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.