Hello,
This is a first post. I will do my best to descriptive enough.
I am a Javascript newb. An assignment I’ve been given involves making a page that works like a Magic 8 Ball (you know like, ask a yes or no question and it gives you some lame answer like “give it time” or “of course”.
First of all, had to use arrays and the math random to generate a random response. Also, have to check if the last character in the question is a question mark (?). However, the problem comes when I need to see whether or not the same question is being asked again. Meaning, I need to compare the previous input (or question) to the current input. If they match up, it is supposed to kick back with an error saying something like “Please ask a different question”.
One more thing before I post my code. The class I am taking is VERY basic and (as far as I can tell) is only teaching the concepts behind Javascript and the ways it is similar to other programming languages such as VB. That being said, they are not teaching (what i have recently found) to be proper code, so they don’t use the whole document.xxxxx, which I believe is called the DOM.. am I correct with that. Anyway, I studied forums for quite a while to teach myself DOM just to get my script working in Firefox and not just IE.
So if my code looks stupid or sloppy… that’s why. And please point out these errors to me!! I need to learrrn!
Here we go, this is the if, else if, else in the function that is called when the “Ask..” button is pressed. So my problem is when it comes to “else if (quest == prevquest) I’ve tried many different placements of declaring the variables as well (used PHP as suggested in the How to for Thread posting):
[CODE]var quest = document.getElementById(‘txtquestion’).value;
var checkquest = quest.charAt(quest.length – 1);
if (checkquest != “?”)
{
window.alert (“Please end your question with a ?”);
document.getElementById(‘txtquestion’).value = “”;
document.getElementById(‘txtquestion’).select();
return false;
}
else if (quest == prevquest){
window.alert (“Did you really think you could get away with asking the same question again? Try again.”);
document.getElementById(‘txtquestion’).value = “”;
document.getElementById(‘txtquestion’).select();
return false;
}
else{
var Select = Math.round(Math.random() * (14)) + 0;
document.getElementById(‘theAnswer’).innerHTML = “”;
document.getElementById(‘theAnswer’).innerHTML = (Ans[Select]);
document.getElementById(‘txtquestion’).select();
var prevquest = document.getElementById(‘txtquestion’).value;
}
Other than it not kicking back with that alert when needed, it seems to work fine. Here is the link to where I have this posted [URL=”http://deptcis2.fvtc.edu/200011995/Web%20Development%20II/project04/Project04.html”]Magic 8 Ball
Thank you so much for any help you can offer me!
-Keith