Why my for loop is not working?

I wrote the same code from the coding train video( Binary Tree - ). I checked the code few times I cant get it why the for loop is not working.. In the Core.js file. Every time I run the code instead of getting 10 output each time I always get random outputs. I tweeted the problem on Twitter to Daniel and I also linked a video there showing the problem. Here is the tweet(https://twitter.com/prasantakun/status/963151930745745409). But ill put my code here -

Sketch (1) Sketch (2) Sketch

Answers

  • Answer ✓

    please post your code as text not as images

  • Answer ✓

    While you add code as text in your post, don't forget to check https://forum.processing.org/two/discussion/15473/readme-how-to-format-code-and-text#latest

    Kf

  • edited February 2018 Answer ✓

    addNode is wrong.

    If you'd posted your code as code I could've told you which line was wrong. Because it's a picture I can't.

  • Answer ✓

    You can add console logging to the methods to see which methods are called when.

    Do they not teach people how to debug code anymore?

  • //Node.js

    function Node(val) {
      this.value = val;
      this.left = null;
      this.right = null;
    }
    
    Node.prototype.visit = function() {
      if(this.left != null) {
        this.left.visit();
      }
      console.log(this.value);
      if(this.right != null) {
        this.right.visit();
      }
    }
    
    Node.prototype.addNode = function(n) {
      if(n.value < this.value) {
        if(this.n == null) {
          this.left = n;
        } else {
        this.left.addNode(n);
        }
      } else if(n.value > this.value) {
        if(this.right == null){
          this.right = n;
        } else {
        this.right.addNode(n);
        }
      }
    }
    
  • //Core.js
    
    var tree;
    
    function setup() {
      noCanvas();
      tree = new Tree();
      for(var i = 0; i < 10; i++) {
        tree.addValue(floor(random(0,100)));
      }
      console.log(tree);
      tree.traverse();
    }
    
  • //tree.js
    
    function Tree(){
      this.root = null;
    }
    
    Tree.prototype.addValue = function(val){
      var n = new Node(val);
      if(this.root == null){
        this.root = n;
      }else{
        this.root.addNode(n);
      }
    }
    
    Tree.prototype.traverse = function() {
      this.root.visit();
    }
    
  • I can't see any attempt at debugging this

    There's what looks like an error in addNode that I can see by just looking at the code. Start there.

  • edited February 2018 Answer ✓

    https://CodePen.io/GoSubRoutine/pen/QQQWYj/right?editors=0011

    index.html:

    <script async src=https://CDN.JSDelivr.net/npm/p5></script>
    <script defer src=sketch.js></script>
    

    sketch.js:

    /**
     * Binary Tree (v2.0.2)
     * by  PrasantaKun (2018-Feb-17)
     * mod GoToLoop (2018-Feb-18)
     *
     * Forum.Processing.org/two/discussion/26409/
     * why-my-for-loop-is-not-working#Item_9
     *
     * CodePen.io/GoSubRoutine/pen/QQQWYj/right?editors=0011
     */
    
    "use strict";
    
    class Tree {
      constructor() { this.root = null; }
    
      traverse() {
        this.root && console.table(this.root.visit());
        return this
      }
    
      addValue(val) {
        const n = new Node(+val || 0);
        this.root? this.root.addNode(n) : this.root = n;
        return this;
      }
    }
    
    class Node {
      constructor(val) {
        this.value = +val || 0;
        this.left = this.right = null;
      }
    
      visit() {
        this.left  && console.table(this.left.visit());
        this.right && console.table(this.right.visit());
        return this;
      }
    
      addNode(n = new Node(~~random(MAX_VAL))) {
        if (n.value < this.value)
          this.left?  this.left.addNode(n)  : this.left  = n;
        else if (n.value > this.value)
          this.right? this.right.addNode(n) : this.right = n;
        return this;
      }
    }
    
    const NODES = 10, MAX_VAL = 1000, tree = new Tree;
    
    function setup() {
      noCanvas();
      for (let i = 0; i++ < NODES; tree.addValue(~~random(MAX_VAL)));
      console.table(tree.traverse());
    }
    
  • koogs, look you maybe a professional but I'm not a professional. I am new to coding and specially very new to JavaScript. I'm watching Daniels videos to get started. If i could figure out the problem but just looking at the code than i might have never asked the question "Whats wrong in my code". I have no idea how to debug this. I came here because I thought you guys are professional and will help me find the error and also tell me how to fix it next time by myself. But, if you just say check addNode than I cant do anything. So, please be nice to beginners and please tell me where I made the mistak, what is the mistake and how do I solve it from next time. Thank you.

  • edited February 2018 Answer ✓

    ... and please tell me where I made the mistake, ...

    Both Node::left & Node::right properties start out as null: :-B
    this.left = this.right = null;

    But while you're checking out Node::right before invoking Node::addNode() over it: :-bd

    if (this.right == null) {
      this.right = n;
    } else {
      this.right.addNode(n);
    }
    

    You haven't done the same for its Node::left property's counterpart: #-o

    if (this.n == null) {
      this.left = n;
    } else {
      this.left.addNode(n);
    }
    

    Instead you're checking out for Node::n; which doesn't exist anywhere else within your code btW! :-S

  • Oh I see now, Thanks you so much GoToLoop. Thanks for pointing out the error. ^:)^

  • You don't need to be a software professional to look at two bits of code and notice that they are different.

    I narrowed it down for you. It was 11 hours before you replied and only then to criticise.

Sign In or Register to comment.