JS Cómo obtener el número aleatorio (inclusive min max) y presionarlo en una matriz
const MIN = 1000
const MAX = 9999
const myNumbers = [ 2355, 7235, 8135, 1762, 2361, 8351 ]
/* -------------------------------------------------------------------------- */
/**
* Returns a random number, including the maximum and minimum.
* @param {number} min
* @param {number} max
* @returns {number}
*/
const randomInRange = ( min, max ) => {
const randomNumber = Math.floor( Math.random() * ( max - min + 1 ) + min )
return randomNumber
}
/* -------------------------------------------------------------------------- */
/**
*
* @param {number} min
* @param {number} max
* @param {number[]} arr
* @returns {number[]}
*/
const addRandomNumberToArray = ( min, max, arr ) => {
/**
* @type {number}
*/
let randomNumber
const updatedArray = [ ...arr ]
// const updatedArray = arr.concat( [] )
do {
randomNumber = randomInRange( min, max )
} while ( arr.includes( randomNumber ) )
updatedArray.push( randomNumber )
return updatedArray
}
const updatedArray = addRandomNumberToArray( MIN, MAX, myNumbers )
console.log( 'ORIGINAL ARRAY', myNumbers )
console.log( 'UPDATED ARRAY', updatedArray )
Condemned Corncrake