JS-Objects.

Total Number of Questions: 8
  Create a Function.

Complete the function in the editor. In which you are given one object as a parameter, in that object, there is a data member named name. Your task is to create a method inside this object named setter, such that this method will print the value of the data member named as name.

Ans:-
function Check(obj1){
  obj1.setter = function(){
    console.log(obj1.name);
  }
};

  Delete a Parameter.

Complete the function in the editor. You have an object as a parameter. In which you have to delete the rollno property from the object and return the object.

Ans:-
function Check(obj1) {
  let del = delete obj1.rollno;
  return del;
//  return obj1.rollno;
};

  Check whether the Package is Dream Package or not.

Complete the function in the editor. In which you are given an object as a parameter. In that object, there is one property named as salary if the salary is maximum than 5 lakh then return "Dream" otherwise return "NotDream".

Ans:-
function Check(obj1) {
  for(let key in obj1){
    if(obj1.salary > 500000){
	  return "Dream";
    }
    else return "NotDream";
  }  
};

  Check whether the Object has a parameter or not.

Complete the function in the editor. In which you are given an object as a parameter. You have to return "false" if there is no parameter in the object otherwise return "true". 0 means no variable in an object and 1 means there are variables in an object.

Ans:-
function Check(obj1) {
    let count = 0;
    for(let key in obj1){
        count++;
    }
    if(count >= 1){
        return "true";
    }
    else return "false";
};

  Merge the Objects.

Complete the function in the editor. In which you are given two objects as a parameter.In which you have to merge two objects and return a single object.
The input consists of four lines, In which first lines contains two data members of first object which are defined as name and id. And the next two lines contains two data members for the second object which are defined as state and code.

Ans:-
function Check(obj1,obj2) {
    return Object.assign({}, obj1, obj2);
};

  Objects Multiplyer.

Complete the function in the editor. In which you are given an object and a digit N as a parameter.In the object there are two variable id and houseno are defined as a data member. You have to multiply both the data members with a given digit N.
There are three line in input, where the first one contains N, the next two lines contains the first data member i.e. id and second data member i.e. houseno.

Ans:-
function Check(a,obj1) {
    obj1.id = obj1.id * a;
    obj1.houseno = obj1.houseno * a;
    return obj1;
};

  Find the sum of Object Members.

You are given a function Check which takes an object Obj as a parameter. Your taks is to find out the sum of the three data members of the object. The data members are named as p1, p2, p3.
The input contains three number p1, p2 and p3 which represents the data members of the object.

Note: Complete the Check(obj1) function. No need to take any input.

Ans:-
function Check(obj1) {
    let newObj = obj1.p1 + obj1.p2 + obj1.p3;
    return newObj;
};

  Check whether the Objects are same or not.

Complete the function in the editor. In which you are given an object as a parameter. That object contains two variables name and id. Your task is to compare the objects name and id to the new_name and new_idn there as a parameter. Return "true" if new_name and new_id are same as objects name and id otherwise return "false".

Ans:-
function check(obj1,a,b) {
    if(obj1.name === a && obj1.id === b){
        return "true";
    }
    else return "false";
};