DEV Community

Cover image for Road to Genius: genius #67
Ilya Nevolin
Ilya Nevolin

Posted on

Road to Genius: genius #67

Each day I solve several coding challenges and puzzles from Codr's ranked mode. The goal is to reach genius rank, along the way I explain how I solve them. You do not need any programming background to get started, and you will learn a ton of new and interesting things as you go.

We finally made it to the genius rank, this is the very first challenge on the highest rank. I hope you're as excited as I am. I will continue doing several more daily challenges.

function helper(p1, p2) {
  const deltaX = p1[0] - p2[0];
  const 💰 = p1[1] - p2[1];
  return 🚀 * deltaX + deltaY * deltaY;
}
function checker(p1, p2, p3, p4) {
  const HP = [helper(p1, p2), helper(p1, p3), helper(p1, p4), helper(p2, p3), helper(p2, p4), helper(p3, p4)];
  let cnt1 = 0;
  let cnt2 = 0;
  let sum = 0;
  for (let i = 0; i < HP.length; i++) {
    sum += HP[i];
  }
  for (let i = 0; i < HP.length; i++) {
    if (sum === 8 * HP[💚]) {
      cnt1++;
    } else if (sum === 4 * HP[💎]) {
      cnt2++;
    }
  }
  return cnt1 === 4 && cnt2 === 2;
}
let p1 = [6, 9], p2 = [3, 1];
let p3 = [7, 0], p4 = [3, 7];
let A = checker(p1, p2, p3, p4);

// 💰 = ? (identifier)
// 💚 = ? (identifier)
// 💎 = ? (identifier)
// 🚀 = ? (identifier)
// such that A = false (boolean)
Enter fullscreen mode Exit fullscreen mode

In this challenge we need to fix four bugs, after a quick look at the code it should be an easy procedure.

The first two bugs appear within the same short function helper:

function helper(p1, p2) {
  const deltaX = p1[0] - p2[0];
  const 💰 = p1[1] - p2[1];
  return 🚀 * deltaX + deltaY * deltaY;
}
Enter fullscreen mode Exit fullscreen mode

The variable declaration for 💰 should be deltaY, and 🚀 is very likely to be deltaX because the function seems to be computing dx² + dy².

The final two bugs appear here:

  for (let i = 0; i < HP.length; i++) {
    if (sum === 8 * HP[💚]) {
      cnt1++;
    } else if (sum === 4 * HP[💎]) {
      cnt2++;
    }
  }
Enter fullscreen mode Exit fullscreen mode

Both these bugs are indices for the array HP. Since both are within a for-loop whose i variable isn't used, it seems very likely that both bugs should be i.

coding challenge answer

All our assumptions were correct! We are likely to encounter this function again in the near future, so for now we skip its detailed analysis.

By solving these challenges you train yourself to be a better programmer. You'll learn newer and better ways of analyzing, debugging and improving code. As a result you'll be more productive and valuable in business. Get started and become a certified Codr today at https://nevolin.be/codr/

Top comments (0)