Back to Freecodecamp

Challenge 223: QR Decoder

curriculum/challenges/english/blocks/daily-coding-challenges-javascript/69a890af247de743333bd4cc.md

latest3.1 KB
Original Source

--description--

Given a 6x6 matrix (array of arrays), representing a QR code, return the string of binary data in the code.

  • The QR code may be given in any rotation of 90 degree increments.
  • A correctly oriented code has a 2x2 group of 1's (orientation markers) in the bottom-left, top-left, and top-right corners.
  • The three 2x2 orientation markers are not part of the binary data.
  • The binary data is read left-to-right, top-to-bottom (like a book) when the QR code is correctly oriented.
  • A code will always have exactly one valid orientation.

For example, given:

js
[
  "110011",
  "110011",
  "000000",
  "000000",
  "110000",
  "110001"
]

or given the same code with a different orientation:

js
[
  "110011",
  "110011",
  "000000",
  "000000",
  "000011",
  "100011"
]

Return "000000000000000000000001", all the binary data excluding the three 2x2 orientation markers.

--hints--

decodeQr(["110011", "110011", "000000", "000000", "110000", "110001"]) should return "000000000000000000000001".

js
assert.equal(decodeQr(["110011", "110011", "000000", "000000", "110000", "110001"]), "000000000000000000000001");

decodeQr(["100011", "000011", "000000", "000000", "110011", "110011"]) should return "000000000000000000000001".

js
assert.equal(decodeQr(["100011", "000011", "000000", "000000", "110011", "110011"]), "000000000000000000000001");

decodeQr(["110011", "111111", "010000", "110000", "110011", "110100"]) should return "001101000011000000110100".

js
assert.equal(decodeQr(["110011", "111111", "010000", "110000", "110011", "110100"]), "001101000011000000110100");

decodeQr(["011011", "101011", "101000", "100010", "110011", "111011"]) should return "010001000100010101010110".

js
assert.equal(decodeQr(["011011", "101011", "101000", "100010", "110011", "111011"]), "010001000100010101010110");

decodeQr(["111100", "110001", "100011", "001101", "110011", "110011"]) should return "010000100100100101001110".

js
assert.equal(decodeQr(["111100", "110001", "100011", "001101", "110011", "110011"]), "010000100100100101001110");

--seed--

--seed-contents--

js
function decodeQr(qrCode) {

  return qrCode;
}

--solutions--

js
function isOriented(code) {
  const markerIndices = [
    [0, 0],
    [0, 1],
    [0, 4],
    [0, 5],
    [1, 0],
    [1, 1],
    [1, 4],
    [1, 5],
    [4, 0],
    [4, 1],
    [5, 0],
    [5, 1]
  ];

  for(const [row, col] of markerIndices) {
    if (code[row][col] !== '1') return false;
  }

  return true;
}

function rotate(code) {
  const newCode = ['', '', '', '', '', ''];
  for (let i = 0; i < 6; i++) {
    for (let j = 5; j >= 0; j--) {
      newCode[i] += code[j][i];
    }
  }
  return newCode;
}

function getData(code) {
  let data = '';
  data += code[0].substring(2, 4)
  data += code[1].substring(2, 4)
  data += code[2]
  data += code[3]
  data += code[4].substring(2)
  data += code[5].substring(2)
  return data;
}

function decodeQr(qrCode) {
  let code = [...qrCode];

  while(!isOriented(code)) {
    code = rotate(code);
  }

  return getData(code);
}