mirror of
https://github.com/theoludwig/programming-challenges.git
synced 2024-12-08 00:45:29 +01:00
✨ Add "sort-array-number" challenge
This commit is contained in:
parent
b06df550bb
commit
888940a04c
11
challenges/sort-array-number/README.md
Normal file
11
challenges/sort-array-number/README.md
Normal 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.
|
14
challenges/sort-array-number/input-output.json
Normal file
14
challenges/sort-array-number/input-output.json
Normal 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]
|
||||
}
|
||||
]
|
0
challenges/sort-array-number/solutions/.gitkeep
Normal file
0
challenges/sort-array-number/solutions/.gitkeep
Normal file
@ -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
|
Loading…
Reference in New Issue
Block a user