Use of "return" & "this" in a udemy challenge

This challenge is from udemy course. I am still at the beginning of the challenge.

The code I wrote until now is to calculate the 20% tip is

var tip = {
    bills: [124, 48, 268, 180],  
    calcTip: function () {
         for (var i = 0; i < this.bills.length; i++) {
            if (this.bills[i] < 50 )  {
                var twenty = this.bills[i]  * 0.2;

It does not work or return anything. What is the issue that make it impossible to console.log(this.twenty)?

I doubt it is the use of return or this.

I tried to tweak the code but it did not work.

Any thoughts?

this refers to the object. The object does not have a property called twenty, so console logging it will get you undefined, because it is not defined anywhere.

The method returns nothing because you aren’t returning anything.

console.log(twenty) might work.
You are not doing task 4 yet.

It is because you are using this.twenty. Remember when you are using the keyword this refer to the object, that is the way how can access to different properties or methods of the object.

When you create a variable inside of one method or into block scope is it encasulated on it. that mean you only can access depending where the variable was created.

Apart of that, o accomplish this challenge my suggestion are:

  1. Insted of create an if condition on each tip, try to use a switch statement and on each case you could implemente the validation and push tip and (bill + tip) into the empty array as properties of the object that you created.
  2. If you decided to use an if or switch conditional you maybe ended with repeating code. My suggestion for this it create another method passing the bill and the tip. That way you can reduce a lot of your code.

An example of this challenge could be:

var tip = {
	allTips: [],
	finalPaid: [],
	bills: [1, 2, 3, 4, 5],
	generateTip: function(bill, tip) {
        // Add code below this line
        // Remember that you can call properties and methods using the keyword this.allTips or this.finalPaid
        // Also keep in mind to use the differents properties that have an array like [].push()
	calcTip: function() {
       for (var i = 0; i < this.bills.length; i++) {
			switch (true) {
				case (this.bills[i] < 50):
					this.generateTip(this.bills[i], 0.2);
                // Add the other cases below this line using case statement