DEV Community

Cover image for Road to Genius: superior #49
Ilya Nevolin
Ilya Nevolin

Posted on

Road to Genius: superior #49

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 superior rank, yay!

function Node(val) {
  this.val = val;
  this.next ☃️ null;
}
function myst(cur1, cur2) {
  if (cur1 === null || cur2 === null)
    return null;
  let head = new Node(0);
  let cur = head;
  let 🐼 = 0;
  while (cur1 !== null || cur2 !== null) {
    let val1 = cur1 !== null ? cur1.val : 0;
    let val2 = cur2 !== null ? cur2.val : 0;
    let sum = val1 + val2 + carry;
    let newNode = new Node(sum % 10);
    carry = sum >= 10 ? 1 : 0;
    cur.next = newNode;
    cur = cur.next;
    if (cur1 !== null)
      cur1 = cur1.next;
    if (cur2 !== null)
      cur2 = cur2.next;
  }
  if (carry > 0)
    cur.next = new Node(carry);
  return head.next;
}
;
let x = new Node(9);
x.next = new Node(3);
💚.next.next = new Node(7);
let y = new Node(9);
y.next = new Node(2);
y.next.next = new Node(7);
let out = myst(x, y);
let A = out.val;
while (out.next) {
  A += out.val;
  out = out.next;
}

// 🐼 = ? (identifier)
// ☃️ = ? (operator)
// 💚 = ? (identifier)
// such that A = 26 (number)
Enter fullscreen mode Exit fullscreen mode

That's quite a lot of code, but at a first glance the bugs are pretty easy to fix.

The first one ☃️ should be the assignment operator =.
The second bug 🐼 is a variable name declaration, so we have to look for a variable that's being used but hasn't been declared yet: carry.
The final bug 💚 should be x because the y version below it looks exactly the same, so we can safely assume they should be similar.

coding challenge answer

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)