Grouping Multidimension Array

Run Settings
LanguageJavascript
Language Version
Run Command
function groupAnimals(animals) { // you can only write your code here! let temp = [ [] ]; let indeks = 0; sorting(animals) temp[0].push(animals[0]); for (let i = 1; i < animals.length; i++) { if (animals[i][0] === temp[indeks][0][0]) { temp[indeks].unshift(animals[i]); } else { indeks++; temp[indeks] = [animals[i]]; } } sorting(temp[1]) return temp; } function sorting(array) { // your code here let done = false; while (!done) { done = true; for (let i = 1; i < array.length; i++) { if (array[i - 1] > array[i]) { done = false; let tmp = array[i - 1]; array[i - 1] = array[i]; array[i] = tmp; } } } return array; } console.log(groupAnimals(['cacing', 'ayam', 'kuda', 'anoa', 'kancil'])) console.log(groupAnimals(['cacing', 'ayam', 'kuda', 'anoa', 'kancil', 'unta', 'cicak']))
Editor Settings
Theme
Key bindings
Full width
Lines