Help with 'is not a function' error, Functions were working fine


#1


Ok, so been working on my RPS game.. adding various bits as i figure them out as a learning exercise. This morning i encountered the error 'checkinput is not a function', and i have no idea why, but i added a console.log before the function to see if it was getting that far, and then removed it, and this somehow fixed my error. Twice.

Now im trying to add a single/ multiplayer option and im getting the same error. Now i know these functions work as before i added this menu the game functioned fine, yet when i remove it, i return to the 'checkinput is not function' error. Can anyone shed any light on what is happening as it isnt making any sense to me?? :confused:

https://www.codecademy.com/en/courses/javascript-beginner-en-Bthev-mskY8/0/3?curriculum_id=506324b3a7dffd00020bf661


Replace this line with your code. 
//***********************************************************//
//This is the intro + main menu
var rps = function(){
console.log ("Rock, Paper Scissors! by DDUk\n\nWelcome to RPSv1.01! When Prompted, please enter your name and click 'Ok'\n");
name = prompt("What is your name?");
console.log ("Ok, thank you " + name + ", Let's Play Rock, Paper Scissors!\n");
mainmenu = prompt ("Would you like a 1 or 2 player game?");
if (mainmenu === "1") {console.log ("Loading AI Player...");
singleplayer();}
else {console.log ("Loading multiplayer"); 
multiplayer ();}
};
rps();
//***********************************************************//
//This is the single player  function!
var singleplayer = function(){
userChoice = prompt("Hi " + name + ", Do you choose Rock, Paper or Scissors?");
checkinput();
//This does the PC choice
choice2 = Math.random();
{
if (choice2 < 0.34) {choice2 = "Rock";} 
else if(choice2 <= 0.67) {choice2 = "Paper";}
else {choice2 = "Scissors";}
}
//This calls the compare function and displays the result!
console.log ("Computers Choice: " + choice2 +"\n");
compare(userChoice, choice2);
};
singleplayer();
//***********************************************************//
//This is the multiplayer function!
var multiplayer = function(){
userChoice = prompt("Hi " + name + ", Do you choose Rock, Paper or Scissors?");
checkinput();
//This does player 2's choice
name2 = prompt("Player 2, what is your name?")
choice2 = prompt("Hi " + name2 + ", Do you choose Rock, Paper or Scissors?");
//This calls the compare function and displays the result!
console.log (name2 + "'s Choice: " + choice2 +"\n");
compare(userChoice, choice2);
};
multiplayer();
//***********************************************************//
// These functions apply to all game modes!

//This funstion is to check the input!
var checkinput = function(){
if (userChoice === "Rock"){
console.log ("Your Choice: " + userChoice);}
else if (userChoice === "Scissors"){return ("Your Choice: " + userChoice);}
else if (userChoice === "Paper"){return ('Your Choice: ' + userChoice);}
else {alert ("Invalid choice, please retry!");
singleplayer();} // only called if input is incorrect
};

// This function is to compare the choices and determines a winner! (Singleplayer Only)
var compare = function(userChoice, choice2) {
if (name === "Chuck Norris") {console.log ("Nothing beats " + name + ", " + name + " wins!");}
else if (userChoice === choice2) {console.log ("The result is a tie, please retry!\n");
singleplayer ();}
else if (userChoice === "Rock" && choice2 === "Paper",  userChoice === "Paper" && choice2 === "Scissors",
userChoice === "Scissors" && choice2 === "Rock")
{console.log (choice2 + " beats " + userChoice + "\nComputer Wins!");
alert ("Computer Wins!");}
else { console.log (userChoice + " beats " + choice2 + "\n" + name + " Wins!");
alert ("Congratulations, you win!");
}
};
//***********************************************************//

//This is the end of the game.


#2

@dduk,
If you use the syntax

var myFunction = function() {};

You are actually assigning a so-called anonymous function to the variable myFunction.

You can only use the functionality of myFunction AFTER the definition.

There is however
a second posibillity....
=defining= a so-called named Function
and you would do that with

function myFunction() { }

(no semi-colon-; needed )

Javascript has 2 phases if execute code
- parsing phase the code is loaded
- execution phase the loaded code is executed.

Now if you have used the named Function syntax
the function is hoisted
meaning it is put up-front during the parse phase
and thus you are able to use the function
before it is seen in your code.....

So you either place

var rps = function(){
console.log ("Rock, Paper Scissors! by DDUk\n\nWelcome to RPSv1.01! When Prompted, please enter your name and click 'Ok'\n");
name = prompt("What is your name?");
console.log ("Ok, thank you " + name + ", Let's Play Rock, Paper Scissors!\n");
mainmenu = prompt ("Would you like a 1 or 2 player game?");
if (mainmenu === "1") {console.log ("Loading AI Player...");
singleplayer();}
else {console.log ("Loading multiplayer"); 
multiplayer ();}
};
rps();

at the END-OF-YOUR code
OR
you start using named Function's

http://stackoverflow.com/questions/336859/var-functionname-function-vs-function-functionname?rq=1


#3

EDIT

Moved on from this.... didnt manage to get named variables working, but I see now that I had mis-read your instructions and will return to this at a later date.

Thanks again for your advice.


#5

Just to update, named functions worked great. Thank you, very helpful