Back to Freecodecamp

Challenge 214: Domino Chain Validator

curriculum/challenges/english/blocks/daily-coding-challenges-javascript/699c8e045ee7cb94ed2322d5.md

latest1.7 KB
Original Source

--description--

Given a 2D array representing a sequence of dominoes, determine whether it forms a valid chain.

  • Each element in the array represents a domino and will be an array of two numbers from 1 to 6, (inclusive).
  • For the chain to be valid, the second number of each domino must match the first number of the next domino.
  • The first number of the first domino and the last number of the last domino don't need to match anything.

--hints--

isValidDominoChain([[1, 3], [3, 6], [6, 5]]) should return true.

js
assert.isTrue(isValidDominoChain([[1, 3], [3, 6], [6, 5]]));

isValidDominoChain([[6, 2], [3, 4], [4, 1]]) should return false.

js
assert.isFalse(isValidDominoChain([[6, 2], [3, 4], [4, 1]]));

isValidDominoChain([[2, 5], [5, 6], [5, 1]]) should return false.

js
assert.isFalse(isValidDominoChain([[2, 5], [5, 6], [5, 1]]));

isValidDominoChain([[4, 3], [3, 1], [1, 6], [6, 6], [6, 5], [5, 1], [1, 1], [1, 4], [4, 4], [4, 2]]) should return true.

js
assert.isTrue(isValidDominoChain([[4, 3], [3, 1], [1, 6], [6, 6], [6, 5], [5, 1], [1, 1], [1, 4], [4, 4], [4, 2]]));

isValidDominoChain([[2, 3], [3, 3], [3, 6], [6, 1], [1, 4], [3, 5], [5, 5], [5, 4], [4, 2], [2, 2]]) should return false.

js
assert.isFalse(isValidDominoChain([[2, 3], [3, 3], [3, 6], [6, 1], [1, 4], [3, 5], [5, 5], [5, 4], [4, 2], [2, 2]]));

--seed--

--seed-contents--

js
function isValidDominoChain(dominoes) {

  return dominoes;
}

--solutions--

js
function isValidDominoChain(dominoes) {
  for (let i = 0; i < dominoes.length - 1; i++) {
    if (dominoes[i][1] !== dominoes[i+1][0]) return false
  }

  return true;
}