Javascript interview questions

Javascript quiz questions

  • 1.

    Use the teamCity() function and teams array to create the following array ['Giants from New York', 'Seahawks from Seattle'].

    function Team(name, city) {
      this.name = name;
      this.city = city;
    }
    
    teams = [
      new Team("Giants", "New York"),
      new Team("Seahawks", "Seattle")
    ]
    
    function teamCity () {
      return this.name + " from " + this.city;
    }

    Answer:

    result = []
    teams.forEach(function (team) {
      result.push(teamCity.call(team));
    });
    console.log(result);
    View
  • 2.

    What does the following code print to the console?

    function sum (x, y) {
      return this.name + ", the sum is " + (x + y);
    }
    
    person = {
      name: "Phil"
    }
    
    console.log(sum.call(person, 2, 2));

    Answer:

    Phil, the sum is 4
    

    The call() method is similar to apply(), but it takes the parameters as arguments instead of an array.

    View
  • 3.

    What does the following code print to the console?

    function full_name (first, last) {
      return first + " " + last + ": " + this.age;
    }
    
    shredder = {
      age: 37
    }
    

    console.log(full_name.apply(shredder, ["evil", "badguy"]));

    Answer:

    evil badguy: 37
    

    The apply() method takes an array of arguments as the second parameter.

    View
  • 4.
    dude = {
      greet: "hi",
      bye: "peace"
    }
    
    function a () {
      return this.greet + ", " + this.bye;
    }
    
    console.log(a.apply(dude));

    Answer:

    "hi, peace"
    

    The apply() method is defined for function objects and accepts an object as the first parameter. The object that's passed to apply() as an argument is assigned to "this" when the function is invoked.

    View
  • 5.

    What does the following code print to the console?

    function Builder () {
      this.self = function () {
        return this;
      }
    }
    b = new Builder();
    console.log(b.self() === b);

    Answer:

    true
    

    For constructor functions, "this" refers to the object that's created by the constructor function.

    View
  • 6.

    What does the following code print to the console?

    hat = function () {
      return this;
    }
    
    obj1 = {
      check: hat
    }
    
    obj2 = {
      check: hat
    }
    
    console.log(obj1 === obj1.check())
    console.log(obj2 === obj2.check())

    Answer:

    true
    true
    

    When the hat() method is bound to obj1, "this" refers to obj1. When the hat() method is bound to obj2, "this" refers to obj2. The "this" keyword for a given method can be assigned to different objects depending on the invocation context for the method.

    View
  • 7.

    What does the following code print to the console?

    phone = {
      ring: function () {
        return this === phone;
      }
    }
    console.log(phone.ring());

     

    Answer:

    true
    

    The ring() function is invoked as a method, so the "this" keyword is assigned to the phone object.

    View
  • 8.

    What does the following code print to the console?

    result = function surfer() {
      return this === window;
    }();
    console.log(result);

    Answer:

    true
    

    There are four different ways to invoke functions in JavaScript and this example demonstrates invocation as a function. The keyword "this" equals window for functions that are invoked as functions.

    View
  • 9.

    Write the $$$.inject() function so the result variable is assigned to the array [1, 4, 9, 16].

    var callback = function (num) {
      return num * num;
    }
    
    var arr = [1, 2, 3, 4]
    result = $$$.inject(arr, [], callback)
    console.log(result)

    Answer:

    $$$ = {
      inject: function (arr, memo, callback) {
        arr.forEach(function (e) {
          memo.push(
            callback(e)
          )
        })
        return memo;
      }
    }
    View
  • 10.

    Write a function that satisfies the following console.log assertions.

    var callback = function (num) {
      return num % 2 === 0
    }
    
    console.log($$$.any([1, 3, 4], callback) === true)
    console.log($$$.any([1, 11, 111], callback) === false)

    Answer:

    $$$ = {
      any: function (arr, callback) {
        for (var i = 0, l = arr.length; i < l; i ++) {
          if (callback(arr[i])) {
            return true;
          }
        }
        return false;
      }
    }
    View
  • 11.

    Write a function that satisfies the following console.log assertions.

    var callback = function (num) {
      return num % 2 === 0
    }
    
    console.log($$$.all([1, 3, 4], callback) === false)
    console.log($$$.all([2, 6, 4], callback) === true)

     

    Answer:

    $$$ = {
      all: function (arr, callback) {
        for (var i = 0, l = arr.length; i < l; i ++) {
          if (!callback(arr[i])) {
            return false;
          }
        }
        return true;
      }
    }
    View
  • 12.

    Write the $$$.reject() function so the result variable is assigned to the array 'happy', 'joy'.

    arr = ['happy', 'snakes', 'joy', 'slippers'];
    result = $$$.reject(arr, function (word) {
      var last_letter = word[word.length - 1];
      return last_letter === "s";
    });
    console.log(result);

    Answer:

    $$$ = {
      reject: function (arr, callback) {
        result = [];
        arr.forEach(function(e) {
          if (!callback(e)) {
            result.push(e);
          }
        });
        return result;
      }
    }
    View
  • 13.

    Write the $$$.select() function so the result variable is assigned to the array [5, 7, 9].

    arr = [5, 6, 7, 8, 9];
    result = $$$.select(arr, function(num) {
      return num % 2 === 1;
    });
    console.log(result);

    Answer:

    $$$ = {
      select: function(arr, callback) {
        result = [];
        arr.forEach(function (num) {
          if (callback(num)) {
            result.push(num);
          }
        });
        return result;
      }
    }
    View
  • 14.

    Write the $$$.find() function so the result variable is assigned to the string "cat".

    arr = ['ant', 'cat', 'dog']
    result = $$$.find(arr, function(str) {
      return str[0] === "c";
    });
    console.log(result);

    Answer:

    $$$ = {
      find: function (arr, callback) {
        for (var i = 0; i < arr.length; i++) {
          var e = arr[i];
          if (callback(e)) {return e};
        }
      }
    }
    View
  • 15.

    Write the $$$.map() function so the result variable is assigned to the array [4, 8, 12].

    result = $$$.map([2, 4, 6], function(num) {
      return num * 2;
    });
    console.log(result);

    Answer:

    $$$ = {
      map: function(arr, callback) {
        result = [];
        arr.forEach(function (e) {
          result.push(callback(e));
        });
        return result;
      }
    }
    View
  • 16.

    Sort the values in the following array in ascending report: [32, 1, 44, 9]

    Answer:

    [32, 1, 44, 9].sort(function (x, y) {
      return x - y;
    })
    

    The sort() method takes an anonymous callback function as a parameter.

    View
  • 17.

    What does the following code print to the console?

    function c(f) {
      return f();
    }
    function blub() {
      return "monsters";
    }
    c(blub);

    Answer:

    "monsters"
    

    The c() function takes a callback function as a parameter. blub is passed to c() as an argument and is invoked within the body of c().

    Functions are "first class" in JavaScript because they can be passed as parameters and stored in data structures.

    View
  • 18.

    What does the following code print to the console?

    function sum() {
      result = 0;
      arguments.forEach(function (num) {
        result += num;
      });
      return result;
    }
    sum(1, 2, 3);

    Answer:

    This code raises an error because arguments is an array-like object, but it's not actually an array, so we can't use the forEach() method. According to Douglas Crockford, having arguments as an array-like object and not an actual array is a design flaw in the language.

    View
  • 19.

    What does the following code print to the console?

    love_story = function(x, y) {
      console.log(arguments);
    };
    love_story("princess", "frog");

    Answer:

    ["princess", "frog"]
    

    Arguments is an array-like object that corresponds with the parameters that are passed into a function.

    View
  • 20.

    What does the following function print to the console?

    function a(x, y, z) {
      return z;
    }
    console.log(a("blah"));

    Answer:

    undefined
    

    When too few arguments are supplied to a function, JavaScript assigns the missing arguments to undefined.

    View

© 2017 QuizBucket.org