#Challenge 25
1 messages · Page 1 of 1 (latest)
Mi solución al último reto del calendario. Muchas gracias a @zinc nymph por hacerlo posible 🙂
#25, súper divertido el AdventJS 🔥
Hala, desconocía que se pudiera resolver el problema de Array Outbounds de esa forma con el operador '?' 🤩
aparcao 😎
Pase un buen rato con los retos
se le llama optional chaining
sí, el optional chaining funciona muy bien especialmente para este reto
V2 un pelín más corta y simple:
Reto 25! Muchas gracias @zinc nymph por llevar este proyecto adelante!
Mi solución al día #25, felices fiestas a todos
PD: @agile parrot esta vez es una solución general y no una "!solución" que pasa los test jajaja
Mi última solución y hasta el año que viene 😬 Gracias @zinc nymph por la iniciativa
No encontré otra manera de hacer esto :,v
finalizado el dia 25 y finalizados todos los retos 😄
Hola necesito ayuda porfavor
creo que el código que hice pasa todas las pruebas y me marca un error en la página
😦
y no se xq será 😦
||const canMouseEat = (direction, game) => {
let x = 0;
let y = 0;
switch (direction) {
case 'up':
y = -1;
break;
case 'down':
y = 1;
break;
case 'left':
x = -1;
break;
case 'right':
x = 1;
break;
}
let currentX = 0;
let currentY = 0;
for (let i = 0; i < game.length; i++) {
for (let j = 0; j < game[i].length; j++) {
if (game[i][j] === 'm') {
currentX = j;
currentY = i;
}
}
}
return (game[currentY + y][currentX + x] === '*') ? true : false;
}||
ahí está por si alguien lo quiere probar y me pueda dar un consejo D:
El error está el el final de tu función, concretamente al hacer el return, habrá veces que game[currentY + y] valdrá undefined y por tanto al hacer [currentX + x] de undefined ese test en concreto revienta.
Para solucionar esto puedes utilizar el operador optional chaining ?. así en esos casos no peta.
Por último el operador ternario te sobra ya que es redundante, así que te quedaría esto:
return game[currentY + y]?.[currentX + x] === '*'
creí que iba por el lado de los valores undefined, no conocía el operadoor optional chaining
Muchas gracias!!
Nos vemos el proximo anio.
Solución clean code
||```js
const MOUSE = 'm'
const FOOD = '*'
const DIRECTION_OFFSETS = Object.freeze({
up : [ 0, -1 ],
down : [ 0, 1 ],
right : [ 1, 0 ],
left : [ -1, 0 ]
})
export default function canMouseEat( direction, game ) {
const [ mouseX, mouseY ] = getMouseCoordinates( game )
const [ offsetX, offsetY ] = DIRECTION_OFFSETS[ direction ]
const [ destinationX, destinationY ] = [ mouseX + offsetX, mouseY + offsetY ]
if( !areCoordinatesValid( [ destinationX, destinationY ], game ) ) return false
return game[ destinationY ][ destinationX ] === FOOD
}
function getMouseCoordinates( game ) {
for( const [ y, row ] of game.entries() )
for( const [ x, cell ] of row.entries() )
if( cell === MOUSE ) return [ x, y ]
}
function areCoordinatesValid( [ x, y ], game ) {
return x >= 0 && y >= 0 && y < game.length && x < game[ 0 ].length
}