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... -
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
-
-
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-
<html xmlns="http://www.w3.org/1999/xhtml">
-
<head>
-
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
-
<title>Project 4 - Magic 8 Ball (Javascript)</title>
-
<link href="project04stylesheet.css" rel="stylesheet" type="text/css" />
-
-
<script type="text/javascript" language="javascript">
-
-
<!--
-
//Declare variables - GLOBAL
-
var oldQuestion = "";
-
var count = "";
-
var newQuestion = "";
-
var testLastChar = "";
-
-
-
//TEST!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
-
window.alert("oldQuestion says" + " " + oldQuestion + " " + "after script header and var oldQuestion declared")//says null value
-
window.alert("newQuestion says" + " " + newQuestion + " " + "after script header and var oldQuestion declared")//doesn't even show
-
-
//Create function "resetAnswer" to reset the "Answer" marquee onKeyPress event of txtQuestion text box
-
function resetAnswer()
-
{
-
document.getElementById('Answer').innerHTML = "Ask the 8 Ball a question...";
-
//var newQuestion = "";
-
-
//If assigned value here, oldQuestion has a null value and newQuestion has the value it's supposed to
-
-
}
-
-
//Create the array "Message" for math object to randomly select from
-
var Message = new Array();
-
-
Message[0] = "Ask again later...";
-
Message[1] = "Yes!";
-
Message[2] = "Noooo...";
-
Message[3] = "It appears to be so!";
-
Message[4] = "The reply is hazy. Please try a new question.";
-
Message[5] = "Definate affirmative!";
-
Message[6] = "What is it you really want to know?";
-
Message[7] = "The outlook is promising...";
-
Message[8] = "My sources say no.";
-
Message[9] = "All of the signs point to yes.";
-
Message[10] = "Don't count on it.";
-
Message[11] = "I cannot predict this now.";
-
Message[12] = "As I see it, yes.";
-
Message[13] = "I better not tell you now...";
-
Message[14] = "Concentrate and ask your question in a different way...";
-
-
-
//If assigned value here, oldQuestion has a null value and newQuestion has the value it's supposed to
-
-
//Create function "answerQuestion" to assign answer to "Answer" marquee, if it meets requirements, when btnAsk is clicked
-
function answerQuestion()
-
{
-
-
//Randomly pick a number between 0 and 14 and assign it to "count"
-
//First line from the PowerPoints...Second and third lines are from the internet...What's "response" and ".floor" mean?
-
var count = Math.round(Math.random() * (13)) + 1;
-
//var count = Math.random()*response.length;
-
//count = Math.floor(count);
-
-
//assign txtQuestion to newQuestion variable
-
var newQuestion = document.form.txtQuestion.value;
-
-
//Get last character of question asked
-
var testLastChar = newQuestion.charAt(newQuestion.length - 1);
-
-
//If assigned value here, oldQuestion is same as newQuestion
-
//assign txtQuestion to oldQuestion variable
-
//var oldQuestion = document.form.txtQuestion.value;
-
-
//If "question" variable has nothing in it, alert user to enter a question.
-
if(newQuestion == ""){
-
window.alert("Please enter a question.");
-
document.form.txtQuestion.focus();
-
-
//TEST!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
-
window.alert("oldQuestion says" + " " + oldQuestion + " " + "in first if statement") //if this runs, "undefined" value, unless old def in stmt
-
window.alert("newQuestion says" + " " + newQuestion + " " + "in first if statement") //if this runs, null value
-
-
-
//If the last character of the "question" variable isn't a "?", alert user to end their question with a "?" sign.
-
}else if(testLastChar != "?"){
-
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!");
-
document.form.txtQuestion.select();
-
-
//TEST!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
-
window.alert("oldQuestion says" + " " + oldQuestion + " " + "in 2nd if statement")
-
window.alert("newQuestion says" + " " + newQuestion + " " + "in 2nd if statement")
-
-
-
//Compare the newQuestion to the oldQuestion; if they match, give an error message.
-
}else if(newQuestion == oldQuestion){
-
window.alert("Please ask a NEW question and quit wasting the 8 Ball's precious time!");
-
document.form.txtQuestion.select();
-
-
//If assigned value here, oldQuestion is undefined and newQuestion has value it's supposed to.
-
-
-
}else{
-
//document.getElementById('idname').whatever is the format for calling an element; here?
-
//yes: document.getElementById('Answer').innerHTML instead of Answer.innerHTML
-
document.getElementById('Answer').innerHTML = (Message[count]);
-
-
-
//If assigned value here, oldQuestion is same as newQuestion
-
}
-
-
-
//TEST!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
-
window.alert("oldQuestion says" + " " + oldQuestion + " " + "at the end of 2nd function")//all of them always say what txtQuestion says by this
-
window.alert("newQuestion says" + " " + newQuestion + " " + "at the end of 2nd function")//point
-
-
//If assigned value here, oldQuestion is undefined and newQuestion has value it's supposed to.
-
-
-
}
-
-
//If assigned value here, oldQuestion has a null value and newQuestion has value it's supposed to.
-
//assign txtQuestion to oldQuestion variable
-
var oldQuestion = newQuestion;
-
-
//TEST!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
-
window.alert("oldQuestion says" + " " + oldQuestion + "after 2nd function")//none of them will play at this point
-
window.alert("newQuestion says" + " " + newQuestion + "after 2nd function")
-
-
//If assigned value here, oldQuestion is undefined and newQuestion has value it's supposed to.
-
-
//-->
-
</script>
-
</head>
-
-
<body onload="document.form.txtQuestion.focus()">
-
-
<h1>Magic 8 Ball</h1>
-
<br />
-
<h3>What would you like to know?</h3>
-
<form id="form" name="form" method="post" action="">
-
<p>
-
<input name="txtQuestion" type="text" class="txtQuestion" id="txtQuestion" accesskey="q" tabindex="1" size="60" onkeypress="resetAnswer()" />
-
</p>
-
<br/ >
-
<p>
-
<input name="btnAsk" type="button" class="btnAsk" id="btnAsk" accesskey="a" tabindex="2" value="Ask the 8 Ball" onClick="answerQuestion()" />
-
</p>
-
<br/ >
-
<br/ >
-
<p>The 8 Ball says: </p>
-
-
<marquee behavior="scroll" id="Answer">Ask the 8 Ball a question...</marquee>
-
-
</form>
-
</body>
-
</html>
-
1 1593
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. -
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
-
-
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-
<html xmlns="http://www.w3.org/1999/xhtml">
-
<head>
-
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
-
<title>Project 4 - Magic 8 Ball (Javascript)</title>
-
<link href="project04stylesheet.css" rel="stylesheet" type="text/css" />
-
-
<script type="text/javascript" language="javascript">
-
-
<!--
-
//Declare variables - GLOBAL
-
var oldQuestion = "";
-
var count = "";
-
var newQuestion = "";
-
var testLastChar = "";
-
-
function resetAnswer() {
-
document.getElementById('Answer').innerHTML = "Ask the 8 Ball a question...";
-
}
-
var Message = new Array();
-
-
Message[0] = "Ask again later...";
-
Message[1] = "Yes!";
-
Message[2] = "Noooo...";
-
Message[3] = "It appears to be so!";
-
Message[4] = "The reply is hazy. Please try a new question.";
-
Message[5] = "Definate affirmative!";
-
Message[6] = "What is it you really want to know?";
-
Message[7] = "The outlook is promising...";
-
Message[8] = "My sources say no.";
-
Message[9] = "All of the signs point to yes.";
-
Message[10] = "Don't count on it.";
-
Message[11] = "I cannot predict this now.";
-
Message[12] = "As I see it, yes.";
-
Message[13] = "I better not tell you now...";
-
Message[14] = "Concentrate and ask your question in a different way...";
-
-
function answerQuestion() {
-
var count = Math.round(Math.random() * (13)) + 1;
-
var newQuestion = document.form.txtQuestion.value;
-
var testLastChar = newQuestion.charAt(newQuestion.length - 1);
-
if (newQuestion == "") {
-
window.alert("Please enter a question.");
-
document.form.txtQuestion.focus();
-
} else if (testLastChar != "?") {
-
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!");
-
document.form.txtQuestion.select();
-
} else if (newQuestion == oldQuestion) {
-
window.alert("Please ask a NEW question and quit wasting the 8 Ball's precious time!");
-
document.form.txtQuestion.select();
-
} else {
-
document.getElementById('Answer').innerHTML = (Message[count]);
-
oldQuestion = newQuestion;
-
}
-
}
-
-
</script>
-
</head>
-
-
<body onload="document.form.txtQuestion.focus()">
-
-
<h1>Magic 8 Ball</h1>
-
<br />
-
<h3>What would you like to know?</h3>
-
<form id="form" name="form" method="post" action="" onsubmit="answerQuestion(); return false;">
-
<p>
-
<input name="txtQuestion" type="text" class="txtQuestion" id="txtQuestion" accesskey="q" tabindex="1" size="60" onkeypress="resetAnswer()" />
-
</p>
-
<br/ >
-
<p>
-
<input name="btnAsk" type="submit" class="btnAsk" id="btnAsk" accesskey="a" tabindex="2" value="Ask the 8 Ball" onClick="answerQuestion()" />
-
</p>
-
<br/ >
-
<br/ >
-
<p>The 8 Ball says: </p>
-
-
<marquee behavior="scroll" id="Answer">Ask the 8 Ball a question...</marquee>
-
-
</form>
-
</body>
-
</html>
-
-
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
| |