Ad

Refactored the code for better readability, consistency and modularity:

  • Changed the behaviour of findTheBomb() to throw error when called with an empty array, as in the original request. (Returning a string as error message made it hard to asses the result of the function programmatically.)
  • Changed the behaviour of findTheBomb() to return empty array when no bomb is found. This is to make the return value more predictable.
  • Introduced the box() and withBomb() functions to make the test cases (and debugging) easier to construct and more readable.
  • Abstracted the main function into more generic functions to help readability, reusability and testing.
  • Simplified the names for better readability.
Code
Diff
  • export interface Box {
      code: string
      bomb?: boolean
      boxes?: Box[]
    }
    
    
    /**
     * Creates a box with optional boxes inside
     */
    export function box(code: string, boxes?: Box[]): Box {
      return boxes ? { code, boxes } : { code }
    }
    
    
    /**
     * Returns a copy of a box with bomb
     */
    export function withBomb(box: Box) {
      return { ...box, bomb: true }
    }
    
    
    /**
     * Finds the bomb in a box
     */
    export function pathToBomb(box: Box, path: string[] = []): string[] {
      const newPath = [...path, box.code]
      
      if (box.bomb) return newPath
      if (!Array.isArray(box.boxes)) return []
      return traverseBoxes(box.boxes, newPath)
    }
    
    
    /**
     * Finds the bomb in an array of boxes
     */
    export function traverseBoxes(boxes: Box[], path: string[] = []): string[] {
      return boxes.reduce((result: string[], box) => (
        result.length > 0 ? result : pathToBomb(box, path)
      ), [])
    }
    
    
    /**
     * The main function
     */
    export function findTheBomb(boxes: Box[]): string {
      if (boxes.length === 0) throw new Error('Empty array')
      return traverseBoxes(boxes).join(' > ')
    }
    
    • interface ListOfBoxes {
    • export interface Box {
    • code: string
    • bomb?: boolean
    • boxes?: ListOfBoxes[]
    • boxes?: Box[]
    • }
    • export function findTheBomb (listOfBoxes: ListOfBoxes [], path: string[] = []): string | null {
    • if (!listOfBoxes.length) {
    • return "No Boxes No Bomb!"
    • }
    • for (const box of listOfBoxes) {
    • const newPath = [...path, box.code]
    • if (box.bomb) {
    • return newPath.join(" > ")
    • }
    • if (box.boxes) {
    • const found = findTheBomb(box.boxes, newPath)
    • if (found) {
    • return found
    • }
    • }
    • }
    • /**
    • * Creates a box with optional boxes inside
    • */
    • export function box(code: string, boxes?: Box[]): Box {
    • return boxes ? { code, boxes } : { code }
    • }
    • /**
    • * Returns a copy of a box with bomb
    • */
    • export function withBomb(box: Box) {
    • return { ...box, bomb: true }
    • }
    • /**
    • * Finds the bomb in a box
    • */
    • export function pathToBomb(box: Box, path: string[] = []): string[] {
    • const newPath = [...path, box.code]
    • return null
    • if (box.bomb) return newPath
    • if (!Array.isArray(box.boxes)) return []
    • return traverseBoxes(box.boxes, newPath)
    • }
    • const listBoxes1: ListOfBoxes [] = [
    • {
    • code: "B1",
    • boxes: [
    • {
    • code: "B1.1"
    • },
    • {
    • code: "B1.2"
    • }
    • ]
    • },
    • {
    • code: "B2",
    • boxes: [
    • {
    • code: "B2.1",
    • boxes: [
    • {
    • code: "B2.1.1"
    • },
    • {
    • code: "B2.1.2",
    • bomb: true
    • }
    • ]
    • },
    • {
    • code: "B2.2"
    • }
    • ]
    • }
    • ]
    • const listBoxes2: ListOfBoxes [] = [
    • {
    • "code": "B1.2",
    • "boxes": [
    • {
    • "code": "B1.2.2",
    • "boxes": [
    • {
    • "code": "B1.2.2.6"
    • }
    • ]
    • },
    • {
    • "code": "B1.2.7"
    • },
    • {
    • "code": "B1.2.4"
    • },
    • {
    • "code": "B1.2.0"
    • },
    • {
    • "code": "B1.2.9",
    • "boxes": [
    • {
    • "code": "B1.2.9.0",
    • "bomb": true,
    • }
    • ]
    • }
    • ]
    • },
    • {
    • "code": "B2.8",
    • "boxes": [
    • {
    • "code": "B2.8.0"
    • },
    • {
    • "code": "B2.8.7"
    • },
    • {
    • "code": "B2.8.6"
    • },
    • {
    • "code": "B2.8.3"
    • }
    • ]
    • },
    • {
    • "code": "B3.6",
    • "boxes": [
    • {
    • "code": "B3.6.5"
    • },
    • {
    • "code": "B3.6.7",
    • "boxes": [
    • {
    • "code": "B3.6.7.9"
    • }
    • ]
    • }
    • ]
    • }
    • ]
    • const listBoxes3: ListOfBoxes [] = [
    • {
    • "code": "B1.6",
    • "boxes": [
    • {
    • "code": "B1.6.9"
    • },
    • {
    • "code": "B1.6.2"
    • }
    • ]
    • },
    • {
    • "code": "B2.4",
    • "boxes": [
    • {
    • "code": "B2.4.0"
    • },
    • {
    • "code": "B2.4.3",
    • "boxes": [
    • {
    • "code": "B2.4.3.9",
    • "bomb": true
    • }
    • ]
    • },
    • {
    • "code": "B2.4.7"
    • }
    • ]
    • }
    • ]
    • findTheBomb([])
    • findTheBomb(listBoxes1);
    • findTheBomb(listBoxes2);
    • findTheBomb(listBoxes3);
    • /**
    • * Finds the bomb in an array of boxes
    • */
    • export function traverseBoxes(boxes: Box[], path: string[] = []): string[] {
    • return boxes.reduce((result: string[], box) => (
    • result.length > 0 ? result : pathToBomb(box, path)
    • ), [])
    • }
    • /**
    • * The main function
    • */
    • export function findTheBomb(boxes: Box[]): string {
    • if (boxes.length === 0) throw new Error('Empty array')
    • return traverseBoxes(boxes).join(' > ')
    • }