0

I am reading "Head First: Javascript" currently, and there's a task to create "battleship". I followed each step and my code looks exactly, the one from the book, but my code always generates the same number, which should get added by 1, each time. This means, if it generates the number 2, the following 2 numbers should be 3 and 4 (3 locations = 1 ship). I want it to be between 0 and 4 (because if it's 4, the 5 and 6 are also on the raster.) The user should pick a number between 0 and 6.

The random numbers are ALWAYS: 0, 1 and 1.. Here's the code:

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

var location1 = randomLoc;
var location2 = location1++;
var location3 = location2++;

var guess;

var hits = 0;

var guesses = 0;


var isSunk = false;

alert(location1 + " " + location2 + " " + location3); //To show the numbers for debugging.

while (isSunk == false) {

    guess = prompt("Anlegen, Zielen Feuer! (Geben Sie eine Zahl zwischen 0 und 6 ein) :");

    if (guess < 0 || guess > 6) {
        alert("Diese Zahl (sofern es eine war) liegt nicht auf dem Raster")
    } else {
        guesses += 1;

        if (guess == location1 || guess == location2 || guess == location3) {
            alert("Treffer!")
            hits += 1;

            if (hits == 3) {
                isSunk = true;

                alert("Schiff versenkt!");
            }
        } else {
            alert("Daneben!");
        }
    }

}
var stats = "Sie haben " + guesses + " Versuche gebraucht, um das Schiff zu versenken. " +

    "Das entspricht einer Genauigkeit von " + (3 / guesses) * 100 + "%";
alert(stats);
Tom Otten
  • 1
  • 2

2 Answers2

1

Because Math.random() generates a number between 0 to 1. When you Math.floor(Math.random()), you always get 0 as output. What you really want is Math.floor(Math.random()*5).

Math.random()*5 will scale the output range to 0 to 5. So the flooring function would actually do as expected, truncating the decimals after 0 to 4 inclusively.

One way to test, in the future, is to use the Console in your browser via F12. You can test your code bit by bit, to see where you have gone wrong, to debug your script.

Daniel Cheung
  • 4,779
  • 1
  • 30
  • 63
  • Ah! Changed it. Now the first number is random, but the following 2 are always same, like this line: var location3 = location2 += 1; wouldn't work.. – Tom Otten Nov 30 '18 at 15:34
0

There are actually two problems with the number generation here:

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

var location1 = randomLoc;
var location2 = location1++;
var location3 = location2++;

Math.random() returns a number between 0 and 1, so Math.floor(Math.random()) will always be 0. That line should be Math.floor(Math.random() * 5).

Secondly, the ++ operator increments the variable, and it does so after everything else, so when you say location2 = location1++ you're saying "Assign the current value of location1 to location2, then increment location1."

Those lines should be just:

var location1 = randomLoc;
var location2 = location1 + 1;
var location3 = location2 + 1;
Herohtar
  • 5,347
  • 4
  • 31
  • 41