首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >变量总是返回未定义的值

变量总是返回未定义的值
EN

Stack Overflow用户
提问于 2021-07-03 02:27:51
回答 1查看 52关注 0票数 0

如果这个问题的格式不正确,我道歉,这是我在SO上的第一篇帖子,也是我第一次真正被卡住。

在下面的代码中,函数playerChoice和computerChoice的返回值应该是‘石头’、‘布’或‘剪刀’。但每次我在控制台中记录它们时,它们都显示为未定义,并且我无法比较这两个选项。我需要这些值来继续开发网页,否则代码不能做任何事情来比较选择。我将在下面展示我尝试操作它们的位置(它们标有*:)

(我只展示了我试着把‘石头’值放在哪里,因为剪刀和布是放在同一个地方的。

任何帮助都是非常感谢的。

代码语言:javascript
运行
复制
// 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;
    
}
EN

Stack Overflow用户

发布于 2021-07-03 06:41:47

chooseRock和其他选择器应该填充一个内部变量以跟踪状态。我猜应该是playerChoice。因此,除了在HTML中显示它之外,还要将它保存到您选择的变量中,以跟踪选择。

未定义computerPlay函数。

另一个建议:试着把你的游戏逻辑和你的演示分开。

票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68229851

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档