Further Help on Adding bouncing balls features

My code did work but I need further help.

  1. :slightly_smiling_face:
    if (distance < this.size + balls[j].size) {
    balls[j][‘exists’] = false;
    // ---- reduce count -----
    count–;
    para.textContent = Alive balls: ${count};
    }
    Concerning the count part, why can’t I just put my paragraph content at the end of my code(like I did in my codepen tho I later turned it into comment) instead of having it inside my if statement?

  2. :slightly_smiling_face:
    At EvilCircle constructor, some parameters were given fixed values but when I wanted to create an instance object, I specified the values again(At Bringing the evil circle into the program). Can’t i just omit those parameters?
    Modified: I skipped the fixed paramters and it did work. I’d be happy if I could get explanation on why both are correct

  3. At ** setControls()**, why let _this = this? You can check my answer in my codepen
    https://codepen.io/BikkyBlexxy/pen/mdeNROJ
    Thanks!

Hi there @Olaleye_Blessing, and thanks for sending your code in! Your code looks pretty complete to me, although when I run your codepen it gives me a syntax error in the console. You might want to have a look at this.

On to your questions:

  1. I don’t really understand this question — inside the if statement, you are reducing the count by 1 each time a ball is hit, and then you are updating the paragraph content to tell the user the new total of balls left. They need to do together really.

  2. Yes, you don’t need to specify the fixed parameters again when you instantiate the EvilCircle object instance (I think this is what you are asking?). Our version of the call looks like this:

let evil = new EvilCircle(random(0,width), random(0,height), true);

So in the instantiation, we are only specifying a random position for the EvilCircle, and whether it exists.

  1. You are using let _this = this because you want to use the value of this as it is inside the EvilCircle.prototype.setControls = function() { } function, not the value of this inside the window.onkeydown = function(e) { } function. When you define a new function, you get a new scope, and therefore a new this (although bear in mind that this is not the case with arrow functions. They work differently with regards to this).
1 Like