Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Tadjiev Codes
Tadjiev Codes
9,626 Points

JavaScript Function Constructor Methods

Hey Folks, I just wanted to ask how can I run the methods on the console or console.log() to check if the method is actually working?

function VideoType(title, category, cast, price, qty, length) {
    this.title = title; // string
    this.category = category; // string
    this.cast = cast; //array
    this.price = price; //number
    this.qty = qty; //number
    this.length = length; //number

    // Setting a default value for the parameter inside the constructor = 5
    this.changeQtyBy = function(ChangingNumber = 5) {

        if (Number.isInteger(this.qty)) {
            return this.qty + ChangingNumber;
        } else {
            return this.qty - ChangingNumber;
        }
    }

    this.getCast = function() {
        return this.cast; // this one line just returns values of the array
    }

    this.toString = function() { // add born part maybe 
            return `${this.title}|| ${this.category}|| ${this.cast}|| ${this.price}|| ${this.qty}|| ${this.length}|| ${this.getCast()}|| ${this.changeQtyBy()}`;
        } //I added the methods in toString() method to display the results

} // end of the constructor


// calling this function body onload event
function startMeUp() {

    let videos = [];

    videos.push(new VideoType("Simba", "Cartoon", ["castItem1", "castItem2"], 25, 10, 50));
    videos.push(new VideoType("Taken", "Thriller", ["castItem5", "castItem6"], 20, 11, 40));
    videos.push(new VideoType("Execution", "Thriller", ["castItem3", "castItem4"], 15, 8, 30));
    videos.push(new VideoType("Kungfu", "Comedy", ["castItem7", "castItem8"], 18, 7, 20));

    for (var i = 0; i < videos.length; i++) {

        videos[i].qty += 1;
        // So it adds 1 to all the items of the qty property in the Object which is stored in the array videos. So we had to loop through it
        // to add 5 units to the qty property for all videos. You must use a for loop to do this
    }

    let outputElement = document.getElementById('divVideoOutput');
    // clears the output every time
    outputElement.innerHTML = "";

    for (let i = 0; i < videos.length; i++) {

        var tempVideo = videos[i]; //Instead of typing StoreItems[i] every time the array we need, we can assign to a variable and use that to refer
        var tempEL = document.createElement("p");
        tempEL.innerText += tempVideo.toString();
        //Append to the Div
        outputElement.appendChild(tempEL);
    } //end for loop

} // closing function startMeUP

// I didn't use the document.write as tried using .toString() method plus DOM createElement P and then appendChild instead 

I know that changeQtyBy method is working as I tested within the toString() method. Although if I wasn't checking there. How to do that quickly on the console? I tried doing

console.log(changeQtyBy() );  and console.log(this.changeQtyBy() );  

But both of them result in errors. Thanks in advance))))))

1 Answer

Steven Parker
Steven Parker
231,153 Points

To test a method, you must call it using an instance of the class, so create that first:

var simba = new VideoType("Simba", "Cartoon", ["castItem1", "castItem2"], 25, 10, 50);
console.log( simba.changeQtyBy() );