Problem Statement: 

Consider the following JavaScript code and pick the statement that best describes the code

function Dog (name, age) {

this.name = name;

this.age = age;

}

var ageDiff = function(dog1, dog2) {

return Math.abs(dog1.age - dog2.age);

}

ageDiff(new Dog("Sparky", 12), new Dog("Molly", 8));

Options:
A. The JavaScript engine will create two dog objects, then pass them as arguments to function ageDiff

B. The JavaScript engine will call the function ageDiff first, then create two dog objects to calculate the age difference.

C. Since objects can not be passed as parameters, the JavaScript engine will just take age variables, 12 and 8, to calculate age difference and no object will be created.

D. JavaScript engine will create two dog objects but won’t assign values to their properties while calling function ageDiff.

Solution

A. The JavaScript engine will create two dog objects, then pass them as arguments to function ageDiff

Explanation :

The function call ageDiff(new Dog(“Sparky”, 12), new Dog(“Molly”, 8)) will create two Dog objects with properties of name and age assigned. Then, the ageDiff() function is called, which subtracts the age of dog1 from the age of dog2 and returns the absolute value. The output of the code snippet will be 4.

Options B and C are invalid since the ageDiff() function is called after creating the objects, and objects can be passed as parameters

Option D is also invalid since the values of properties are assigned to the objects.

You might also like, JavaScript homework help.