Respuesta 8 #27

Open
opened 2022-03-28 19:16:45 +02:00 by ghost · 0 comments
ghost commented 2022-03-28 19:16:45 +02:00 (Migrated from github.com)

Mi solución con splice:

const listOfInstruments = ["JavaScript", "Clojure", "CCS", "HTML", "Python"];

function moverElement(oldIndexElement, newIndexElement, listOfInstruments) {
    const element = listOfInstruments[oldIndexElement];
    listOfInstruments.splice(oldIndexElement, 1);
    listOfInstruments.splice(newIndexElement, 0, element);
    return listOfInstruments;
}

console.info(moverElement(4, 0, listOfInstruments));
// [ 'Python', 'JavaScript', 'Clojure', 'CCS', 'HTML' ]
console.info(moverElement(2, 4, listOfInstruments));
// [ 'JavaScript', 'Clojure', 'HTML', 'Python', 'CCS' ]
console.info(moverElement(0, 3, listOfInstruments));
// [ 'Clojure', 'CCS', 'HTML', 'JavaScript', 'Python' ]
Mi solución con splice: ``` const listOfInstruments = ["JavaScript", "Clojure", "CCS", "HTML", "Python"]; function moverElement(oldIndexElement, newIndexElement, listOfInstruments) { const element = listOfInstruments[oldIndexElement]; listOfInstruments.splice(oldIndexElement, 1); listOfInstruments.splice(newIndexElement, 0, element); return listOfInstruments; } console.info(moverElement(4, 0, listOfInstruments)); // [ 'Python', 'JavaScript', 'Clojure', 'CCS', 'HTML' ] console.info(moverElement(2, 4, listOfInstruments)); // [ 'JavaScript', 'Clojure', 'HTML', 'Python', 'CCS' ] console.info(moverElement(0, 3, listOfInstruments)); // [ 'Clojure', 'CCS', 'HTML', 'JavaScript', 'Python' ] ```
Sign in to join this conversation.