1

This is my first question and I'll try to be clear.

I need to assign a value in positions. I have a two-dimensional array of 7 rows ( for example). Depending on the value someone give me, I have to change value positions.

Example If someone give me: 3. I must put these 3, in the following order.

1 * * * * * * * * * 
2 * * * * * * * * * 
3 * * * * * * * * O 
4 * * * * * * * * O 
5 * * * * * * * * O 
6 * * * * * * * * * 
7 * * * * * * * * * 

If some one give me: 2

1 * * * * * * * * * 
2 * * * * * * * * * 
3 * * * * * * * * O 
4 * * * * * * * * O 
5 * * * * * * * * * 
6 * * * * * * * * * 
7 * * * * * * * * * 

I define de some start point in the "middle" of the array. These i do OK. start point in the middle. S = Start point

1    * * * * * * * * * 
2    * * * * * * * * * 
3    * * * * * * * * * 
4    S * * * * * * * * 
5    * * * * * * * * * 
6    * * * * * * * * * 
7    * * * * * * * * * 

The problem is when i must put '0' in the final of the array, because the number of '0' is variable and must be centered I have these code:

var height = 7;
var width = 9;
var exit = 3; // or can be 2, or 4, 5 etc.

var matrix = new Array(height); 
for (var i=0; i<height; i++) { 
    matrix[i] = new Array(width); 
    for (var j = 0; j<width; j++) { 
        matrix[i][j] = *; 
    } 
}

function startPoint(height, matrix){
    // Se define el punto de partida
    var startPoint = Math.round(height/2) -1;
    document.write("el punto de partida es: "+startPoint)
    matrix[startPoint][0] = 'S';
    return matrix
}

function finishPoint(exit, matrix){
    // punto de salida.

    return matrix
}

I'm learning JavaScript and I found a problem that I can not resolv. I'm trying and I can not have a clear idea.

Andreas
  • 21,535
  • 7
  • 47
  • 56
  • The `*` in `matrix[i][j] = *` has to be a string `"*"` (there must be an error message in the console "Uncaught SyntaxError: Unexpected token *", have a look at: [How can I debug my JavaScript code?](http://stackoverflow.com/questions/988363/how-can-i-debug-my-javascript-code)). And don't use `document.write` after the page is loaded, as this will erase the complete document -> [`document.write()`](https://developer.mozilla.org/en-US/docs/Web/API/Document/write). You can use `console.log()` instead – Andreas Oct 28 '15 at 06:27
  • hey, did you get it to work? Did you try my example? – AwokeKnowing Oct 28 '15 at 17:44
  • Thank you very much for the advice. I will review the documentation you comment. – Gabriel Calle Torrez Oct 28 '15 at 19:29

2 Answers2

0

To center an array of k values in a space of s values (where s >= k) you can use the formula

 var x = Math.floor((s - k) / 2);

and store the k values in x, x+1, ... , x+k-1.

The idea of the formula is

  1. Compute how many free places are remaining (i.e. s - k)
  2. Split these in two to give half of them to each side

of course if the number of free places ends up being odd you cannot split them exactly in half, and one side will get more than the other. Using Math.floor takes care of this adjustment giving the first side one element less.

6502
  • 112,025
  • 15
  • 165
  • 265
0

You just need to find the center (height/2) and subtract HALF the height of the "exit". so if the exit is 4 tall, then 2 should be on one side and 2 on the other, so you subtract 2 from the center.

var height = 7;
var width = 9;
var exit = 3; // or can be 2, or 4, 5 etc.

var matrix = new Array(height); 
for (var i=0; i<height; i++) { 
    matrix[i] = new Array(width); 
    for (var j = 0; j<width; j++) { 
        matrix[i][j] = '*'; 
    } 
}

function startPoint(height, matrix){
    // Se define el punto de partida
    var startPoint = Math.round(height/2)-1 ;
    document.body.innerHTML+=("el punto de partida es: "+startPoint+'<br>')
    matrix[startPoint][0] = 'S';
    return matrix
}

function finishPoint(exit, matrix){
    // punto de salida.
    var startPoint=Math.round(height/2-(exit/2))
    for(var i=0;i<exit;i++)
      matrix[startPoint+i][width-1]='0'
    return matrix
}

matrix=startPoint(height,matrix)
matrix=finishPoint(exit,matrix)

for(var i=0;i<height;i++){
  for(var j=0;j<width;j++)
     document.body.innerHTML+=matrix[i][j]
  document.body.innerHTML+='<br>'
}

document.body.innerHTML+='la salida es de altura: '+exit+ ' entonces buscamos el centro ('+(height/2)+') y restamos mitad de la altura ('+exit/2+')'
AwokeKnowing
  • 7,728
  • 9
  • 36
  • 47