最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

HTML, JavaScript and ERR_FILE_NOT_FOUND error - Stack Overflow

matteradmin8PV0评论

I have a simple html page with javascript code.

HTML

<!doctype html>
<html>
   <head>
      <meta charset="utf-8" />
      <title>Sink The Battle Ship</title>
   </head>
   <body>
      <h1>Battleship</h1>
      <script src="battleship.js"></script>
   </body>
</html>

JavaScript

var location = Math.floor(Math.random() * 5);
var numberOfGuesses = 0;
var isSunk = false;
var guess;
var guess = prompt("Ready, aim, fire! (enter a number from 0-6):");
var guessedLocation = parseInt(guess);
console.log(guess);
console.log(guessedLocation);

Every time I launch the html in browser, the prompt displays and when I enter a value, it gives me an error "ERR_FILE_NOT_FOUND". It looks like the browser is trying to re-direct to a page with the value I entered. Any idea what is going wrong here? I tried opening the html in different browsers and still no luck.

I have a simple html page with javascript code.

HTML

<!doctype html>
<html>
   <head>
      <meta charset="utf-8" />
      <title>Sink The Battle Ship</title>
   </head>
   <body>
      <h1>Battleship</h1>
      <script src="battleship.js"></script>
   </body>
</html>

JavaScript

var location = Math.floor(Math.random() * 5);
var numberOfGuesses = 0;
var isSunk = false;
var guess;
var guess = prompt("Ready, aim, fire! (enter a number from 0-6):");
var guessedLocation = parseInt(guess);
console.log(guess);
console.log(guessedLocation);

Every time I launch the html in browser, the prompt displays and when I enter a value, it gives me an error "ERR_FILE_NOT_FOUND". It looks like the browser is trying to re-direct to a page with the value I entered. Any idea what is going wrong here? I tried opening the html in different browsers and still no luck.

Share Improve this question edited Feb 19, 2017 at 3:29 Raju Joseph asked Feb 19, 2017 at 3:24 Raju JosephRaju Joseph 5331 gold badge7 silver badges15 bronze badges 2
  • 1 That did not help. – Raju Joseph Commented Feb 19, 2017 at 3:30
  • Please check my answer, I just mented here to say that, in my 5 years of js development, this is the first time I saw this error, hahaha, I had no idea this could happen but it does make sense. – Vinícius Negrão Commented Feb 19, 2017 at 3:35
Add a ment  | 

1 Answer 1

Reset to default 3

The problem is that you are redefining a global variable, called location.

When you declare a variable like this

var location = 1;

is the same as doing this

window.location = 1;

Location is a browser variable used to define in which page (location) the user is in.

You can do two things,

1 - Rename your variable location to: $location, location_2, my_location

var myLocation = Math.floor(Math.random() * 5);

2 - Create a local scope

(function(){
    var location = Math.floor(Math.random() * 5);
    var numberOfGuesses = 0;
    var isSunk = false;
    var guess = prompt("Ready, aim, fire! (enter a number from 0-6):");
    var guessedLocation = parseInt(guess);
    console.log(guess);
    console.log(guessedLocation);
})()

Also, stop re-declaring the variable guess, only use ONE 'var' for every variable name

(function(){
  var location = Math.floor(Math.random() * 5);
  var numberOfGuesses = 0;
  var isSunk = false;
  var guess;
  var guess = prompt("Ready, aim, fire! (enter a number from 0-6):");
  var guessedLocation = parseInt(guess);
  console.log(location);
  console.log(guessedLocation);
  guessedLocation == location ? console.log('you sank me!') : console.log('ha! missed...')
})();
<!doctype html>
<html>
   <head>
      <meta charset="utf-8" />
      <title>Sink The Battle Ship</title>
   </head>
   <body>
      <h1>Battleship</h1>
      <script src="battleship.js"></script>
   </body>
</html>

Post a comment

comment list (0)

  1. No comments so far