## DEV Community is a community of 640,935 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

loading...

# Daily Challenge #194 - Spread Number

### Setup

Implement a function that will create an array and fill it with numbers ranging from `1` to `n`. The numbers will always be positive.

### Examples

`spreadNumber(1)` => ``

`spreadNumber(2)` => `[1, 2]`

`spreadNumber(5)` => `[1, 2, 3, 4, 5]`

### Tests

`spreadNumber(3)`

`spreadNumber(6)`

`spreadNumber(9)`

Good luck!

This challenge comes from linisnie on CodeWars. Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!

Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!

## Discussion (29) Craig McIlwrath

Haskell, many ways

``````spreadNumber n = [1..n]
spreadNumber = enumFromTo 1
spreadNumber = flip take [1..]

-- The most flexible way
spreadEnum :: Enum a => a -> a
spreadEnum = enumFromTo \$ toEnum 1
`````` Cent

Javascript, just using a for loop

``````
const spreadNumber = (number) => {
let returnArray = [];

for (let i = 0; i < number; i++) {
returnArray.push(i+1);
}

return returnArray;
}

``````

Codepen @nobody • Edited

Clojure short and simple

``````(ns daily-challenge.one-ninety-four)

(defn spreadNumber [n]
;; precondition check to bound domain
{:pre [(> 0 n)]}

(unless (= n 1)
(vec (range 1 n))
))
``````

and some tests...

``````(deftest one-ninety-four
(is (= [1 2 3] (spreadNumber 3)))
(is (= [1 2 3 4 5 6] (spreadNumber 6)))
(is (= [1 2 3 4 5 6 7 8 9] (spreadNumber 9))))

(run-tests 'daily-challenge.one-ninety-four)

=> Testing daily-challenge.one-ninety-four

Ran 1 tests containing 3 assertions.
0 failures, 0 errors.
{:type :summary, :test 1, :pass 3, :fail 0, :error 0}
`````` Natamo • Edited

Late to the party, as always.

``````Func<int, int[]> SpreadNumber = (int n) => Enumerable.Range(1, n).ToArray();
``````

Example:

``````foreach(int i in SpreadNumber(10)) {
Console.WriteLine(i);
}
`````` Michael Kohl • Edited

Golfscript:

``````{,{1+}%}:spread;
``````

Usage:

``````5 spread
``````

Or if we don't need a function just

``````5,{1+}%
``````

Explanation: `,` takes the top of the stack and turns it into an array from 0 to n - 1. `{1+}` is a block adding 1 to each argument and `%` is the map function. The surrounding `{}` turns everything into a block which we assign (`:`) to the name `spread`. Lamonte • Edited

dart

``````List<int> spreadNumber(int n) {
var nums = List<int>();
for(var x = 1; x <= n.abs(); x++) {
nums.add(x);
}
return nums;
}
``````

lol damn, while this is one way, looking at other solutions. I forgot range was a thing.

``````List<int> spreadNumber(int n) {
return List<int>.generate(n.abs(), (n) => n + 1);
}
`````` Nijeesh Joshy

### GO

``````func main() {
x := spreadNumber(10)
fmt.Println(x)
}

func spreadNumber(n int) []int {
ar := make([]int, n)
for i := range ar {
ar[i] = i + 1
}
return ar
}
`````` Nils Andrey

Javascript

``````spreadNumber = n => Array.from(Array(n).keys()).map(x => x+1);
``````

Zero based ;)

``````spreadNumber = n => Array.from(Array(n).keys());
``````