簡體   English   中英

JS錯誤(預期為類,接口或枚舉)

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

我是JS的新手,正在嘗試制作石頭,紙,剪刀游戲...

它不斷提出這個錯誤

error: class,interface, or enum expected

這是我的代碼:

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); 僅在Chrome中受支持

更改

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

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

可能不只是將return放在末尾,您可能希望使其成為函數。 現在,您的“ ref”變量包含一個數組,因為這是裁判函數返回的內容。

我看到Hitham S. AlQadheeb擊敗了我,答案大聲笑!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM