Skip to content

Commit 455e425

Browse files
committed
Length and two values.
1 parent 583752a commit 455e425

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

7_kyu/alternate.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
## [Length and two values.](https://www.codewars.com/kata/62a611067274990047f431a8/train/javascript)
2+
3+
##### Story
4+
5+
- Given an integer n and two other values, build an array of size n filled with these two values alternating.
6+
7+
#### Examples (input -> output):
8+
9+
```js
10+
5, true, false --> [true, false, true, false, true]
11+
10, "blue", "red" --> ["blue", "red", "blue", "red", "blue", "red", "blue", "red", "blue", "red"]
12+
0, "one", "two" --> []
13+
```
14+
15+
#### Solution:
16+
17+
```js
18+
function alternate(n, firstValue, secondValue){
19+
return Array.from({length: n}, (_, i) => i % 2 === 0 ? firstValue : secondValue);
20+
}
21+
22+
23+
console.log(alternate(5, true, false)); // [true, false, true, false, true]
24+
console.log(alternate(10, "blue", "red")); // ["blue", "red", "blue", "red", "blue", "red", "blue", "red", "blue", "red"]
25+
console.log(alternate(0, "one", "two")); // []
26+
```

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ Solutions to CodeWars programming tasks. I try to update every day
387387
</tr>
388388
<tr>
389389
<td><a href=" " > </a></td>
390-
<td><a href="7 kyu" > </a></td>
390+
<td><a href="https://github.com/esadakman/javaScript-coding-challenges/blob/master/7_kyu/alternate.md" >alternate</a></td>
391391
<td><a href="https://github.com/esadakman/javaScript-coding-challenges/blob/master/6_kyu/simpleFrequencySort.md" >simpleFrequencySort</a></td>
392392
<td><a href="5 kyu" > </a></td>
393393
<td><a href="others " ></a></td>

0 commit comments

Comments
 (0)