5 Replies - 2137 Views - Last Post: 29 November 2011 - 12:24 PM

#1 DiamondEyes  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 28-March 11

String Comparison - Preventing Duplicate User Input w/ One Textbox

Posted 28 November 2011 - 02:08 PM

Hello Javascript programmers, and thanks in advance for your help.
I'm fairly new to Javascript and I need some assistance with my code.

I am creating a Magic 8 Ball.
These are my requirements
  • User input is accepted through the textbox.
  • The question must end with a question mark.
  • A question may not be asked twice in a row.


My problem lies within my last requirement.
I have tried creating an array that will hold the user's input in memory , and then compare it against the string variable.However, since I only need to check for the LAST question, I don't think an array is necessary.
Then I tried using the substring method with the string variable.

If anyone could give me a push in the right direction I would greatly appreciate it!

This is my HTML code.
<!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" xml:lang="en" lang="en">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
		<title>Magic 8 Ball</title>
		<script type="text/javascript" language="Javascript" src="code.js"></script>
		<link href="style.css" type="text/css" rel="stylesheet"/>
	</head>
	<body onload="Focus()">
		<div id="wrapper">
			<div id="header">
				<h2>Magic 8 Ball</h2>
				<h4>What would you like to know?</h4>
			<!--end #header-->
			</div>
			<div id="main">
				<img id="ball" src="ball.png" alt="Magic 8 Ball"/>
				<p id="lblMarquee">Ask the 8 ball a question...</p>
				<input type="text" id="txtQuestion"/>
				<button id="btnAsk" onclick="getQuote();">Ask the 8 Ball</button>
				<p>The 8 Ball says:</p>
			<!--end #main-->
			</div>
		<!--end #wrapper-->
		</div>
	</body>
</html>



This is my Javascript code.
	//Global variables
	var Question = "";
	var LastChar = "";
	var oldQuestion = "";
	var intNumber = "";
	
	function Focus(){
		document.getElementById("txtQuestion").focus();
	}
	//This is a page level variable. It is at a different scope than the rest of the code.
	var LastQuote = -1;

	//Declare array
	var arrQuotes = new Array()
	arrQuotes[0] = "Ask again later..."
	arrQuotes[1] =	"Yes"
	arrQuotes[2] =	"No"
	arrQuotes[3] =	"It appears to be so"
	arrQuotes[4] =	"Reply is hazy, please try again"
	arrQuotes[5] =	"What do you really want to know?"
	arrQuotes[6] =	"Outlook is good"
	arrQuotes[7] =	"My sources say no"
	arrQuotes[8] =	"Signs point to yes"
	arrQuotes[9] =	"Don't count on it"
	arrQuotes[10] =	"Cannot predict now"
	arrQuotes[11] =	"As I see it, yes"
	arrQuotes[12] =	"Don't quit your day job"
	arrQuotes[13] =	"Better not tell you now"
	arrQuotes[14] =	"Concentrate and ask again"
	arrQuotes[15] =	"Fate is on your side"

	//Generate reply from array in random order 
	function getQuote(){
		var Question = new String(document.getElementById("txtQuestion").value);
		var LastChar = new String("?");	
		var oldQuestion = new String(document.getElementById("txtQuestion").value);
		

		//Declare variable for random array
		var intNumber = Math.random() * 16;
		intNumber = Math.round(intNumber);

		//Check for empty form
		if(Question==0 || Question=="")
		{
  			alert("You need to ask a question in order to receive an answer...");
  			document.getElementById("txtQuestion").focus();
  			return; 
  		//Check for question mark at end of question			
		}else if(Question.charAt(Question.length-1) != LastChar){
			alert("I predict you will place a question mark at the end of your question.");
			document.getElementById("txtQuestion").focus();
			return;
		//Check for same question
		}else if(Question.value == oldQuestion.substring(0,-1)){
			alert("You may not ask the 8 Ball the same question twice in a row!");
			document.getElementById("txtQuestion").select();
			return;
		}else{		
			document.getElementById("txtQuestion").focus();
			document.getElementById("txtQuestion").select();
		}	
		if(LastQuote == intNumber)
		{
			return;
		}
		LastQuote = intNumber;
		document.getElementById("lblMarquee").innerHTML = arrQuotes[intNumber];
	}



Is This A Good Question/Topic? 0
  • +

Replies To: String Comparison - Preventing Duplicate User Input w/ One Textbox

#2 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 948
  • View blog
  • Posts: 2,357
  • Joined: 15-February 11

Re: String Comparison - Preventing Duplicate User Input w/ One Textbox

Posted 28 November 2011 - 06:37 PM

In your Javascript line #55 substring(0, -1) will return an empty string. I also don't see how Question and oldQuestion will be different since you assigned the exact same value to both of them at the beginning of the getQuote function.
Was This Post Helpful? 0
  • +
  • -

#3 DiamondEyes  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 28-March 11

Re: String Comparison - Preventing Duplicate User Input w/ One Textbox

Posted 28 November 2011 - 09:27 PM

Thank you codprada.

I am trying to make a multi-dimensional array, but I am not succeeding.


new Javascript code
	//Global variables
	var LastChar = "";
	var intNumber = "";
	
	function Focus(){
		document.getElementById("txtQuestion").focus();
	}
	//This is a page level variable. It is at a different scope than the rest of the code.
	var LastQuote = -1;

	//Declare array
	var arrQuotes = new Array()
	arrQuotes[0] = "Ask again later..."
	arrQuotes[1] =	"Yes"
	arrQuotes[2] =	"No"
	arrQuotes[3] =	"It appears to be so"
	arrQuotes[4] =	"Reply is hazy, please try again"
	arrQuotes[5] =	"What do you really want to know?"
	arrQuotes[6] =	"Outlook is good"
	arrQuotes[7] =	"My sources say no"
	arrQuotes[8] =	"Signs point to yes"
	arrQuotes[9] =	"Don't count on it"
	arrQuotes[10] =	"Cannot predict now"
	arrQuotes[11] =	"As I see it, yes"
	arrQuotes[12] =	"Don't quit your day job"
	arrQuotes[13] =	"Better not tell you now"
	arrQuotes[14] =	"Concentrate and ask again"
	arrQuotes[15] =	"Fate is on your side"

	//Generate reply from array in random order 
	function getQuote(){
		var Question = new Array();
		Question = new String(document.getElementById("txtQuestion").value);
		Question[0] = ["",""];
		var LastChar = new String("?");	
		
		//Declare variable for random array
		var intNumber = Math.random() * 16;
		intNumber = Math.round(intNumber);

		//Check for empty form
		if(Question==0 || Question=="")
		{
  			alert("You need to ask a question in order to receive an answer...");
  			document.getElementById("txtQuestion").focus();
  			return; 
  		//Check for question mark at end of question			
		}else if(Question.charAt(Question.length-1) != LastChar){
			alert("I predict you will pla	ce a question mark at the end of your question.");
			document.getElementById("txtQuestion").focus();
			return;
		//Check for same question
		}else if(Question[0][0].substring(0) == Question.substring[0][1].substring(0)){
			alert("You may not ask the 8 Ball the same question twice in a row!");
			document.getElementById("txtQuestion").select();
			return;
		}else{		
			document.getElementById("txtQuestion").focus();
			document.getElementById("txtQuestion").select();
		}	
		if(LastQuote == intNumber)
		{
			return;
		}
		LastQuote = intNumber;
		document.getElementById("lblMarquee").innerHTML = arrQuotes[intNumber];
	}

Was This Post Helpful? 0
  • +
  • -

#4 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 948
  • View blog
  • Posts: 2,357
  • Joined: 15-February 11

Re: String Comparison - Preventing Duplicate User Input w/ One Textbox

Posted 29 November 2011 - 05:07 AM

A multidimensional array goes like this
var multi = [[], [], [], []]



The assignment on line 33 will overwrite the previous assignment and since it's a string line 34 will fail.

Remember when getQuote finishes executing the variables created in there will be destroyed. If you want to hold the last question asked you must create a variable outside of getQuote to store this value. A global variable would do the job but as you get into Javascript you'll run into Closures.

Example
var old_question = null;
function getQuote(){
	// ...
	
	if(Question == old_question){
		//same question
	}

        // ...
        old_question = Question;
}



Was This Post Helpful? 0
  • +
  • -

#5 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5901
  • View blog
  • Posts: 12,805
  • Joined: 16-October 07

Re: String Comparison - Preventing Duplicate User Input w/ One Textbox

Posted 29 November 2011 - 08:15 AM

What's with the arrays, and the substrings, and...

Right, I'd do something like this:
function getAnswer() {
	//Declare array
	var arrQuotes = new Array()
	arrQuotes[0] = "Ask again later..."
	arrQuotes[1] =	"Yes"
	arrQuotes[2] =	"No"
	arrQuotes[3] =	"It appears to be so"
	arrQuotes[4] =	"Reply is hazy, please try again"
	arrQuotes[5] =	"What do you really want to know?"
	arrQuotes[6] =	"Outlook is good"
	arrQuotes[7] =	"My sources say no"
	arrQuotes[8] =	"Signs point to yes"
	arrQuotes[9] =	"Don't count on it"
	arrQuotes[10] =	"Cannot predict now"
	arrQuotes[11] =	"As I see it, yes"
	arrQuotes[12] =	"Don't quit your day job"
	arrQuotes[13] =	"Better not tell you now"
	arrQuotes[14] =	"Concentrate and ask again"
	arrQuotes[15] =	"Fate is on your side"
	//Declare variable for random array
	var intNumber = Math.round(Math.random() * arrQuotes.length);
	return arrQuotes[intNumber];
}

var LastQuestion = null;

function getQuote(){
	// you declare an array for some unknown reason
	// var Question = new Array();
	// then overwrite it
	// Question = new String(document.getElementById("txtQuestion").value);
	// wtf?
	// Question[0] = ["",""];
	// var LastChar = new String("?");	
	var ele = document.getElementById("txtQuestion");
	var Question = ele.value;

	
	if(Question==null || Question.length==0) { //Check for empty form
		alert("You need to ask a question in order to receive an answer...");
		ele.focus();
		// return; 
	
	} else if(Question.charAt(Question.length-1) != "?"){ //Check for question mark at end of question			
		alert("I predict you will pla	ce a question mark at the end of your question.");
		ele.focus();
	
	} else if(LastQuestion != null && LastQuestion == Question){ //Check for same question
		alert("You may not ask the 8 Ball the same question twice in a row!");
		ele.select();
		
	} else { // made it
		ele.focus();
		ele.select();
		LastQuestion = Question; // that's it, that's all you need
		document.getElementById("lblMarquee").innerHTML = getAnswer();
	}
}



Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#6 DiamondEyes  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 28-March 11

Re: String Comparison - Preventing Duplicate User Input w/ One Textbox

Posted 29 November 2011 - 12:24 PM

Thank you both for your help - it works!

Baavgi- your code was helpful and I am happy to have amused you with my novitiate script writing ability. ;)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1