Javascript问题*编程新手*

Javascript issues *New to programming*

本文关键字:新手 编程 Javascript 问题      更新时间:2023-09-26

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

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);

prompt()(实际上是window.prompt())在Node.js环境中不可用。请改用Node的"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();
});