3

I created a table using an 8x8 matrix for a game and i also put into the table prizes, and etc in random places but I am having an issue creating also random start position for the user that doesn't conflict with the objects that are already in the table.

For now I have:

function startPos(matrix) {
  var x = Math.round(Math.random() * matrix.length);
  var y = Math.round(Math.random() * matrix.length);

  while (matrix[y][x] != undefined) {
    var x = Math.round(Math.random() * matrix.length);
    var y = Math.round(Math.random() * matrix.length);
    return matrix[y][x];
  };


  return matrix[y][x];
};

but I get nothing. Sorry if the question seems trivial I am just starting Javascript and have looked everywhere for a relevant answer with no avail.

1
  • 1
    the problem is that you're returning on the first iteration of your while loop. If you remove that return, then your while loop will iterate until it finds an x and y that work, then the loop will end and your code will hit the final return Commented Mar 20, 2017 at 22:01

2 Answers 2

1

A few mistakes:

  • you must not return from within the loop, but have to check the condition to leave it
  • you need to use Math.floor instead of Math.round
  • you need to return the position, not the value of the field (which you just asserted to be undefined)

function startPos(matrix) {
  var l = matrix.length; // assumes square matrix
  do { // assumes at least one empty field
    var x = Math.floor(Math.random() * l);
    var y = Math.floor(Math.random() * l);
  } while (matrix[y][x] != undefined);
  return [y, x];
}
Sign up to request clarification or add additional context in comments.

Comments

0

It looks like what you want to do is select a random position in the matrix, if it is undefined then return it, otherwise select a new random position and try again. You need to make a few changes for this.

First - the return statement in your loop is unnecessary and is causing your function to be returned on the first run of the loop, stopping it from doing its job.

Second - In most cases, you should use === and !== instead of == and !=. You can find a detailed explanation here - Which equals operator (== vs ===) should be used in JavaScript comparisons?

Third - When you want to check if a variable is undefined, while myVar === undefined should work most of the time, there a situations where it could fail. best practice is to use typeof myVar === 'undefined'. More info here - https://stackoverflow.com/a/15093969/7739148

try this:

function startPos(matrix){
  // first select a random position
  var x = Math.round(Math.random() * matrix.length);
  var y = Math.round(Math.random() * matrix.length);

  // if the position is not empty, select a new one.
  // continue like this until an empty spot is found.
  while(typeof matrix[y][x] !== 'undefined'){
    x = Math.round(Math.random() * matrix.length);
    y = Math.round(Math.random() * matrix.length);
  };

  // once we have an empty position, return it
  return matrix[y][x];
};

Caution - if there are no positions that are undefined the loop will never end, so you should either make sure your matrix will have at least one empty spot, or perform a check at the start of your function.

3 Comments

typeof … !== undefined is always true
"the value of x and y are never changed […] you are creating new variables that only exist within the scope of your loop" - Nope. var has function scope
oops! meant to put typeof x === 'undefined' with undefined as a string. And you are correct about the function scoping. I have edited my answer to reflect.

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.