Files
freeCodeCamp/curriculum/challenges/spanish/08-coding-interview-prep/rosetta-code/department-numbers.spanish.md
2018-10-08 13:34:43 -04:00

3.1 KiB

title, id, localeTitle, challengeType
title id localeTitle challengeType
Department Numbers 59f40b17e79dbf1ab720ed7a 59f40b17e79dbf1ab720ed7a 5

Description

Hay una ciudad altamente organizada que ha decidido asignar un número a cada uno de sus departamentos:

Policía del departamento Saneamiento departamento de Cuerpo de bomberos

Cada departamento puede tener un número entre 1 y 7 (inclusive).

Los tres números de departamento deben ser únicos (diferentes entre sí) y deben sumar hasta el número 12.

Al jefe de la policía no le gustan los números impares y quiere tener un número par para su departamento.

Tarea:

Escriba un programa que produzca todas las combinaciones válidas:

[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]

Instructions

Tests

tests:
  - text: <code>combinations</code> deben ser una función.
    testString: 'assert(typeof combinations === "function", "<code>combinations</code> should be a function.");'
  - text: &#39; <code>combinations([1, 2, 3], 6)</code> deben devolver un Array.&#39;
    testString: 'assert(Array.isArray(combinations([1, 2, 3], 6)), "<code>combinations([1, 2, 3], 6)</code> should return an Array.");'
  - text: &#39; <code>combinations([1, 2, 3, 4, 5, 6, 7], 12)</code> deben devolver una matriz de longitud 14.&#39;
    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: &#39; <code>combinations([1, 2, 3, 4, 5, 6, 7], 12)</code> deben devolver todas las combinaciones válidas.&#39;
    testString: 'assert.deepEqual(combinations(nums, total), result, "<code>combinations([1, 2, 3, 4, 5, 6, 7], 12)</code> should return all valid combinations.");'

Challenge Seed

function combinations (possibleNumbers, total) {
  // Good luck!
  return true;
}

After Test

console.info('after the test');

Solution

function combinations (possibleNumbers, total) {
  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;
}