mirror of
https://github.com/theoludwig/programming-challenges.git
synced 2025-05-18 12:02:53 +02:00
✨ Add "sort-array-number" challenge
This commit is contained in:
@ -0,0 +1,4 @@
|
||||
# javascript-sort - sort-array-number
|
||||
|
||||
Programming language : JavaScript
|
||||
Created by [@Divlo](https://github.com/Divlo) at 5 July 2020.
|
@ -0,0 +1,23 @@
|
||||
function minNumber (array) {
|
||||
let minNumber = { index: 0, value: array[0] }
|
||||
for (let index = 1; index < array.length; index++) {
|
||||
const number = array[index]
|
||||
if (number < minNumber.value) {
|
||||
minNumber = { index: index, value: array[index] }
|
||||
}
|
||||
}
|
||||
return minNumber
|
||||
}
|
||||
|
||||
function solution (array) {
|
||||
const arrayDuplicated = [...array]
|
||||
const resultArray = []
|
||||
while (array.length !== resultArray.length) {
|
||||
const min = minNumber(arrayDuplicated)
|
||||
resultArray.push(min.value)
|
||||
arrayDuplicated.splice(min.index, 1)
|
||||
}
|
||||
return resultArray
|
||||
}
|
||||
|
||||
module.exports = solution
|
Reference in New Issue
Block a user