Sie sind auf Seite 1von 5

Javascript Interview Exercises - ComplyAdvantage

1. Create a Javascript Function used to return next fibonacci number every time you
call it. Please do not expose any variable as we do not want any external script on
the page to change those internal variables.

function fibonacci(reset) {
//If reset is true - reset to the first fibonacci number
//Your code here
}

fibonacci(); //returns 1;
fibonacci(); //returns 1;
fibonacci(); //returns 2;
fibonacci(); //returns 3;
fibonacci(); //returns 5;
fibonacci(true); //returns 1;
fibonacci(); returns 1;
fibonacci(); returns 2;

Hint: Make use of closures and “private” variables.

*Follow-up: Incorporate above function within a js library and use it in a simple HTML page
to display returned value every time a user clicks on a NEXT button and reset numbers
when click on RESET. The HTML page will have <html>, <head> and <body> tags and also
3 additional HTML elements, a NEXT button, a RESET button and a inline element to
display returned value of fibonacci() from both next and reset buttons.

2. Create a set of function constructors to describe job titles and properties within a
Company. We define the following function constructors :
a. Employee as base object having two properties: “name”(empty string by
default) and “department”(“general” by default).
b. Manager inherits from Employee and adds a new property called “reports”
as Array.
c. WorkBee inherits from Employee and adds a new property called
“projectName” as empty String.
d. SalesPerson inherits from WorkBee and overwrites department name to
“sales”, project name to “internal”, plus adds a new property called “revenue”
as Number.
e. SoftwareEngineer inherits from WorkBee and overwrites department name
to “tech”, project name to “App-ComplyAdvantage”, plus adds a new property
called techSkills as Array.

function Employee(name) {
//name and department defaults.
}

function Manager(name, reports) {


//name, department inherited - plus reports
}

function WorkBee(name) {
//name, department inherited - plus projectName
}

function SalesPerson(name, revenue) {


//name, department and projectName inherited - plus revenue
}

function SoftwareEngineer(name, techSkills, projectName) {


//name, department and projectName inherited - plus techSkills
}

var John = new Manager("John Doe", [{name: "Q1", "statistics": ...}, {name: "Q2",
statistics: ...}]);

console.log(John.department); // General
console.log(John.name) // John Doe
console.log(John.reports) // [{name: "Q1", "statistics": ...}, {name: "Q2", statistics: ...}]

var Michael = new SalesPerson("Michael T", 2540);

console.log(Michael.department); // sales
console.log(Michael.name) // Michael T
console.log(Michael.projectName); // internal
console.log(Michael.revenue); // 2540

var Joseph = new SoftwareEngineer("Joseph K. Ellis", ["Javascript", "HTML"], "App-


ComplyAdvantage");

console.log(Joseph.department); // tech
console.log(Joseph.name) // Joseph K. Ellis
console.log(Joseph.projectName); // App-ComplyAdvantage
console.log(Joseph.techSkills); // ["Javascript", "HTML"]

Hint: Please make use of Prototypes to inherit from a function constructor to another.

3. What does the follow code print and why?

for(var i = 0; i < 10; i++) {


setTimeout(function() {
console.log(i);
}, 0);
}
*Follow-up: How would you adjust this to print 0, 1, 2, 3, … 9?

4. What will the following code output to the console and why?

var hero = {
_name: 'John Doe',
getName: function (){
return this._name;
}
};

var stolenName= hero.getName;

console.log(stolenName());
console.log(hero.getName());

5. What will the following code output to the console and why?

var a = 1;
function b() {
a = 10;
return;
function a() {}
}
b();
console.log(a);

*Follow-up: What if we remove “function a() {}” from the code

6. What will the following code output to the console and why?

console.log('script start');

setTimeout(function() {
console.log('setTimeout');
}, 0);

Promise.resolve().then(function() {
console.log('promise1');
}).then(function() {
console.log('promise2');
});

console.log('script end');

7. Create a Javascript native function attached to Object to check if two objects are
equal. Name this function “isEqual” and do not overwrite this if another function
already exists with same name attached to Object.
var obj1 = {
name: "John",
age: 10,
info: {
address: 'some place',
tel: '0123456789',
hobbies: ['1', '2']
}
}

var obj2 = {
name: "John",
age: 10,
info: {
address: 'some place',
tel: '0123456789',
hobbies: ['1', '2']
}
}

var smallObj = {
name: "John",
age: 10
}

var obj3 = obj2;


var obj4 = obj1;

smallObj.isEqual(obj1); //return false;


smallObj.isEqual({name: “John”}); //return false;
obj1.isEqual(undefined); //return false;
obj2.isEqual(obj1); //returns true;
obj3.isEqual(obj1); //returns true;
obj4.isEqual(obj1); //returns true;

8. What will the following code output to the console and why? What is wrong with this
code and how you can improve it (if needed)?

(function() {
var a = b = 5;
})();

console.log(b);

9. How we can preserve immutability of the following list of heroes?

const heroes = [
{ name: 'Wolverine', family: 'Marvel', isEvil: false },
{ name: 'Deadpool', family: 'Marvel', isEvil: false },
{ name: 'Magneto', family: 'Marvel', isEvil: true },
{ name: 'Charles Xavier', family: 'Marvel', isEvil: false },
{ name: 'Batman', family: 'DC Comics', isEvil: false },
{ name: 'Harley Quinn', family: 'DC Comics', isEvil: true },
{ name: 'Legolas', family: 'Tolkien', isEvil: false },
{ name: 'Gandalf', family: 'Tolkien', isEvil: false },
{ name: 'Saruman', family: 'Tolkien', isEvil: true }
]

const newHeroes = heroes.map(h => {


h.name = h.name.toUpperCase()
return h
});

Das könnte Ihnen auch gefallen