Adding Nodes to Binary Search Tree recursively

Hey there, I was working on writing an add method to the BST in the FCC challenge here:

My code is here:

var displayTree = (tree) => console.log(JSON.stringify(tree, null, 2));
function Node(value) {
    this.value = value;
    this.left = null;
    this.right = null;
function BinarySearchTree() {
    this.root = null;
    // change code below this line
    this.add = function(insertElem){
    	let currNode = this.root;

    	var recurInsert = function(elem, node){
    		if(node == null){
    			node = new Node(elem);
    			return undefined;
    		else if(elem == node.value){
    			return null
    		else if(elem > node.value){
    			recurInsert(elem, node.right);
    			recurInsert(elem, node.left);

    	recurInsert(insertElem, currNode);
    // change code above this line

I get an error saying: “The add method adds elements according to the binary search tree rules.”

Can someone tell me where I went wrong?

If anyone was wondering what went wrong, I figured out that it was because I technically passing a primitive value into my recurInsert with node.right and node.left, instead of an actual whole object. Since JS is pass by reference for all primitive values, it didn’t change the object properties.

Are you sure your fixed code works? Sure, it might pass the test but I suspect it will produce incorrect result.
Could you post your fixed code?

P.S: JS doesn’t do pass by reference for all primitive types, I think you meant the opposite.

Yea sorry I wrote that completely wrong, it’s pass by value for primitive types, pass by reference for objects.

So my updated code for the add method is here:

this.add = function(value){

    	function check(node, direction) {
            if (node[direction] === null) {
                node[direction] = new Node(value);
                console.log('new node', value);
            if (node[direction].value === value) {
                console.log('equal value', value);
            if (node[direction].value > value) {
                console.log('go left', node[direction].value);
                check(node[direction], 'left');
            if (node[direction].value < value) {
                console.log('go right', node[direction].value);
                check(node[direction], 'right');

        check(this, 'root');

Your code has been blurred out to avoid spoiling a full working solution for other campers who may not yet want to see a complete solution. In the future, if you post a full passing solution to a challenge and have questions about it, please surround it with [spoiler] and [/spoiler] tags on the line above and below your solution code.

Thank you.

Will do! Sorry about that