Hi, this is my first time posting here. I am working on a javascript assignment for a class and I'm having a lot of trouble. I have to make a javascript application that simulates the Let's Make A Deal game show.
I keep getting "function is not defined" error for my Prize and GamePartOne functions. Actually all of these functions are getting this problem. It must be some sort of logic error or really stupid syntax error on my part. Please help me fix this.
Code:
<html>
<head>
<title>Orgath, assignment 2, csxxxxxx, October 24, 2009</title>
<script type="text/javascript" src="random.js"></script>
<script type="text/javascript">
function Prize()
//Assumes: 1 of 3 doors is selected.
//Results: This determines which prize is behind which door.
{
var game;
game = RandomInt(1, 3);
if (game == 1)
{
GamePartOne(pick, 1, 2, 2);
GamePartTwo(final1, 1, 2, 2);
}
else if (game == 2)
{
GamePartOne(pick, 2, 1, 2);
GamePartTwo(final1, 2, 1, 2);
}
else if (game == 3)
{
GamePartOne(pick, 2, 2, 1);
GamePartTwo(final1, 2, 2, 1);
}
else {
alert("error");
}
}
function GamePartOne(pick, door1, door2, door3)
//Assumes: variables door1, door2, and door3 have all been assigned 1 or 2 and pick has been assigned 1, 2 or 3.
//Returns: One door will be revealed and the user will be instructed to pick another another door.
{
var number;
if (1 == pick && 1 == door1)
{
number = RandomInt(1, 2);
if (1 == number)
{
ImgReveal(2, pick);
}
else
{
ImgReveal(3, pick);
}
}
else if (1 == pick && 1 != door1)
{
if (1 == door2)
{
ImgReveal(3, pick);
}
else
{
ImgReveal(2, pick);
}
}
else if (2 == pick && 1 == door2)
{
number = RandomInt(1, 2);
if (1 == number)
{
ImgReveal(1, pick);
}
else
{
ImgReveal(3, pick);
}
}
else if (2 == pick && 1 != door2)
{
if(1 == door1)
{
ImgReveal(3, pick);
}
else
{
ImgReveal(1, pick);
}
}
else if (3 == pick && 1 == door3)
{
number = RandomInt(1, 2);
if (1 == number)
{
ImgReveal(2, pick);
}
else
{
ImgReveal(1, pick);
}
}
else if (3 == pick && 1 != door3)
{
if(1 == door1)
{
ImgReveal(2, pick);
}
else
{
ImgReveal(1, pick);
}
}
else
{
alert("error");
}
}
function ImgReveal(reveal, pick)
//Assumes: The first door has been picked.
//Returns: One door is revealed, a new messege appears, and the onclick attributes change.
{
var dr1a, dr2a, dr3a;
dr1a = document.getElementById('dr1');
dr2a = document.getElementById('dr2');
dr3a = document.getElementById('dr3');
if (1 == reveal)
{
document.getElementById('dr1').src = "http://etc.usf.edu/clipart/4700/4778/donkey_13_md.gif";
dr1a.onclick = "null";
dr2a.onclick = "GamePartTwo(2, door1, door2, door3); Counter(pick, 2, winlose)";
dr3a.onclick = "GamePartTwo(3, door1, door2, door3); Counter(pick, 3, winlose)";
document.getElementById('textBox').value = "You have picked door " + pick + ". Switch or stay by clicking on the doors.";
}
else if (2 == reveal)
{
document.getElementById('dr2').src = "http://etc.usf.edu/clipart/4700/4778/donkey_13_md.gif";
dr1a.onclick = "GamePartTwo(1, door1, door2, door3); Counter(pick, 1, winlose)";
dr2a.onclick = "null";
dr3a.onclick = "GamePartTwo(3, door1, door2, door3); Counter(pick, 3, winlose)";
document.getElementById('textBox').value = "You have picked door " + pick + ". Switch or stay by clicking on the doors.";
}
else
{
document.getElementById('dr3').src = "http://etc.usf.edu/clipart/4700/4778/donkey_13_md.gif";
dr1a.onclick = "GamePartTwo(1, door1, door2, door3); Counter(pick, 1, winlose)";
dr2a.onclick = "GamePartTwo(2, door1, door2, door3); Counter(pick, 2, winlose)";
dr3a.onclick = "null";
document.getElementById('textBox').value = "You have picked door " + pick + ". Switch or stay by clicking on the doors.";
}
}
function GamePartTwo(final1, door1, door2, door3)
//Assumes: One door has been revealed and the user has chosen to switch or stay.
//Returns: Notifies the user if they have won or lost and calls the Counter() function.
{
var dr1a, dr2a, dr3a;
dr1a = document.getElementById('dr1');
dr2a = document.getElementById('dr2');
dr3a = document.getElementById('dr3');
if (1 == final1)
{
if (1 == door1)
{
document.getElementById('textBox').value="You win! Click on a door to play again.";
Counter(pick, final1, 1);
document.getElementById('dr1').src = "http://www.clipartheaven.com/clipart/money/dollar.gif";
document.getElementById('dr2').src = "http://etc.usf.edu/clipart/4700/4778/donkey_13_md.gif";
document.getElementById('dr3').src = "http://etc.usf.edu/clipart/4700/4778/donkey_13_md.gif";
}
else
{
document.getElementById('textBox').value="You lose! Click on a door to play again.";
Counter(pick, final1, 2);
if (1 == door2)
{
document.getElementById('dr1').src = "http://etc.usf.edu/clipart/4700/4778/donkey_13_md.gif";
document.getElementById('dr2').src = "http://www.clipartheaven.com/clipart/money/dollar.gif";
document.getElementById('dr3').src = "http://etc.usf.edu/clipart/4700/4778/donkey_13_md.gif";
}
else
{
document.getElementById('dr1').src = "http://etc.usf.edu/clipart/4700/4778/donkey_13_md.gif";
document.getElementById('dr2').src = "http://etc.usf.edu/clipart/4700/4778/donkey_13_md.gif";
document.getElementById('dr3').src = "http://www.clipartheaven.com/clipart/money/dollar.gif";
}
}
}
else if (2 == final1)
{
if (1 == door2)
{
document.getElementById('textBox').value="You win! Click on a door to play again.";
Counter(pick, final1, 1);
document.getElementById('dr1').src = "http://etc.usf.edu/clipart/4700/4778/donkey_13_md.gif";
document.getElementById('dr2').src = "http://www.clipartheaven.com/clipart/money/dollar.gif";
document.getElementById('dr3').src = "http://etc.usf.edu/clipart/4700/4778/donkey_13_md.gif";
}
else
{
document.getElementById('textBox').value="You lose! Click on a door to play again.";
Counter(pick, final1, 2);
if (1 == door1)
{
document.getElementById('dr1').src = "http://www.clipartheaven.com/clipart/money/dollar.gif";
document.getElementById('dr2').src = "http://etc.usf.edu/clipart/4700/4778/donkey_13_md.gif";
document.getElementById('dr3').src = "http://etc.usf.edu/clipart/4700/4778/donkey_13_md.gif";
}
else
{
document.getElementById('dr1').src = "http://etc.usf.edu/clipart/4700/4778/donkey_13_md.gif";
document.getElementById('dr2').src = "http://etc.usf.edu/clipart/4700/4778/donkey_13_md.gif";
document.getElementById('dr3').src = "http://www.clipartheaven.com/clipart/money/dollar.gif";
}
}
}
else if (3 == final1)
{
if (1 == door3)
{
document.getElementById('textBox').value="You win! Click on a door to play again.";
Counter(pick, final1, 1);
document.getElementById('dr1').src = "http://etc.usf.edu/clipart/4700/4778/donkey_13_md.gif";
document.getElementById('dr2').src = "http://etc.usf.edu/clipart/4700/4778/donkey_13_md.gif";
document.getElementById('dr3').src = "http://www.clipartheaven.com/clipart/money/dollar.gif";
}
else
{
document.getElementById('textBox').value="You lose! Click on a door to play again.";
Counter(pick, final1, 2);
if (1 == door2)
{
document.getElementById('dr1').src = "http://etc.usf.edu/clipart/4700/4778/donkey_13_md.gif";
document.getElementById('dr2').src = "http://www.clipartheaven.com/clipart/money/dollar.gif";
document.getElementById('dr3').src = "http://etc.usf.edu/clipart/4700/4778/donkey_13_md.gif";
}
else
{
document.getElementById('dr1').src = "http://www.clipartheaven.com/clipart/money/dollar.gif";
document.getElementById('dr2').src = "http://etc.usf.edu/clipart/4700/4778/donkey_13_md.gif";
document.getElementById('dr3').src = "http://etc.usf.edu/clipart/4700/4778/donkey_13_md.gif";
}
}
}
else
{
alert("error")
}
dr1a.onclick= "GameReset()";
dr2a.onclick= "GameReset()";
dr3a.onclick= "GameReset()";
}
Thank you
[EDIT: It was too long so I will post the second part below]
First, a 'best practices' note, function and variable names should not generally start with a capital letter, that is usually reserved for constructor functions.
A few observations: within your 'Prize' function you refer to variables 'pick' and 'final1' which do not exist within that function or are not globally accessible near as I can see. Also in your 'GamePartTwo' function you appear to have the same problem with references in there to a 'pick' variable which I don't see defined anywhere's in that function. You do use them in ImgReveal and GamePartOne as function arguments, but are not defined in the other functions.
Also, setting of the onclicks is incorrect, within functions do not attempt to set onclick's to string values, rather define a function for the onclick, such as:
And I don't see where winlose is defined in ImgReveal either... or door1 or door2 or door3...
Another little tip, don't know if you've covered objects yet or not, but this would be a good place to use a single string literal object, could contain all the functionality you need in one global object...
Last edited by astupidname; 10-26-2009 at 10:54 PM.
Bookmarks