#Challenge 25

1 messages · Page 1 of 1 (latest)

olive smelt
gray harbor
#

Mi solución al último reto del calendario. Muchas gracias a @zinc nymph por hacerlo posible 🙂

restive kettle
#

#25, súper divertido el AdventJS 🔥

gray harbor
gilded python
#

aparcao 😎
Pase un buen rato con los retos

gilded python
restive kettle
restive kettle
hot moss
#

Reto 25! Muchas gracias @zinc nymph por llevar este proyecto adelante!

clear root
#

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

swift zealot
#

Mi última solución y hasta el año que viene 😬 Gracias @zinc nymph por la iniciativa

primal swift
#

No encontré otra manera de hacer esto :,v

normal lodge
#

finalizado el dia 25 y finalizados todos los retos 😄

frigid pivot
#

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:

restive kettle
# frigid pivot ||const canMouseEat = (direction, game) => { let x = 0; let y = 0; ...

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] === '*'
frigid pivot
#

creí que iba por el lado de los valores undefined, no conocía el operadoor optional chaining

#

Muchas gracias!!

slim coral
#

Nos vemos el proximo anio.

wooden surge
agile parrot
#

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
}

hushed wedge