Beruflich Dokumente
Kultur Dokumente
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;
*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 WorkBee(name) {
//name, department inherited - plus projectName
}
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: ...}]
console.log(Michael.department); // sales
console.log(Michael.name) // Michael T
console.log(Michael.projectName); // internal
console.log(Michael.revenue); // 2540
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.
4. What will the following code output to the console and why?
var hero = {
_name: 'John Doe',
getName: function (){
return this._name;
}
};
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);
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
}
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);
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 }
]