Reputation: 2607
let me first say that I indeed began by searching and there was a very similar question, but it was sufficiently different that I remain confused.
Disclaimer: I'm making a Rock Paper Scissors game for a homework assignment and just have a quick question about appropriate javascript function/parameter passing.
I have a function called opponentRandom() that returns a randomized "rock or paper or scissors" value as the opponent's (computer's) choice.
I have another function compareChoices() that compares your value (determined by an "onclick" in the html document) with the value from the opponentRandom() function.
Now, inside the table where the game resides, one of the table data (to give you guys an example of how clicking one of the pictures is supposed to work) is as follows:
<td onclick="comparisonChoices('paper', 'opponentRandomizer()**;**');">
<img src="paper.jpg" alt="paper" />
</td>
Now, the first parameter in the comparison is supposed to be your choice, and I would really like if I could easily just call the opponentRandom function inside the compareChoices function inside the td onclick, because then the the random result would be considered inside the compareChoices function.
Does this work? Are you allowed to just throw a function inside another called function, as a parameter? the opponentRandom function does indeed return either a rock, paper, scissors value, and the comparison function then either determines whether you won, lost, or tied. I don't know the appropriate syntax or if I can even do this, and I haven't yet integrated the javascript into my html and I don't know any easy way to implement it until I have all the functions necessary for the game to run.
TL;DR - Can you put a function that returns a value inside another function being called as a onclick="function(param,'function()');" ?
Also,should the bolded semi-colon I have above be there? (EDIT: the ** ** is not showing up as bolded, but I am talking about the semi-colon surrounded by the ** **)
Upvotes: 0
Views: 157
Reputation: 360762
what's wrong with:
function comparisonChoices(selection) {
other_selection = opponentRandomizer();
etc....
}
?
Upvotes: 2