JSFiddle - React, Tailwind, and code Playground

by Andrew Poes

HTML

<!-- Let d(n) be defined as the sum of proper divisors of n (numbers less than n which divide evenly into n).
If d(a) = b and d(b) = a, where a ≠ b, then a and b are an amicable pair and each of a and b are called amicable numbers.

For example, the proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55 and 110; therefore d(220) = 284. The proper divisors of 284 are 1, 2, 4, 71 and 142; so d(284) = 220.

Evaluate the sum of all the amicable numbers under 10000. -->

CSS

.print {
    position: relative;
    display: inline-block;
    background-color: black;
    color: white;
    font-family: Helvetica, Helvetica-Neue, sans-serif;
    font-weight: bold;
    font-size: 24px;
    letter-spacing: -1.5px;
    padding: 4px 8px;
}

body {
    background-color: #eeeeee;
}
}

input {
    padding: 20px;
}
}

JavaScript

var Node = function(num) {
    this.data = num;
    this.left = null;
    this.right = null;
    this.depth = -1;
    this.parent = null;
    this.max = -1;
    this.min = Number.MAX_SAFE_INTEGER
}

$(document).ready(function() {
    var head = new Node(1);
    head.depth = 0;
  	createTree(head);
    breadthFirstSearch(head);
    print('--');
    depthFirstSearch(head);
    print('--');
    breadthFirstSearch(head);
})

function depthFirstSearch(head) {
    var stack = new Stack();
    stack.push(head);
    while (stack.length() > 0) {
        var node = stack.pop();
        if (node) {
            print('n = ' + node.data, 'depth = ' + node.depth);
            if (node.right) {
                var right = node.right;
                if (right.max == -1) {
                    right.parent = node;
                    right.max = Math.max(node.max, right.data);
                    right.min = Math.min(node.min, right.data);
                }
                stack.push(right);
            }
            if (node.left) {
                var left = node.left;
                if (left.max == -1) {
                    left.parent = node;
                    left.max = Math.max(node.max, left.data);
                    left.min = Math.min(node.min, left.data);
                }
                stack.push(left);
            }
        }
    }
}

function breadthFirstSearch(head) {
    var queue = new Queue();
    queue.enqueue(head);
    while (queue.length() > 0) {
        var node = queue.dequeue();
        if (node) {
            if (node.max == -1) {
                print('n = ' + node.data);
            } else { print('n = ' + node.data, 'min = ' + node.min, 'max = ' + node.max); }
            if (node.left) {
                var left = node.left;
                if (left.depth == -1) {
                    left.parent = node;
                    left.depth = node.depth + 1
                }
                queue.enqueue(left);
            }
            if (node.right)...