473,387 Members | 1,585 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Working with global variables

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
1 1593
iam_clint
1,208 Expert 1GB
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

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

Similar topics

9
by: Larry Woods | last post by:
I have a site that works fine for days, then suddenly, I start getting ASP 0115 errors with an indication that session variables IN SEPARATE SESSIONS have disappeared! First, for background...
10
by: Matt | last post by:
Greetings, What are people's thoughts on global variables in C++? Why are we taught not to use them in programming? Is it true that if you are running two copies of the C program one copy can...
12
by: David WOO | last post by:
Hi, I am a newbie on C++, I need to define some global variables which should be accessible to most classes. In the mean time, I don't won't the global variables be modified freely at most of...
2
by: Bryan Parkoff | last post by:
….I would like to know which is the best optimization to use global variable or global struct. I always tell C/C++ Compiler to turn on optimization. ….I use underscore between first name and...
2
by: Patient Guy | last post by:
I have a library of functions representing a filesystem interface (essentially a file selection interface, to be used in opening/reading/writing/closing files). Heavily scripted HTML document...
33
by: MLH | last post by:
I've read some posts indicating that having tons of GV's in an Access app is a bad idea. Personally, I love GVs and I use them (possibly abuse them) all the time for everything imaginable - have...
8
by: tshad | last post by:
I have an Application_Error function in my Global.asax function that works fine until I try to access my Session variables. I am emailing the results to myself whenever I get an error and would...
9
by: CDMAPoster | last post by:
About a year ago there was a thread about the use of global variables in A97: http://groups.google.com/group/comp.databases.ms-access/browse_frm/thread/fedc837a5aeb6157 Best Practices by Kang...
5
by: Sandman | last post by:
I dont think I understand them. I've read the section on scope in the manual inside out. I'm running PHP 5.2.0 Here is the code I'm working on: //include_me.php <?php $MYVAR = array(); global...
112
by: istillshine | last post by:
When I control if I print messages, I usually use a global variable "int silent". When I set "-silent" flag in my command line parameters, I set silent = 1 in my main.c. I have many functions...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.