Ad

Improve readability

Code
Diff
  • interface ListOfBoxes {
      code: string
      bomb?: boolean
      boxes?: ListOfBoxes[]
    }
    
    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
              }
          }
      }
      
      return null
    }
    
    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);
    • interface ListOfBoxes {
    • code: string;
    • bomb?: boolean;
    • boxes?: ListOfBoxes[];
    • code: string
    • bomb?: boolean
    • boxes?: ListOfBoxes[]
    • }
    • export function findTheBomb (listOfBoxes: ListOfBoxes [], path: string[] = []): string | null {
    • // You Can Code Below Here
    • if(listOfBoxes.length > 0){
    • 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;
    • }
    • }
    • }
    • return null;
    • }else{
    • 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
    • }
    • }
    • }
    • return null
    • }
    • 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);