2018-09-30 22:01:58 +00:00
|
|
|
---
|
|
|
|
title: Department Numbers
|
|
|
|
id: 59f40b17e79dbf1ab720ed7a
|
|
|
|
challengeType: 5
|
|
|
|
---
|
|
|
|
|
|
|
|
## Description
|
|
|
|
<section id='description'>
|
2019-03-01 08:10:50 +00:00
|
|
|
There is a highly organized city that has decided to assign a number to each of their departments:
|
|
|
|
<ul>
|
|
|
|
<li>Police department</li>
|
|
|
|
<li>Sanitation department</li>
|
|
|
|
<li>Fire department</li>
|
|
|
|
</ul>
|
|
|
|
Each department can have a number between 1 and 7 (inclusive).
|
|
|
|
The three department numbers are to be unique (different from each other) and must add up to the number 12.
|
|
|
|
The Chief of the Police doesn't like odd numbers and wants to have an even number for his department.
|
2018-09-30 22:01:58 +00:00
|
|
|
</section>
|
|
|
|
|
|
|
|
## Instructions
|
|
|
|
<section id='instructions'>
|
2019-05-23 04:57:59 +00:00
|
|
|
Write a program which outputs all valid combinations as an array.
|
|
|
|
|
|
|
|
```js
|
|
|
|
[2, 3, 7] [2, 4, 6] [2, 6, 4]
|
|
|
|
[2, 7, 3] [4, 1, 7] [4, 2, 6]
|
|
|
|
[4, 3, 5] [4, 5, 3] [4, 6, 2]
|
|
|
|
[4, 7, 1] [6, 1, 5] [6, 2, 4]
|
2019-03-30 14:06:46 +00:00
|
|
|
[6, 4, 2] [6, 5, 1]
|
2019-05-23 04:57:59 +00:00
|
|
|
```
|
2018-09-30 22:01:58 +00:00
|
|
|
</section>
|
|
|
|
|
|
|
|
## Tests
|
|
|
|
<section id='tests'>
|
|
|
|
|
|
|
|
```yml
|
2018-10-04 13:37:37 +00:00
|
|
|
tests:
|
|
|
|
- text: <code>combinations</code> should be a function.
|
2018-10-20 18:02:47 +00:00
|
|
|
testString: assert(typeof combinations === 'function', '<code>combinations</code> should be a function.');
|
|
|
|
- text: <code>combinations([1, 2, 3], 6)</code> should return an Array.
|
|
|
|
testString: assert(Array.isArray(combinations([1, 2, 3], 6)), '<code>combinations([1, 2, 3], 6)</code> should return an Array.');
|
|
|
|
- text: <code>combinations([1, 2, 3, 4, 5, 6, 7], 12)</code> should return an array of length 14.
|
|
|
|
testString: assert(combinations(nums, total).length === len, '<code>combinations([1, 2, 3, 4, 5, 6, 7], 12)</code> should return an array of length 14.');
|
|
|
|
- text: <code>combinations([1, 2, 3, 4, 5, 6, 7], 12)</code> should return all valid combinations.
|
|
|
|
testString: assert.deepEqual(combinations(nums, total), result, '<code>combinations([1, 2, 3, 4, 5, 6, 7], 12)</code> should return all valid combinations.');
|
2018-09-30 22:01:58 +00:00
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
## Challenge Seed
|
|
|
|
<section id='challengeSeed'>
|
|
|
|
|
|
|
|
<div id='js-seed'>
|
|
|
|
|
|
|
|
```js
|
2019-03-02 08:42:56 +00:00
|
|
|
function combinations(possibleNumbers, total) {
|
2018-09-30 22:01:58 +00:00
|
|
|
// Good luck!
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
### After Test
|
|
|
|
<div id='js-teardown'>
|
|
|
|
|
|
|
|
```js
|
2018-10-20 18:02:47 +00:00
|
|
|
const nums = [1, 2, 3, 4, 5, 6, 7];
|
|
|
|
const total = 12;
|
|
|
|
const len = 14;
|
|
|
|
const result = [
|
|
|
|
[2, 3, 7],
|
|
|
|
[2, 4, 6],
|
|
|
|
[2, 6, 4],
|
|
|
|
[2, 7, 3],
|
|
|
|
[4, 1, 7],
|
|
|
|
[4, 2, 6],
|
|
|
|
[4, 3, 5],
|
|
|
|
[4, 5, 3],
|
|
|
|
[4, 6, 2],
|
|
|
|
[4, 7, 1],
|
|
|
|
[6, 1, 5],
|
|
|
|
[6, 2, 4],
|
|
|
|
[6, 4, 2],
|
|
|
|
[6, 5, 1]
|
|
|
|
];
|
2018-09-30 22:01:58 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
## Solution
|
|
|
|
<section id='solution'>
|
|
|
|
|
|
|
|
|
|
|
|
```js
|
2019-03-02 08:42:56 +00:00
|
|
|
function combinations(possibleNumbers, total) {
|
2018-09-30 22:01:58 +00:00
|
|
|
let firstNumber;
|
|
|
|
let secondNumber;
|
|
|
|
let thridNumber;
|
|
|
|
const allCombinations = [];
|
|
|
|
|
|
|
|
for (let i = 0; i < possibleNumbers.length; i += 1) {
|
|
|
|
firstNumber = possibleNumbers[i];
|
|
|
|
|
|
|
|
if (firstNumber % 2 === 0) {
|
|
|
|
for (let j = 0; j < possibleNumbers.length; j += 1) {
|
|
|
|
secondNumber = possibleNumbers[j];
|
|
|
|
|
|
|
|
if (j !== i && firstNumber + secondNumber <= total) {
|
|
|
|
thridNumber = total - firstNumber - secondNumber;
|
|
|
|
|
|
|
|
if (thridNumber !== firstNumber && thridNumber !== secondNumber && possibleNumbers.includes(thridNumber)) {
|
|
|
|
allCombinations.push([firstNumber, secondNumber, thridNumber]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return allCombinations;
|
|
|
|
}
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
</section>
|