Cognizant Handson - Employee Experience Details - Class and Object & Date

 

Employee Experience Details - Class and Object & Date

 

Write a JavaScript program to display the emplyoee experience details. 

The method displayEmployee must take an employee's name, designation & year of experience as its arguments and invoke the createEmployee method with these arguments.

The createEmployee method should create an Employee Object  out of the Employee class (object with values using a its parameterized constructor) and return the object to the caller - displayEmployee method.

Validate this object using instanceof operator and if valid, calculate the no of years in service (current year - year of joining) & return the same appended to a string (refer to console output).  Note: You may fetch the current year using the Date function & you may verify the correctness of your function by invoking it from within console.log.

 

console.log(displayEmployee("Jerold","Manager",15));

Console Output :


Solution:
script.js

function Employee(_name, _designation, _year_of_experience) 
{
//fill code here
        this.name=_name;
        this.designation=_designation;
        this.year_of_experience=_year_of_experience
    
}

function createEmployee(name, designation, year_of_experience)
{
//fill code here
    var emp= new Employee(name,designation,year_of_experience);
    
    return emp;
}

function validateObject(employee)
{
    //fill code here
    if(employee instanceof(Employee)){
        var today=new Date();
        var curr_year=today.getFullYear()
        var exp=curr_year - employee.year_of_experience;
        return employee.name+ " is serving the position of "+employee.designation+" since " +exp;
    }
}
function displayEmployee(name,designation,year_of_experience){
        var emp= createEmployee(name,designation,year_of_experience);
        return validateObject(emp);
    }
console.log(displayEmployee("Jerold","Manager",15));

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.