如果这个问题的格式不正确,我道歉,这是我在SO上的第一篇帖子,也是我第一次真正被卡住。
在下面的代码中,函数playerChoice和computerChoice的返回值应该是‘石头’、‘布’或‘剪刀’。但每次我在控制台中记录它们时,它们都显示为未定义,并且我无法比较这两个选项。我需要这些值来继续开发网页,否则代码不能做任何事情来比较选择。我将在下面展示我尝试操作它们的位置(它们标有*:)
(我只展示了我试着把‘石头’值放在哪里,因为剪刀和布是放在同一个地方的。
任何帮助都是非常感谢的。
// buttons to be manipulated
const newButton = document.getElementById('newGameButton'); 
const rockSelect = document.getElementById('rockPicture');
const paperSelect = document.getElementById('paperPicture');
const scissorSelect = document.getElementById('scissorPicture');
// image or text fields to be manipulated
const playerWeapon = document.getElementById('playerWep');
const compWeapon = document.getElementById('compWep');
const pWepLabel = document.querySelector('.playerWeaponLabel');
const compWepLabel = document.querySelector('.compWeaponLabel');
const roundResult = document.getElementById('roundResult');
const playerScore = document.getElementById('scoreplayer');
const compScore = document.getElementById('scoreComp');
const img = '';
const src = ''; 
// player onclick function
let playerChoice = '';
function playerPlay() {
    
    rockSelect.addEventListener('click', function(){
        *** TRIED PLACING playerChoice = 'rock' HERE, LOGGED UNDEFINED ***
        chooseRock();
        computerPlay();
        if (document.getElementById('imageP')) {
            (document.getElementById('imageP')).remove();
        }
        const img = document.createElement('img');
        img.className = 'rock';
        img.id = 'imageP';
        img.src = 'images/rock1.png';
        const src = document.getElementById('playerWep');
        src.appendChild(img);
        *** TRIED PLACING playerChoice = 'rock' HERE, LOGGED UNDEFINED ***
     
    })
    paperSelect.addEventListener('click', function(){
        choosePaper();
        computerPlay();
        if (document.getElementById('imageP')) {
            (document.getElementById('imageP')).remove();
        }
        const img = document.createElement('img');
        img.className = 'paper';
        img.id = 'imageP';
        img.src = 'images/paper1.png';
        const src = document.getElementById('playerWep');
        src.appendChild(img);
        
      
    })
    scissorSelect.addEventListener('click', function(){
        chooseScissors();
        computerPlay();
        if (document.getElementById('imageP')) {
            (document.getElementById('imageP')).remove();
        }
        const img = document.createElement('img');
        img.className = 'scissors';
        img.id = 'imageP';
        img.src = 'images/scissors1.png';
        const src = document.getElementById('playerWep');
        src.appendChild(img);
        
    })
    
    function chooseRock() {
        pWepLabel.innerHTML = 'Rock';
        *** TRIED PLACING playerChoice = 'rock' HERE, LOGGED UNDEFINED ***
    }
    function choosePaper() {
        pWepLabel.innerHTML = 'Paper';
        
    }
    function chooseScissors() {
        pWepLabel.innerHTML = 'Scissors';
        
    }
    return playerChoice;
    
}发布于 2021-07-03 06:41:47
chooseRock和其他选择器应该填充一个内部变量以跟踪状态。我猜应该是playerChoice。因此,除了在HTML中显示它之外,还要将它保存到您选择的变量中,以跟踪选择。
未定义computerPlay函数。
另一个建议:试着把你的游戏逻辑和你的演示分开。
https://stackoverflow.com/questions/68229851
复制相似问题