HTML function not working?

megan1989

Baseband Member
Messages
29
Location
USA
I have created a function to take someone through a short series of questions, but when I try to push the button on the webpage it does nothing. I have gone through it a couple of times and have been unable to see where I have gone wrong. Here is the code for the function:

function womenTrivia();
{
var count = 0;
Meg();

function Meg();
{
var questionMeg = window.prompt("Who does Meg marry?");
var sentMeg = 'John Brooke';

window.prompt("Who does Meg marry?");

if(questionMeg != sentMeg)
{
questionMeg = window.prompt("Who does Meg marry?");
}
else
{
count = count + 1;
window.alert("You are Correct!");
}
if(count == 1)
{
Jo();
}
}
function Jo();
{
var questionJo = window.prompt("Where does Jo travel to pursue a literary career?");
var sentJo = 'New York City';

window.prompt("What does Jo travel to pursue a literary career?");

if(questionJo != sentJo)
{
questionJo = window.prompt("Where does Jo travel to pursue a literay career?");
}
else
{
count = count + 1;
window.alert("You are Correct!");
}
if(count == 2)
{
Beth();
}
}
function Beth();
{
var questionBeth = window.prompt("What causes Beth's health to deteriorate?");
var sentBeth = 'Scarlet Fever';

window.prompt("What causes Beth's health to deteriorate?");

if(questionBeth != sentBeth)
{
questionBeth = window.prompt("What causes Beth's health to deteriorate?");
}
else
{
count = count + 1;
window.alert("You are Correct!");
}
if(count == 3)
{
Amy();
}
}
function Amy();
{
var questionAmy = window.prompt("Is Amy the youngest sister, True or False?");
var sentAmy = 'True';

window.prompt("Is Amy the youngest sister, True or False?");

if(questionAmy != sentAmy)
{
questionAmy = window.prompt("Is Amy the youngest sister, True or False?");
}
else
{
count = count + 1;
window.alert("You are Correct!");
}
if(count == 4);
{
window.alert("Congratulations, you have finished the Trivia!");
}
}
}


And this is the code for the button to be pressed on the screen:

<p><input type="button" onclick="womenTrivia();" value="Little Women Trivia" class="auto-style4" style="width: 172px; height: 25px;"></p>
 
I haven't run this to check yet, but two mistakes I can see straight off:

- You seem to have all your functions *inside* the womenTrivia() function - they should all be separate. i.e. close the womenTrivia function (with a curly bracket) before the Meg() function starts (and delete the curly bracket at the end of the code to make it line up.)

- You shouldn't put a semi-colon after function headers, so when you have:

function Meg();
{
...etc

It should be:

function Meg()
{
...etc.
 
Thank you Berry, I thought that there may have been a way to be able to call all of those functions within a master function, so to speak. Apparently not, anyway I made the corrections that you suggested and it is working correctly now. Thank you
 
Try dis.

Code:
<script>
function womenTrivia() { 
var score = 0;
var possible = 4;

score += prompt("Who does Meg marry?") == "John Brooke" ? 1 : 0;
score += prompt("Where does Jo travel to pursue a literary career?") == "New York City" ? 1 : 0;
score += prompt("What causes Beth's health to deteriorate?") == "Scarlet Fever" ? 1 : 0;
score += prompt("Amy is the youngest sister. True or False?") == "True" ? 1 : 0;

alert("Congratulations! You have finished. \n\nYou scored " + score + " points \nOut of a possible " + possible);
} 
</script>

<button onclick="womenTrivia()">Little Women Trivia</button>
 
Last edited:
The code tag is painful to look at. Perhaps a quote tag in the future?

Quote will wrap the text, making it more painful to look at for a developer. Code is so you can copy/paste/maintain formatting standards... for, you know... code.

;)
 
Last edited:
Back
Top Bottom