简体   繁体   English

JS错误(预期为类,接口或枚举)

[英]JS Error (class,interface, or enum expected)

I am new to JS and trying to make a Rock, Paper, Scissors Game... 我是JS的新手,正在尝试制作石头,纸,剪刀游戏...

It keeps on coming up with this error 它不断提出这个错误

error: class,interface, or enum expected

Here is my code: 这是我的代码:

function referee(){

    var training = {};

    function learn(winner,loser){
        if (!training[winner]) training[winner] = {};
        training[winner][loser]=1;
    }

    function judge(play1,play2){
        if (play1 === play2){ return "tie"; }
        return ( (training[play1][play2] === 1)? play1: play2 )+ "wins!";
    }

    function validate(choice) {
        return choice in training;
    }

    function choices() {
        return Object.keys(training);
    }

    return {
        "learn": learn,
        "judge": judge,
        "validAction": validate,
        "getChoices": choices
    };
}



var ref = referee();
ref.learn("rock","scissors");
ref.learn("paper","rock");
ref.learn("scissors","paper");

do {
   var userChoice = prompt("Do you choose rock, paper or scissors?");
} while(!ref.validAction(userChoice))

var choices = ref.getChoices(),
computerChoice = choices[Math.floor(Math.random()*choices.length)];

console.log("User Choice: " + userChoice); 
console.log("Computer Choice: " + computerChoice);
console.log(ref.judge(userChoice, computerChoice));

Object.keys(training); is only supported in Chrome 仅在Chrome中受支持

Change 更改

function choices() {
    return Object.keys(training);
}

to

function choices() {
   var keys = [];
   for(var key in this) keys.push(key);
   return keys;
}

Instead of just placing the return at the end, you might want to make it a function. 可能不只是将return放在末尾,您可能希望使其成为函数。 Right now your "ref" variable holds an array, since that's what the referee function returns. 现在,您的“ ref”变量包含一个数组,因为这是裁判函数返回的内容。

I see Hitham S. AlQadheeb has beat me to the answer lol! 我看到Hitham S. AlQadheeb击败了我,答案大声笑!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM