quiero usar un ciclo for para encontrar el número más pequeño en una matriz 50 veces, luego eliminarlo con splice(). Por fin quiero terminar con una lista ascendente de números. El problema que tengo es que mi programa solo encuentra el número más pequeño para la primera matriz y no la actualizada.
array=[] for(i=0; i<50; i++) { array[i]=parseInt(Math.random()*100+1); } min = Math.min(...array) minindex = array.indexOf(min); splice = array.splice(minindex, 1) console.log(splice)¿Quizás algo como esto?
const array = []; //create array for (let i = 0; i < 100; i++) { array.push(Math.floor(Math.random() * 100)); } for (let i = 0; i < 50; i++) { array.splice(array.indexOf(Math.min.apply(Math, array)), 1) } array.sort() console.log(array) O más simple, use array.sort() al principio
const array = []; //create array for (let i = 0; i < 100; i++) { array.push(Math.floor(Math.random() * 100)); } array.sort(); for (let i = 0; i < 50; i++) { array.splice(0, 1) } console.log(array)Un enfoque sencillo...
const array = Array .from( { length: 50 }, () => parseInt(Math.random() * 100 + 1), ) .sort((a, b) => a - b); // mutate/shorten `array`, remove all values equal to the minimum value. array.splice(0, array.lastIndexOf(array[0]) + 1);Prueba de la implementación anterior...
const array = Array .from( { length: 50 }, () => parseInt(Math.random() * 100 + 1), ) .sort((a, b) => a - b); console.log({ arrayLength: array.length, minValue: array[0], sorted: array, }); // mutate/shorten `array`, remove all values equal to the minimum value. array.splice(0, array.lastIndexOf(array[0]) + 1); console.log({ arrayLength: array.length, shortened: array, }); .as-console-wrapper { min-height: 100%!important; top: 0; } Tareas similares que mutan una matriz eliminando/cortando elementos de ella de forma activa podrían basarse en un enfoque más genérico basado en el reject ...
function reject(targetArray, condition, target) { const rejectedItemList = []; let idx = targetArray.length; const copy = [...targetArray]; // Processing the array from RIGHT to LEFT keeps the `idx` always in sync // with both related array items, the one of the mutated and also the one // of the non mutated version of the processed target array reference. // Thus the `condition` always gets passed the non mutated shallow copy. while (idx) { if ( // take *sparse array* into account. targetArray.hasOwnProperty(--idx) && // [item, idx, copy] called within `target` context. condition.call((target ?? null), copy[idx], idx, copy) ) { // keep filling the list of rejected items // FROM its LEFT side while mutating the target array. rejectedItemList.unshift(targetArray.splice(idx, 1)[0]); } } // returns an array of rejected items // but not the processed and mutated // target array reference. return rejectedItemList; } const array = Array .from( { length: 50 }, () => parseInt(Math.random() * 100 + 1), ); const minValue = Math.min(...array); console.log({ arrayLength: array.length, minValue, array, }); console.log({ rejectedItems: reject(array, value => value === minValue), arrayLength: array.length, sorted: array.sort((a, b) => a - b), }); .as-console-wrapper { min-height: 100%!important; top: 0; }