0

When I run this code I get the following:

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

I have no prior experience with programming or Javascript but I'm excited to learn. Any Input is appreciated ^_^

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);
15
  • 2
    prompt is a JavaScript method available in web browsers. It's not available in Node. Commented Apr 19, 2014 at 0:30
  • 1
    Please don't downvote beginner questions. Let's encourage, not discourage. Commented Apr 19, 2014 at 0:32
  • 1
    @Matt Yes, but also don't sympathy-upvote them because of the rep imbalance. Commented Apr 19, 2014 at 0:36
  • 3
    @Matt You don't seem to understand downvotes. A downvote means "this post has questionable-quality content, an edit may help". When it gets improved, it will receive upvotes. People who downvote are not evil. If you really want, you could leave this site since your attitude doesn't seem very positive. Commented Apr 19, 2014 at 0:49
  • 2
    @Matt Explanations are encouraged for downvotes. If somebody doesn't leave an explanation, too bad. They don't have to. If you want to, that would be great! Saying "the community doesn't look at downvoted content" is wrong. Most views come from Google or the home page, neither of which are affected by voting. Many times I will specifically find bad posts to improve, not everyone thinks it's just a waste of time. You seem to, which is just the wrong attitude for SO. If you think that downvotes are bad, they will be. Commented Apr 19, 2014 at 1:00

1 Answer 1

2

prompt() (actually window.prompt()), is not availabe in the Node.js environment. Use Node's "readline" module instead.

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();
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.