No generar números mágicos: crea constantes que describan el porqué de ese numero.
//¿Por qué 18?
if(adrian > 18) console.log('es mayor de edad')
//Ya sabes porqué
const CUMPLE_MAYORIA_DE_EDAD = 18
if(adrian > CUMPLE_MAYORIA_DE_EDAD) console.log('es mayor de edad')
const numeroRandom = Math.random()
document.write(`<h2>${numeroRandom}</h2>`)
🌌 Copias de objetos y Arrays
//retornar una copia del array editada
//... sin editar el array fuente, u objeto fuente
const aMeses = persona => ({
//crea la copia
...persona,
//cambia el valor de la copia
edad: persona.edad * 12
})
var personasMeses = personas.map(aMeses)
🐳 Comparador de arrays
//this function takes the entire array from the game, and compare it with
//..the array lines part by part, the game is tic tac toe
function calculateWinner(squares) {
const lines = [
[0, 1, 2], //horizontales
[3, 4, 5],
[6, 7, 8],
[0, 3, 6], //verticales
[1, 4, 7],
[2, 6, 8],
[0, 4, 8], //cruzadas
[6, 4, 2]
];
//check one by one each line to see if there is a similar sequence as lines
for (let i = 0; i < lines.length; i++) {
const [a, b, c] = lines[i]; //put the first line of the winner's ones
if (squares[a] && squares[a] === squares[b] && squares[a] === squares[c]) {
// if a, b and c are the same char, then send that char, cuz' it won
return squares[a];
}
}
//if not, send null
return null;
}
//put this function on the global scope
//you can use this function in every single component