SHARE
TWEET

Untitled

a guest Oct 23rd, 2019 84 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. //Matriz en espiral
  3. function m() {
  4.   var sizeElem = document.getElementById("size");
  5.   var dimension = sizeElem.value;
  6.   var i, j;
  7.  
  8.   var matriz = [];
  9.   for (i = 0; i < dimension; i++) {
  10.     matriz[i] = [];
  11.     for (j = 0; j < dimension; j++) {
  12.       matriz[i][j] = 0;
  13.     }
  14.   }
  15.  
  16.   var n = dimension * dimension;
  17.   for (i = 0; i < dimension; i++) {
  18.     for (j = dimension - 1 - i; j >= i; j--) {
  19.       matriz[i][j] = n--;
  20.     }
  21.     for (j = i + 1; j < dimension - i; j++) {
  22.       matriz[j][i] = n--;
  23.     }
  24.     for (j = i + 1; j < dimension - i; j++) {
  25.       matriz[dimension - 1 - i][j] = n--;
  26.     }
  27.     for (j = dimension - 2 - i; j >= i + 1; j--) {
  28.       matriz[j][dimension - 1 - i] = n--;
  29.     }
  30.   }
  31.  
  32.   var espiralElem = document.getElementById("espiral");
  33.   var tabla, linea;
  34.  
  35.   tabla = "<table>";
  36.   for (i = 0; i < dimension; i++) {
  37.     linea = "<tr>";
  38.     for (j = 0; j < dimension; j++) {
  39.       linea += "<td>" + matriz[i][j] + "</td>";
  40.     }
  41.     linea += "</tr>";
  42.     tabla += linea;
  43.   }
  44.   tabla += "</table>";
  45.   espiralElem.innerHTML = tabla;
  46. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top