Javascript问题*编程新手*

时间:2014-04-19 00:26:36

标签: javascript node.js

当我运行此代码时,我得到以下内容:

at Module._compile (module.js:456:26)

at Object.Module._extensions..js (module.js:474:10)

at Module.load (module.js:356:32)

at Function.Module._load (module.js:312:12)

at Function.Module.runMain (module.js:497:10)

at startup (node.js:119:16)

at node.js:902:3

我没有编程或Javascript的经验,但我很高兴学习。感谢任何输入^ _ ^

var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
if (computerChoice < 0.34) {
    computerChoice = "rock";
} else if(computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
} console.log("Computer: " + computerChoice);
var compare = function(choice1, choice2) {
if (choice1 === choice2) {
    return "The result is a tie!";
}
else if (choice1 === "rock") {
    if (choice2 === "scissors") {
        return "rock wins";
    }
    else {
        return "paper wins";
    }
}
else if (choice1 === "paper") {
    if (choice2 === "rock") {
        return "paper wins";
    }
    else {
        return "scissors wins";
    }
}
if (choice1 === "scissors") {
    if (choice2 === "rock") {
        return "rock wins";
    }
    else {
        return "scissors wins";
    }
}
}
compare(userChoice, computerChoice);

1 个答案:

答案 0 :(得分:2)

prompt()(实际上是window.prompt())在Node.js环境中不可用。请改用节点“readline”模块。

var readline = require('readline');

var rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout
});

rl.question("Do you choose rock, paper or scissors?", function(answer) {
  // code to handle the answer goes here
  rl.close();
});