# Thread: [RESOLVED] Newbie, Need help with counters.!

1. ## [RESOLVED] Newbie, Need help with counters.!

I am having a hard time getting the code to count the number of correct answers. It counts the total but not the correct. Any input would be greatly appreciated.

Code:
```<html>
<!-- ========================================================================================== -->
<!-- This page will select the states at random, ask the user to guess each state capital, and  -->
<!-- then compare the user's guesses against the correct answers. The user is also allowed to   -->
<!-- specify the number of questions on the quiz.                                               -->

<title> The State Capital Quiz </title>
<style type="text/css">
.style1
{
margin-top: 19px;
}
</style>

<h1 style="text-align:center; font-family: FELTPOINT;" > How Well Do You Know Your State Capitals? </h1>

<script type="text/javascript" src="random.js"></script>

<script type="text/javascript">
function stateGuess()						                         // THIS IS THE FUNCTION IN WHICH THE PROGRAM DEPENDS ON
// Assumes: That the user will specify how many questions are on the quiz.
// Returns: The amount of questions the user specified and random states are generated, via a parallel array.

{

/****** Declared Variables ******/

var numQuestion, userAnswer, stateIndex, Count = 0, correctAnswer = 0, numGuess = 0;

/****** End of Declaration ******/

/* This page will select a state for each question by generating a random index from 0 to 49. The random
index is then assigned to a variable. Once the user enters a guess, this will be checked against the state
capital stored in the index. */

STATES = 		["Alabama",	    "Alaska",	    "Arizona",	    "Arkansas",
"Florida",	    "Georgia",	    "Hawaii",	    "Idaho",
"Illinois",	    "Indiana",	    "Iowa",	    "Kansas",
"Kentucky",	    "Louisiana",    "Maine",	    "Maryland",
"Massachusetts",   "Michigan",	    "Minnesota",    "Mississippi",
"New Hampshire",   "New Jersey",   "New Mexico",   "New York",
"North Carolina",  "North Dakota", "Ohio",	    "Oklahoma",
"Oregon",  	    "Pennsylvania", "Rhode Island", "South Carolina",
"South Dakota",    "Tennessee",    "Texas",	    "Utah",
"Vermont",	    "Virginia",	    "Washington",   "West Virginia",
"Wisconsin",	    "Wyoming"];

CAPITALS =           ["Montgomery",	"Juneau",	"Phoenix",	"Little Rock",
"Sacramento",	"Denver",	"Hartford",	"Dover",
"Tallahassee",	"Atlanta",	"Honolulu",	"Boise",
"Springfield",	"Indianapolis",	"Des Moines",	"Topeka",
"Frankfort",		"Baton Rouge",	"Augusta",	"Annapolis",
"Boston",		"Lansing",	"Saint Paul",	"Jackson",
"Jefferson City",	"Helena",	"Lincoln",	"Carson City",
"Concord",		"Trenton",	"Santa Fe",	"Albany",
"Raleigh",		"Bismarck",	"Columbus",	"Oklahoma City",
"Salem",		"Harrisburg",	"Providence",	"Columbia",
"Pierre",		"Nashville",	"Austin",	"Salt Lake City",
"Montpelier",	"Richmond",	"Olympia",	"Charleston",

numQuestion = document.getElementById('numQuestionBox').value;			// ASSIGNED TO VARIBLE
numQuestion = parseFloat(numQuestion);			                	// PARSEFLOAT THE VARIABLE CONTAINING A NUMBER

for (Count=0; Count < numQuestion; Count=Count+1)				// THIS IS THE LOOP THAT DETERMINES HOW MANY QUESTIONS IN THE QUIZ
{
stateIndex = RandomInt(0, 49);

userAnswer = prompt("What is the capital of " + STATES[stateIndex] + " ?");	// THIS IS THE PROMPT FOR THE QUESTION

}

// COMPARES USER'S ANSWER TO THE RANDOM GENERATED STATE

if (userAnswer == CAPITALS[stateIndex])	{					/* THIS IS THE STATEMENT (IF) THAT COMPARES THE USERS ANSWERS WITH
THE ANSWERS IN THE INDEX. */

}
else {

alert("The correct answer is " + CAPITALS[stateIndex] + "."); 	 	/* IF THE USER IS WRONG, BASED ON COMPARING WITH A ELSE
STATEMENT, THEY GET AN ALERT. */
}

document.getElementById('CountBox').value = Count;

numGuess = correctAnswer / Count * (100);

numGuess = parseFloat(numGuess);
document.getElementById('numGuessBox').value = numGuess;
}

</script>

<body style="background-position: left top; text-align: center; background-image: url('Images/state map.jpg'); background-repeat: no-repeat;">

<h3 align="center" style="font-size: medium; font-family: FELTPOINT; font-weight: lighter;">
Welcome to the State Capital Quiz. Below you will find a box to specify the number of question you would like to be quizzed on. </h3>

<h3 align="center" style="font-size: medium; font-family: FELTPOINT; font-weight: lighter;">
Once you input the number of questions you would like, click on the button that says "Run Quiz". </h3>

<h3 align="center" class="style1" style="font-size: medium; font-family: FELTPOINT; font-weight: bold;">
Please note that you will be entering your answers in a "prompt" box, then hitting the enter key to submit them.</h3>

<br /><br />

<h3 align="center" style="font-size: medium; font-family: FELTPOINT; font-weight: lighter;">
Please specify the number of questions you would like.
<input type="text" id="numQuestionBox" size="4" value="0" />

<br /><br />

<input type="button" value="Run Quiz" onclick="stateGuess();" />  				<!-- THIS IS THE BUTTON THAT CALLES THE FUNCTION, OR STARTS IT. -->
<br /><br />
You have correctly answered the questions <input type="text" id="correctAnswerBox" size="10" value="0" />
out of <input type="text" id="CountBox" size="10" value="0" /> times.
<br /><br />
That means you were correct <input type="text" id="numGuessBox" size="10" value="0" />% of the time.

<!-- THE END OF THE STATES QUIZ. -->
</body>
</html>```

2. Can not evaluate code without <script type="text/javascript" src="random.js"></script>

3. ## Here is the random script.

Code:
```// File: random.js
// Author: Dave Reed
//
// This file contains several routines for generating random values.
// Source code is available at http://dave-reed.com/book
//////////////////////////////////////////////////////////////////////

function RandomNum(low, high)
// Given : low <= high
// Returns : a random number in the range [low, high)
{
return Math.random()*(high-low) + low;
}

function RandomInt(low, high)
// Given : low <= high
// Returns : a random integer in the range [low, high]
{
return Math.floor(Math.random()*(high-low+1)) + low;
}

function RandomChar(str)
// Given : str is a nonempty string
// Returns: a random character from the string
{
return str.charAt(RandomInt(0, str.length-1));
}

function RandomOneOf(list)
// Given : list is a nonempty list (array)
// Returns: a random item from the list
{
return list[RandomInt(0, list.length-1)];
}```

4. ## Note the changes from the original post ...

You had some syntax errors and some 'too soon' loops.
The one that took me longest was a missing */ after a /* comment.

I left a lot of your original code in place with leading "//" to show the difference
Remove the extra // code lines after you see my changes.

HTML Code:
```<html>
<!-- ========================================================================================== -->
<!-- This page will select the states at random, ask the user to guess each state capital, and  -->
<!-- then compare the user's guesses against the correct answers. The user is also allowed to   -->
<!-- specify the number of questions on the quiz.                                               -->

<title> The State Capital Quiz </title>
<style type="text/css">
.style1 {
margin-top: 19px;
}
</style>

<!-- script type="text/javascript" src="random.js"></script -->
<script type="text/javascript">
// File: random.js
// Author: Dave Reed
//
// This file contains several routines for generating random values.
// Source code is available at http://dave-reed.com/book
//////////////////////////////////////////////////////////////////////

function RandomNum(low, high)
// Given : low <= high
// Returns : a random number in the range [low, high)
{
return Math.random()*(high-low) + low;
}

function RandomInt(low, high)
// Given : low <= high
// Returns : a random integer in the range [low, high]
{
return Math.floor(Math.random()*(high-low+1)) + low;
}

function RandomChar(str)
// Given : str is a nonempty string
// Returns: a random character from the string
{
return str.charAt(RandomInt(0, str.length-1));
}

function RandomOneOf(list)
// Given : list is a nonempty list (array)
// Returns: a random item from the list
{
return list[RandomInt(0, list.length-1)];
}
</script>

<script type="text/javascript">
function stateGuess()				                         // THIS IS THE FUNCTION IN WHICH THE PROGRAM DEPENDS ON
// Assumes: That the user will specify how many questions are on the quiz.
// Returns: The amount of questions the user specified and random states are generated, via a parallel array.

{

/****** Declared Variables ******/

var numQuestion, userAnswer, stateIndex, Count = 0, correctAnswer = 0, numGuess = 0;

/****** End of Declaration ******/

/* This page will select a state for each question by generating a random index from 0 to 49. The random
index is then assigned to a variable. Once the user enters a guess, this will be checked against the state
capital stored in the index. */

STATES = 	["Alabama",	    "Alaska",	    "Arizona",	    "Arkansas",
"Florida",	    "Georgia",	    "Hawaii",	    "Idaho",
"Illinois",	    "Indiana",	    "Iowa",	    "Kansas",
"Kentucky",	    "Louisiana",    "Maine",	    "Maryland",
"Massachusetts",   "Michigan",	    "Minnesota",    "Mississippi",
"New Hampshire",   "New Jersey",   "New Mexico",   "New York",
"North Carolina",  "North Dakota", "Ohio",	    "Oklahoma",
"Oregon",  	    "Pennsylvania", "Rhode Island", "South Carolina",
"South Dakota",    "Tennessee",    "Texas",	    "Utah",
"Vermont",	    "Virginia",	    "Washington",   "West Virginia",
"Wisconsin",	    "Wyoming"];

CAPITALS =  ["Montgomery",	"Juneau",	"Phoenix",	"Little Rock",
"Sacramento",	"Denver",	"Hartford",	"Dover",
"Tallahassee",	"Atlanta",	"Honolulu",	"Boise",
"Springfield",	"Indianapolis",	"Des Moines",	"Topeka",
"Frankfort",		"Baton Rouge",	"Augusta",	"Annapolis",
"Boston",		"Lansing",	"Saint Paul",	"Jackson",
"Jefferson City",	"Helena",	"Lincoln",	"Carson City",
"Concord",		"Trenton",	"Santa Fe",	"Albany",
"Raleigh",		"Bismarck",	"Columbus",	"Oklahoma City",
"Salem",		"Harrisburg",	"Providence",	"Columbia",
"Pierre",		"Nashville",	"Austin",	"Salt Lake City",
"Montpelier",	"Richmond",	"Olympia",	"Charleston",

numQuestion = document.getElementById('numQuestionBox').value;			// ASSIGNED TO VARIBLE
numQuestion = parseFloat(numQuestion);			                	// PARSEFLOAT THE VARIABLE CONTAINING A NUMBER

for (Count=0; Count<numQuestion; Count++)  // THIS IS THE LOOP THAT DETERMINES HOW MANY QUESTIONS IN THE QUIZ
{
stateIndex = RandomInt(0, 49);

userAnswer = prompt("What is the capital of " + STATES[stateIndex] + " ?");	// THIS IS THE PROMPT FOR THE QUESTION
//		}

// COMPARES USER'S ANSWER TO THE RANDOM GENERATED STATE

//		if (userAnswer == CAPITALS[stateIndex])	{	/* THIS IS THE STATEMENT (IF) THAT COMPARES THE USERS ANSWERS WITH
//		    								 		   THE ANSWERS IN THE INDEX. */
if (user_Answers[Count] == CAPITALS[stateIndex]) { /* THIS IS THE STATEMENT (IF) THAT COMPARES THE USERS ANSWERS WITH */
} else {
/* IF THE USER IS WRONG, BASED ON COMPARING WITH A ELSE STATEMENT, THEY GET AN ALERT. */
}
}
document.getElementById('CountBox').value = Count;

numGuess = correctAnswer / Count * (100);

numGuess = parseFloat(numGuess);
document.getElementById('numGuessBox').value = numGuess;
}
</script>

<body style="background-position:left;top;text-align:center;background-image:url('Images/state map.jpg');background-repeat: no-repeat;">

<h1 style="text-align:center; font-family: FELTPOINT;" > How Well Do You Know Your State Capitals? </h1>

<h3 align="center" style="font-size: medium; font-family: FELTPOINT; font-weight: lighter;">
Welcome to the State Capital Quiz.
Below you will find a box to specify the number of question you would like to be quizzed on. </h3>

<h3 align="center" style="font-size: medium; font-family: FELTPOINT; font-weight: lighter;">
Once you input the number of questions you would like, click on the button that says "Run Quiz". </h3>

<h3 align="center" class="style1" style="font-size: medium; font-family: FELTPOINT; font-weight: bold;">
Please note that you will be entering your answers in a "prompt" box, then hitting the enter key to submit them.</h3>

<br /><br />

<h3 align="center" style="font-size: medium; font-family: FELTPOINT; font-weight: lighter;">
Please specify the number of questions you would like.
<input type="text" id="numQuestionBox" size="4" value="0" />

<br /><br />

<input type="button" value="Run Quiz" onclick="stateGuess();" />
<!-- THIS IS THE BUTTON THAT CALLES THE FUNCTION, OR STARTS IT. -->
<br /><br />
You have correctly answered the questions <input type="text" id="correctAnswerBox" size="10" value="0" />
out of <input type="text" id="CountBox" size="10" value="0" /> times.
<br /><br />
That means you were correct <input type="text" id="numGuessBox" size="10" value="0" />% of the time.

<!-- THE END OF THE STATES QUIZ. -->
</body>
</html>```
While it might not be the way I would approach the code
at least it now appears to work.

Let me know if the changes cause you confusion.

Good Luck!

5. ## Awesome!

Thank you , I see where I went wrong! Man was I stuck.

Thanks

6. You're most welcome.
Glad I was able to help
Good Luck!