首页 > 解决方案 > 试图让我的第一个 JavaScript 项目工作,完整的功能显示在“控制台”中,而不是我想要的代码的实际结果

问题描述

我正在做我的第一个 JS 项目,它是一个简单的 Rock、Paper、Scissor 游戏,应该可以在控制台中运行。

我认为我的代码是非常正确的,但是当我“console.log”我的函数(playRound)时,我在“console”中得到以下片段:“你们都玩了 undefined It's a Draw!”。我不确定我错过了什么,所以如果有人可以看看,那将非常感激!

这是我的脚本:

function computerPlay (){
    const choices = ["Rock", "Paper", "Scissor"];
    const randomChoice = choices[Math.floor(Math.random() * choices.length)];

    return(randomChoice);
    }


console.log(computerPlay()) //just to show "computerPlay" works

function playRound(playerSelection, computerPlay) { //This is the actual round

    if (playerSelection == computerPlay) {
        result = `You both played ${computerPlay} It's a Draw!`;
    } else if ((playerSelection == "Paper" && computerPlay == "Rock") ||
               (playerSelection == "Rock" && computerPlay == "Scissor") ||
               (playerSelection == "Scissor" && computerPlay == "Paper")) {
                   result = `${playerSelection} beats ${computerSelection} You Win!`;
               } else {
                   result = `${computerPlay} beats ${playerSelection} You Loose!`;
               }
            return (result);
        }

        let playerSelection = 'rock'

console.log(playRound()) // to test if "playRound" works

感谢您的检查!

亲切的问候,编码新手

标签: javascriptfunctiondebuggingconsole.log

解决方案


定义函数后,应使用参数调用它以查看结果。playRound 函数有 2 个参数,请替换console.log(playRound)console.log(playRound(playerSelection, computerPlay())).


推荐阅读