gpt4 book ai didi

javascript - 将循环函数的值存储到变量中,javascript

转载 作者:行者123 更新时间:2023-12-03 01:52:34 24 4
gpt4 key购买 nike

嘿,我需要你的帮助。在这个石头剪刀布游戏中,获胜者是用户和计算机之间第一个获得五胜的人。虽然我还没有循环这个,但我需要帮助将每次胜利的计数存储到一个变量中,从我的 point() 函数到 var playerPoints = 0 ;或者var compPoints = 0 ;取决于谁赢得了这一轮。如果您有循环建议,请随时向我提出建议!谢谢

    //decalring an array with weapons of choice
const weaponArray = ["rock", "paper", "scissors"];
// selects a random index from array to represent computer choice
const computerChoice = weaponArray[[Math.floor(Math.random() * weaponArray.length)]];
//prompts user to make a choice of weapon
const playerPrompt = prompt("Please enter Rock, Paper, or Scissors!");
//lowers all alphabet input to lower case
const playerChoice = playerPrompt.toLowerCase();




//function runs player vs computer choices and determines winner of round
const round = (computerChoice, playerChoice) => {
if (playerChoice === "scissors" && computerChoice === "rock" || playerChoice === "rock" && computerChoice === "paper" || playerChoice ==="paper" && computerChoice === "scissors") {
return "Just Give Up Now Looser!";
}
else if (playerChoice === "rock" && computerChoice === "scissors" || playerChoice === "paper" && computerChoice === "rock" || playerChoice==="scissors" && computerChoice === "paper")
{
return "You Won This Round!";
}
else {
return "Its a Tie!";
}
};

//stores round function value into a variable
var state = round(computerChoice, playerChoice);


// adds points to player or computer based on "state" value
const points = () => {
if (state === "You Won This Round!"){
return playerPoints + 1;
}
else if (state === "Just Give Up Now Looser!"){
return compPoints + 1;
}
else{
return null
}

};


var playerPoints = points() ;
var compPoints = points() ;



console.log(state);
console.log(points());
//console.log(compPoints);
//console.log(playerPoints);

最佳答案

不妨在这里添加我的答案。我不会使用提示方法,而是使用按钮。将分数存储在一个对象中,并使用条件来检查某人在每场比赛后是否达到了 5 分:

const userScore = document.querySelector('.user .score')
const computerScore = document.querySelector('.computer .score')
const resultContainer = document.querySelector('.result')
const userThrowContainer = document.querySelector('.userThrow')
const opponentThrowContainer = document.querySelector('.opponentThrow')
const buttons = document.querySelectorAll('button')

const score = {
user: 0,
computer: 0
}

function rpsMatch(userThrow) {
// Define possible throws
const throwOptions = [
'rock',
'paper',
'scissors'
]

// Choose randomly from array of throws
let opponentThrow = throwOptions[Math.floor(Math.random() * throwOptions.length)]

// Print user and computer throws
userThrowContainer.innerText = `You threw ${userThrow}`
opponentThrowContainer.innerText = `Computer threw ${opponentThrow}`

function userWins() {
resultContainer.innerText = 'You win'
score.user++
updateScores()
}

function computerWins() {
resultContainer.innerText = 'You lose'
score.computer++
updateScores()
}

function updateScores() {
userScore.innerText = score.user
computerScore.innerText = score.computer
}

function resetScore() {
userScore.innerText = 0
computerScore.innerText = 0
score.user = 0
score.computer = 0
}

// RPS logic
if (userThrow == opponentThrow) {
resultContainer.innerText = 'You tied'
} else {
if (userThrow == 'rock') {
opponentThrow == 'scissors' ? userWins() : computerWins()
} else if (userThrow == 'paper') {
opponentThrow == 'rock' ? userWins() : computerWins()
} else {
opponentThrow == 'paper' ? userWins() : computerWins()
}
}

if (score.user === 5) {
alert('You won the first to 5!')
resetScore()
}

if (score.computer === 5) {
alert('You lost the first to 5!')
resetScore()
}
}

// Attach event handlers to each button
buttons.forEach(button => {
button.addEventListener('click', e => {
// Assign data attribute to variable
let userThrow = e.target.dataset.type
e.preventDefault()
// Pass user selection to rpsMatch
rpsMatch(userThrow)
})
})
<div class="user">User Score: <span class="score">0</span></div>
<div class="computer">Computer Score: <span class="score">0</span></div>
<button data-type="rock">Rock</button>
<button data-type="paper">Paper</button>
<button data-type="scissors">Scissors</button>
<div class="userThrow"></div>
<div class="opponentThrow"></div>
<div class="result"></div>

关于javascript - 将循环函数的值存储到变量中,javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50353909/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com