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>
-