Giter Club home page Giter Club logo

javascript-exercise's Introduction

javascript-exercise

Exercise - 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

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 , and 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


Exercise - 2

Create a set of function constructors to describe job titles and properties within a Company.

  • Employee as base object having two properties: “name”(empty string by default) and “department”(“general” by default).
  • Manager inherits from Employee and adds a new property called “reports” as Array.
  • WorkBee inherits from Employee and adds a new property called “projectName” as empty String.
  • SalesPerson inherits from WorkBee and overwrites department name to “sales”, project name to “internal”, plus adds a new property called “revenue” as Number.
  • SoftwareEngineer inherits from WorkBee and overwrites department name to “tech”, project name to “App”, plus adds a new property called techSkills as Array.

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


Exercise - 3

What does the follow code print and why?

for(var i = 0; i < 10; i++) {
    setTimeout(function() {
        console.log(i);
    }, 0);
}

Exercise - 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());

Exercise - 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


Exercise - 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');

Exercise - 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


Exercise - 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);

Exercise - 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
});

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.