1
1
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:
Divlo
2020-07-05 21:00:06 +02:00
parent b06df550bb
commit 888940a04c
5 changed files with 52 additions and 0 deletions

View File

@ -0,0 +1,4 @@
# javascript-sort - sort-array-number
Programming language : JavaScript
Created by [@Divlo](https://github.com/Divlo) at 5 July 2020.

View File

@ -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