1
1
mirror of https://github.com/theoludwig/programming-challenges.git synced 2024-10-29 22:17:23 +01: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,11 @@
# sort-array-number
Created by [@Divlo](https://github.com/Divlo) at 5 July 2020.
## Instructions :
Write a function that should sort the numbers of an array in ascending order.
## Examples :
See the `input-output.json` file for examples of input/output.

View File

@ -0,0 +1,14 @@
[
{
"input": [[4, 12, 1, 8]],
"output": [1, 4, 8, 12]
},
{
"input": [[6, 2, 9, 3, 4]],
"output": [2, 3, 4, 6, 9]
},
{
"input": [[2, 8, 5, 3, 9, 4, 1]],
"output": [1, 2, 3, 4, 5, 8, 9]
}
]

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