Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

173
Vistas
How to insert an X in a specific location in a matrix where a user wants it?

I am trying to do something like this - OOOOOOOOOO \n OOOOOOOOOO \n OOOOOOOOOO \n OOOOOOOOOO \n OOOOOOOOOO \n OOOOOOOOOO \n OOOOOOOOOO \n OOOOOOOOOO \n OOOOOOOOOO \n OOOOOOOOOO \n where a user would input a certain row from the array, and a certain column, and an X can be placed there instead of the zero. I have tried using a switch table, but I can not think of a more efficient way. Here is the code I have so far.

var allRows2 = ["O", "O", "O", "O", "O", "O", "O", "O", "O", "O"];
var allRows3 = ["O", "O", "O", "O", "O", "O", "O", "O", "O", "O"];
var allRows4 = ["O", "O", "O", "O", "O", "O", "O", "O", "O", "O"];
var allRows5 = ["O", "O", "O", "O", "O", "O", "O", "O", "O", "O"];
var allRows6 = ["O", "O", "O", "O", "O", "O", "O", "O", "O", "O"];
var allRows7 = ["O", "O", "O", "O", "O", "O", "O", "O", "O", "O"];
var allRows8 = ["O", "O", "O", "O", "O", "O", "O", "O", "O", "O"];
var allRows9 = ["O", "O", "O", "O", "O", "O", "O", "O", "O", "O"];
var allRows10 = ["O", "O", "O", "O", "O", "O", "O", "O", "O", "O"];
var userinr = prompt("Which row would you like to put the X in?");
var userinc = prompt("Which column would you like to put your piece in?");
switch(userin) {
  case "1":
    var therow = 1;
    break;
  case "2":
    var therow = 2;
    break;
  case "3":
    var therow = 3;
    break;
  case "4":
    var therow = 4;
    break;
  case "5":
    var therow = 5;
    break;
  case "6":
    var therow = 6;
    break;
  case "7":
    var therow = 7;
    break;
} ```
I am unsure how to continue, any help would be appreciated:)
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Instead of storing each row in an individual variable, store all the rows in nested arrays. Then reference the item and position with bracket notation:

var allRows = [["O", "O", "O", "O", "O", "O", "O", "O", "O", "O"],["O", "O", "O", "O", "O", "O", "O", "O", "O", "O"],["O", "O", "O", "O", "O", "O", "O", "O", "O", "O"],["O", "O", "O", "O", "O", "O", "O", "O", "O", "O"],["O", "O", "O", "O", "O", "O", "O", "O", "O", "O"],["O", "O", "O", "O", "O", "O", "O", "O", "O", "O"],["O", "O", "O", "O", "O", "O", "O", "O", "O", "O"],["O", "O", "O", "O", "O", "O", "O", "O", "O", "O"],["O", "O", "O", "O", "O", "O", "O", "O", "O", "O"]];

const row = 2;
const position = 5;

allRows[row][position] = "X"
console.log(JSON.stringify(allRows))

If you want to print the array without commas, you can use Array.join and set the delimiter to an empty string:

var allRows = [["O", "O", "O", "O", "O", "O", "O", "O", "O", "O"],["O", "O", "O", "O", "O", "O", "O", "O", "O", "O"],["O", "O", "O", "O", "O", "O", "O", "O", "O", "O"],["O", "O", "O", "O", "O", "O", "O", "O", "O", "O"],["O", "O", "O", "O", "O", "O", "O", "O", "O", "O"],["O", "O", "O", "O", "O", "O", "O", "O", "O", "O"],["O", "O", "O", "O", "O", "O", "O", "O", "O", "O"],["O", "O", "O", "O", "O", "O", "O", "O", "O", "O"],["O", "O", "O", "O", "O", "O", "O", "O", "O", "O"]];

const row = 2;
const position = 5;

allRows[row][position] = "X"

for(const row of allRows){
  console.log(row.join(''))
}

about 4 years ago · Juan Pablo Isaza Denunciar

0

Here's my example. The comments should make it easier to unserstand.

// easiest way to make a grid.
// Array(n), makes an empty array with a length of n.
// Array.fill(v) changes all the array items to be v.
// Array.map(function) makes a new array from what is returned from the callback function.
const grid = Array(10)
  .fill([])
  .map(() => Array(10).fill("o"));

// Multidimentional arrays work in reverse. y then x. 
// grid[y][x]
grid[5][3] = "x";
console.log("Example 1")
grid.forEach(row => console.log(row.join("")))

// you can make a helper function to make it more intuitive.
function change([x, y], value){
  grid[y][x] = value;
}

// Now we can use the coordinates normally.

console.log("\nExample 2")
change([0, 5], "v")
grid.forEach(row => console.log(row.join("")))

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda