0

I am newbie with javascript and I am writing a very simple javascript according to the textbook and here is the code

var location1 = 3;
var location2 = 4;
var location3 = 5;
var guess;
var hits = 0;
var guesses = 0;
var isSunk = false;

while (isSunk == false) {
  guess = prompt("Ready, aim, fire! (enter a number from 0-6):") // prompt instead of promt
  if (guess < 0 || guess > 6) {
    alert("Please enter a valid cell number!");
  } else {
    guesses = guesses + 1;
    if (guess == location1 || guess == location2 || guess == location3) {
      alert("HIT!");
      hits = hits + 1;
      if (hits == 3) {
        isSunk = true;
        alert("You sank my battleship!");
      }
    } else {
      alert("MISS");
    }
  }
}
var stats = "You took " + guesses + " guesses to sink the battleship, " +
  "which means your shooting accuracy was " + (3 / guesses);
alert(stats);

Then, I installed nodejs, and tried to run this file by many ways. but it said to me many error. The 1st error is, when I ran it directly

Object expected
800A138F
microsoft jscript runtime error

the second error is, when I ran it by the command node battleship.js, here is the error the error about the command "node battleship.js

As you can see in the picture, it said to me that promt is not defined. My problem is, I ran another code no problem. Which means nodejs no problem. And, I ran my code online no problem, which means my code no problem.

So, how can I fix this one ? Could you please help me with this ? Thank you very much for your time.

5

1 Answer 1

1

Node.js isn't the same as running javascript in the browser, it doesn't have the window object, which has things like prompt() or alert(). in browsers, those show up as a pop-up.

Instead of alert(), you can probably just use console.log(), and for prompt(), you can look at this https://nodejs.dev/learn/accept-input-from-the-command-line-in-nodejs

const readline = require('readline').createInterface({
    input: process.stdin,
    output: process.stdout
})

readline.question(`What's your name?`, name => {
    console.log(`Hi ${name}!`)
    readline.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.