Make the following variables (all of them should be OUTSIDE OF EVERY FUNCTION ON TOP OF MAIN.JS), along with the variables I left for you containing images: computerNumber, start it with -1 or any other numberdefine a function and attach it to each of your buttons (and/or to the user hitting the enter key). Call it takeTurn. This function should take a parameter. 1 for rock, 2 for paper, 3 for scissors:each button should have a function call with a different number passed in as a parameter based on how the function should expect numbersIn the function, reassign computerNumber to a random number between 1 and 3 (use randomInt() )Also put two alerts, one to show the user's number (representing which button they pushed) and one to show the computer's random numberYou are done when the alert in the function works correctly
Question
Make the following variables (all of them should be OUTSIDE OF EVERY FUNCTION ON TOP OF MAIN.JS), along with the variables I left for you containing images: computerNumber, start it with -1 or any other numberdefine a function and attach it to each of your buttons (and/or to the user hitting the enter key). Call it takeTurn. This function should take a parameter. 1 for rock, 2 for paper, 3 for scissors:each button should have a function call with a different number passed in as a parameter based on how the function should expect numbersIn the function, reassign computerNumber to a random number between 1 and 3 (use randomInt() )Also put two alerts, one to show the user's number (representing which button they pushed) and one to show the computer's random numberYou are done when the alert in the function works correctly
Solution
Sure, here is a step-by-step guide on how to implement this in JavaScript:
- First, declare the variable
computerNumberat the top of yourmain.jsfile and initialize it with-1or any other number. This should be outside of any function:
let computerNumber = -1;
- Next, define a function called
takeTurn. This function should take a parameter which will be1for rock,2for paper, and3for scissors:
function takeTurn(userNumber) {
// function body will go here
}
- Inside the
takeTurnfunction, reassigncomputerNumberto a random number between1and3. You can use theMath.floor(Math.random() * 3) + 1to generate a random number in this range:
function takeTurn(userNumber) {
computerNumber = Math.floor(Math.random() * 3) + 1;
// rest of the function body will go here
}
- Still inside the
takeTurnfunction, add twoalertstatements. One should show the user's number (which is the parameter of the function) and the other should show the computer's random number:
function takeTurn(userNumber) {
computerNumber = Math.floor(Math.random() * 3) + 1;
alert("User's number: " + userNumber);
alert("Computer's number: " + computerNumber);
}
- Finally, attach this function to each of your buttons. The function call should have a different number passed in as a parameter based on the button. For example, if you have buttons with ids
rock,paper, andscissors, you can do this:
document.getElementById('rock').onclick = function() { takeTurn(1); };
document.getElementById('paper').onclick = function() { takeTurn(2); };
document.getElementById('scissors').onclick = function() { takeTurn(3); };
This will call the takeTurn function with the appropriate parameter when each button is clicked.
Similar Questions
Make the following variables (all of them should be OUTSIDE OF EVERY FUNCTION ON TOP OF MAIN.JS), along with the variables I left for you containing images: num1 a random number between 0 and 9num2 a random number between 0 and 9num3 a random number between 0 and 9totalGuesses start it with a 0use randomInt()define a function and attach it to your button (and/or to the user hitting the enter key). Call it makeGuess. This function should:find what the user typed in the inputs for each number, store it in a variable called userGuessclear the inputsalert all three of the user numbers so you can see it is workingYou are done when the alert in the function works correctly
each button should have a function call with a different number passed in as a parameter based on how the function should expect numbers
make a variable outside of all functions called numGamesdefine another function called setNumberOfGames. Attach it to the button for choosing the number of games. It should find the input where the user chose their number of games, and store the number in numGamesIt should also put that number in the innerHTML of the paragraph on the bottom of the page.Go back to the takeTurn function. In it, every time the function is called, the numGames variable should go down by 1 and the new value should go in the paragraph at the bottom of the pageYou are done if the button set the number at the bottom of the page, and clicking rock, paper, or scissors, reduces that number by 1 each time.
Create In your HTML:a heading that says "Play Rock Paper Scissors"a paragraph underneath it with an input tag and a button in it. The paragraph should say "How many games would you like to play?" then the input, then a button that says "choose number of games"another paragraph that contains three buttons, one that says Rock, one that says paper, one that says scissorsa table with 2 rows and 3 boxes in each rowA paragraph that says "Games to play"A paragraph that says "0"Style it up nicely so it looks goodYou are done when you have all of this
remove the alert from part 2, you don't need it anymoremake a variable outside of all functions called numGamesdefine another function called setNumberOfGames. Attach it to the button for choosing the number of games. It should find the input where the user chose their number of games, and store the number in numGamesIt should also put that number in the innerHTML of the paragraph on the bottom of the page.Go back to the takeTurn function. In it, every time the function is called, the numGames variable should go down by 1 and the new value should go in the paragraph at the bottom of the pageYou are done if the button set the number at the bottom of the page, and clicking rock, paper, or scissors, reduces that number by 1 each time.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.