Giter Club home page Giter Club logo

js-ninja-questions's Introduction

js-ninja-questions

Questions and Solutions for JS Ninjas

Please feel free to add or update any of the following
Your suggestions for improvement are more than welcome at [email protected]

Questions Here (Solutions are below)

Question 1. Make the following Javascript code work for any number of arguments and combinations

console.info(''+multiply(5, 6));    // 30
console.info(''+multiply(5)(6));    // 30
console.info(''+multiply(5, 2)(3)); // 30
console.info(''+multiply(5)(2)(3)); // 30

Question 2. What will be the following three outputs

var z={},
    y={'0':'nice'},
    x={'1':'nicer'};
z[y] = 0;
z[x] = 1;
console.log(z[y]);
console.log(z[x]);
console.log(z[y] === z[x]);

Question 3. How about the equality checks between the objects with the same content

var a = {'key': 'value'};
var b = {'key': 'value'};
console.info(a == b);
console.info(a === b);

Question 4. Let's implement a getUniqueId with auto incrementing in JS

getUniqueId(); // 1
getUniqueId(); // 2
getUniqueId(); // 3

Question 5. More Questions to be added... ๐Ÿ˜‰

Now Solutions

Solution 1.

var multiply = function() {
  var currentResult = eval(Array.prototype.join.call(arguments, '*'));
  multiply.lastResult = (multiply.lastResult ? (multiply.lastResult*currentResult) : currentResult);
  return multiply;
};
multiply.toString = function() {
  var temp = multiply.lastResult;
  multiply.lastResult = null;
  return temp;
};
//Test Cases
console.info(''+multiply(5, 6));    // 30
console.info(''+multiply(5)(6));    // 30
console.info(''+multiply(5, 2)(3)); // 30
console.info(''+multiply(5)(2)(3)); // 30

Solution 2.

var z={},
    y={'0':'nice'},
    x={'1':'nicer'};
z[y] = 0;
z[x] = 1;
console.log(z[y]);          //1
console.log(z[x]);          //1
console.log(z[y] === z[x]); //true

Solution 3.

var a = {'key': 'value'};
var b = {'key': 'value'};
console.info(a == b);   // false
console.info(a === b);  // false 

Solution 4.

Option 1 - Let's thrill with overriding the function itself being inside the function ๐Ÿ’€

var getUniqueId = function() {
    var id = 0;
    getUniqueId = function() {
        return ++id;
    };
    return ++id;
};

getUniqueId(); // 1
getUniqueId(); // 2
getUniqueId(); // 3

Option 2 - Using IIFE

var getUniqueId = (function() {
    var id = 0;
    return function() {
        return ++id;
    };
})();

getUniqueId(); // 1
getUniqueId(); // 2
getUniqueId(); // 3

js-ninja-questions's People

Contributors

ashwinik001 avatar

Stargazers

Jigyasu Arya avatar Rohit Kumar avatar

Watchers

 avatar

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.