Objects exercise

by Mehmetcan Sinir

JavaScript

//Write a JavaScript program to list the properties of a JavaScript object

var student = {
    name: "David Rayy",
    sclass: "VI",
    rollno: 12
};

for (var properties in student) {
    if (student.hasOwnProperty(properties)) {
        console.log(properties);
    }
}

//Write a JavaScript program to delete the rollno property from the following object. Also print the object before or after deleting the property.

delete student.rollno;
console.log(student);

//write the javascript program to get the length of a javascript object

var me = {
    name: "Mehmetcan",
    lastName: "Sinir",
    age: 29,
    job: "software engineer"
};

function getSize(obj) {
    var size = 0,
        keys;

    for (keys in obj) {
        if (obj.hasOwnProperty(keys)) {
            size++;
        }
    }
    return size;
}

console.log(getSize(me));

// Write a JavaScript program to display the reading status (i.e. display book name, author name and reading status) of the following books.

var library = [{
    title: 'Bill Gates',
    author: 'The Road Ahead',
    readingStatus: true
}, {
    title: 'Steve Jobs',
    author: 'Walter Isaacson',
    readingStatus: true
}, {
    title: 'Mockingjay: The Final Book of The Hunger Games',
    author: 'Suzanne Collins',
    readingStatus: false
}];

function displayStatus() {
    var stati = [];
    for (var i = 0; i < library.length; i++) {
        var text = "for " + library[i].title + " the reading status is " + library[i].readingStatus + /\n\r/;
        stati.push(text);
    }
    return stati;
}

console.log(displayStatus());


//Write a JavaScript program to get the volume of a Cylinder with four decimal places using object classes
//Volume of a cylinder : V = πr(squared)h

function Cylinder(height, diameter) {//at this point the Cylinder constructor has a prototype property, which points to Cylinder.prototype, a prototype Object, from which new object instances created by the Cylinder constructor get their methods from....