Back to Freecodecamp

Challenge 75: Hidden Treasure

curriculum/challenges/english/blocks/daily-coding-challenges-javascript/68d2ba1468508398389487d0.md

latest2.7 KB
Original Source

--description--

Given a 2D array representing a map of the ocean floor that includes a hidden treasure, and an array with the coordinates ([row, column]) for the next dive of your treasure search, return "Empty", "Found", or "Recovered" using the following rules:

  • The given 2D array will contain exactly one unrecovered treasure, which will occupy multiple cells.

  • Each cell in the 2D array will contain one of the following values:

    • "-": No treasure.
    • "O": A part of the treasure that has not been found.
    • "X": A part of the treasure that has already been found.
  • If the dive location has no treasure, return "Empty".

  • If the dive location finds treasure, but at least one other part of the treasure remains unfound, return "Found".

  • If the dive location finds the last unfound part of the treasure, return "Recovered".

For example, given:

json
[
  [ "-", "X"],
  [ "-", "X"],
  [ "-", "O"]
]

And [2, 1] for the coordinates of the dive location, return "Recovered" because the dive found the last unfound part of the treasure.

--hints--

dive([[ "-", "X"], [ "-", "X"], [ "-", "O"]], [2, 1]) should return "Recovered".

js
assert.equal(dive([[ "-", "X"], [ "-", "X"], [ "-", "O"]], [2, 1]), "Recovered");

dive([[ "-", "X"], [ "-", "X"], [ "-", "O"]], [2, 0]) should return "Empty".

js
assert.equal(dive([[ "-", "X"], [ "-", "X"], [ "-", "O"]], [2, 0]), "Empty");

dive([[ "-", "X"], [ "-", "O"], [ "-", "O"]], [1, 1]) should return "Found".

js
assert.equal(dive([[ "-", "X"], [ "-", "O"], [ "-", "O"]], [1, 1]), "Found");

dive([[ "-", "-", "-"], [ "X", "O", "X"], [ "-", "-", "-"]], [1, 2]) should return "Found".

js
assert.equal(dive([[ "-", "-", "-"], [ "X", "O", "X"], [ "-", "-", "-"]], [1, 2]), "Found");

dive([[ "-", "-", "-"], [ "-", "-", "-"], [ "O", "X", "X"]], [2, 0]) should return "Recovered".

js
assert.equal(dive([[ "-", "-", "-"], [ "-", "-", "-"], [ "O", "X", "X"]], [2, 0]), "Recovered");

dive([[ "-", "-", "-"], [ "-", "-", "-"], [ "O", "X", "X"]], [1, 2]) should return "Empty".

js
assert.equal(dive([[ "-", "-", "-"], [ "-", "-", "-"], [ "O", "X", "X"]], [1, 2]), "Empty");

--seed--

--seed-contents--

js
function dive(map, coordinates) {

  return map;
}

--solutions--

js
function dive(map, coordinates) {
  const [row, col] = coordinates;
  const target = map[row][col];

  if (target === "-") {
    return "Empty";
  }

  if (target === "O") {
    for (let r = 0; r < map.length; r++) {
      for (let c = 0; c < map[r].length; c++) {
        if ((r !== row || c !== col) && map[r][c] === "O") {
          return "Found";
        }
      }
    }
    return "Recovered";
  }

  return "Found";
}