0

I have this code:

function Tree() {
    this.capacity = 1;
    this.contents = 0;
    this.children = [];
    this.divided = false;

    this.pour = function(amount) {
        this.contents += amount;
        if (this.contents <= 1) {
            return;
        }

        if (!this.divided) {
            this.divide();
        }
        amount = this.contents - 1;
        this.contents = 1;
        for (let child in this.children) {
            this.children[child].pour(amount * .5);
        }
    }

    this.divide = function() {
        this.children = new Array(2).fill(0).map(x => new Tree());
        this.divided = true;
        return;
    }

    this.getContents = function(row, currentRow) {
        if (currentRow === undefined) {
            currentRow = 0;
        }

        if (row === currentRow) {
            console.log('Contents:', this.contents)
            return this.contents;
        }

        if (this.divided) {
            console.log(row, currentRow)
            currentRow++;
            this.children[0].getContents(row, currentRow);
        } else {
            return;
        }
    }
}

Upon creating a tree, pouring into it, and getting its contents using this:

let tree = new Tree();
tree.pour(10);
tree.getContents(1);

It should return 1 because the second rows contents is 1. It logs 1 in the console but does not return the correct value. I am curious to what is going wrong.

Edit: I looked at switching it to a class and it did not solve the problem:

class Tree {
    constructor() {
        this.capacity = 1;
        this.contents = 0;
        this.children = [];
        this.divided = false;
    }

    pour(amount) {
        this.contents += amount;
        if (this.contents <= 1) {
            return;
        }

        if (!this.divided) {
            this.divide();
        }
        amount = this.contents - 1;
        this.contents = 1;
        for (let child in this.children) {
            this.children[child].pour(amount * .5);
        }
    }

    divide() {
        this.children = new Array(2).fill(0).map(x => new Tree());
        this.divided = true;
        return;
    }

    getContents(row, currentRow) {
        if (currentRow === undefined) {
            currentRow = 0;
        }

        if (row === currentRow) {
            console.log('Contents:', this.contents)
            return this.contents;
        }

        if (this.divided) {
            console.log(row, currentRow)
            currentRow++;
            this.children[0].getContents(row, currentRow);
        } else {
            return;
        }
    }
}
4
  • 1
    I don't see any constructor considering your syntax (new Tree()) Commented Apr 4, 2018 at 0:02
  • I am confused to what this means because I thought that is what I was doing with the code. This is confusing to me because I can access the values within the properties but cannot return them. Commented Apr 4, 2018 at 0:07
  • 1
    Maybe time for a quick refresh @TheIncorrigible1 - developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented Apr 4, 2018 at 0:22
  • @Mark_M $CURRENT_YEAR, Mark_M, $CURRENT_YEAR! SHED YOUR ES1998 SKIN AND EMBRACE TRUE OOP CLASSES (/s, kind of) Commented Apr 4, 2018 at 1:00

2 Answers 2

2

The console.log you are seeing is the result of this call:

 if (this.divided) {
        console.log(row, currentRow)
        currentRow++;
        this.children[0].getContents(row, currentRow); //<-- here
        // this is calling getContents() but ignores the return value

but in that case you don't actually return anything, so the inner console.log() fires but the return value is undefined.

I'm not really sure what the code is supposed to do, but returning a value when that condition is met results in a return value for the whole function:

if (this.divided) {
        console.log(row, currentRow)
        currentRow++;
        return this.children[0].getContents(row, currentRow);
Sign up to request clarification or add additional context in comments.

2 Comments

Then how do I make it so when the row === currentRow it returns the correct result which is what I thought I was doing. imgur.com/a/yjyfF is the result while the expected is 1 instead of undefined.
It will. But currentRow doesn't equal 1 until after that if (this.divided)
0

It logs 1 to the console because you call

tree.pour(10)

Current row is undefined because you do not pass it in the argument

if (currentRow === undefined) {
        currentRow = 0;
    }

    if (row === currentRow) {
        console.log('Contents:->', this.contents)
        return this.contents;
    }

So it mean currentRow is 0 and row(1) is not equals to currentRow (0) which is why it returns undefined.

    } else {

        return;
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.