queensAttack.js

    0

    0

    The Queens attack function takes in three parameters: a number, a king's rank, and a queen's rank. It then calculates the coordinates of the two queens, based off of the supplied numbers. It then places these coordinates into a for loop, which iterates through all of the obstacles in the game. The code then keeps track of the two queens' coordinates, and calculates their combined coordinates. It then returns the coordinates of the right, left, up, and down squares that the queens attack.

    // Complete the queensAttack function below.
    function queensAttack(n, k, r_q, c_q, obstacles) {
      let up = n - r_q;
      let right = n - c_q;
      let down = r_q - 1;
      let left = c_q - 1;
    
      let up_left = Math.min(up, left);
      let up_right = n - Math.max(c_q, r_q);
      let down_left = Math.min(c_q, r_q) - 1;
      let down_right = Math.min(r_q - 1, n - c_q);
    
      for (let i of Array.from({ length: k }, (value, index) => index)) {
        let { 0: r_o, 1: c_o } = obstacles[i];
    
        r_o == r_q &&
          (c_o > c_q
            ? (up = Math.min(up, c_o - c_q - 1))
            : (down = Math.min(down, c_q - c_o - 1)));
    
        c_o == c_q &&
          (r_o > r_q
            ? (right = Math.min(right, r_o - r_q - 1))
            : (left = Math.min(left, r_q - r_o - 1)));
    
        Math.abs(c_o - c_q) == Math.abs(r_o - r_q) &&
          (c_o > c_q && r_o > r_q && (up_right = Math.min(up_right, c_o - c_q - 1)),
          c_o > c_q &&
            r_o < r_q &&
            (down_right = Math.min(down_right, c_o - c_q - 1)),
          c_o < c_q && r_o > r_q && (up_left = Math.min(up_left, c_q - c_o - 1)),
          c_o < c_q &&
            r_o < r_q &&
            (down_left = Math.min(down_left, c_q - c_o - 1)));
      }
    
      return right + left + up + down + down_left + up_left + down_right + up_right;
    }
    
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Playground
    • Snippets
    • Cookbooks
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.