Help with a bug on my minesweeper project

Hey guys, long time no talk :wave:

Since finishing the FCC Javascript module, I have been taking on another javascript and i’m working on creating a Minesweeper game.
Everything is going well so far BUT I have an issue when it comes to generating mines randomly.

/* function to generate mine locations on the board */
export function generateMines(numberOfMines, boardSize) {
  // while loop until you reach the number of mines
  const minePositions = [];
  while (minePositions.length < numberOfMines) {
    const position = {
      x: getRandomIntInclusive(boardSize),
      y: getRandomIntInclusive(boardSize),
    if (
      !minePositions.some((p) => {
        comparePositions(p, position);
    ) {
  return minePositions;

/* function to generate random integers to use for mine locations */
function getRandomIntInclusive(size) {
  const min = 1;
  const max = size;
  return Math.floor(Math.random() * (max - min + 1) + min);

/* function to compare X and Y positions */
export function comparePositions(a, b) {
  return a.x === b.x && a.y === b.y;

Here is what the code does

  1. Create an empty array
  2. Generate random X and Y, check if they’re in the array already and if not, push them to the array

But when I run that function, sometimes I wold end up with two items having the same coordinates, and I can’t figure out why…

Anything I’m missing?

Taking a quick look at this:

    if (
      !minePositions.some((p) => {
        comparePositions(p, position);

gives me pause. Specifically the callback:

(p) => { comparePositions(p, position); }

The callback needs to returns truthy if there is a match. But this will always return undefined because it is not returning anything. You can either include a return:

(p) => { return comparePositions(p, position); }

or you could use the arrow functions implicit return:

(p) => comparePositions(p, position)

I think that would help it work better.

1 Like

You’re the BEST Kevin… how could I miss that hahaha.
It’s working now.
Thanks a bunch :pray::pray:

Because it’s easy to miss. Sometimes we just need an extra set of eyes. At work (before the apocalypse left us holed up in our homes) sometimes my cubicle mate and I would just turn to to each other and say, “I’ve spent an hour on this and just can’t see the problem, can you take a look?” That’s just life. We get better at finding our bugs, but we never get perfect.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.