Skip to content

Instantly share code, notes, and snippets.

@agnellvj
Last active October 6, 2017 06:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save agnellvj/3e5407583e7251505414f2989b3ddffa to your computer and use it in GitHub Desktop.
Save agnellvj/3e5407583e7251505414f2989b3ddffa to your computer and use it in GitHub Desktop.

1. What is the difference between functionOne() and functionTwo() and what will be the output?

console.log(functionOne());
console.log(functionTwo());
var functionOne = function() { 
  return “Hi!  Hello I am functionOne”;
};
function functionTwo() {
  return “Hi!  Hello I am functionTwo”;
}

functionOne should push to the top of whatever it is defined in and it should print out it's value. It will only exist in the scope it is defined in. functionTwo should produce an undefined error and will exist in the global scope most likely window


2. How to refresh the page in javascript? How to do it for hard refresh by clearing cache?

window.location.reload(true); // force a reload from the server

3. Write a sample by using forEach?

[1,2,3,4].forEach(num => console.log("hi " + num));

4. What is the difference between call() and apply()? Mention how to use it in javascript?

They are similar in behavior, they immediately invoke whatever they are used on. The difference between the two is the argument list.


5. Write all HTML5 APIs?

Not sure what this question is about.


6. What are badges in Bootstrap and how to use it?

They usually present information like a count.

7. How to center align the div in the page using CSS?

I had to research this one but there are several ways: https://www.w3.org/Style/Examples/007/center.en.html


8. How to apply class to element conditionally in AngularJS?

use ng-class with an expression


9. How to apply opacity to the border of an element in CSS?

you can't use the opacity property so you have to use rgba values.


10. What is the difference between ng-if and ng-show?

Biggest difference is that using ng-if will either put something in or leave it out of the DOM. ng-{show,hide} will leave it in the DOM and it will be watched and executed on by the digest cycle.

11. How does broadcast(), emit() and on() works in AngularJS?

emit() sends an event from the current scope up to the root scope. broadcast() goes the other direction, it sends the event to the child scopes.


12. What is the use of promises? How to chain multiple promises in AngularJS?

It's used mainly for anything that may be long running and you don't want to wait synchronously for it to return. Not sure what the second part is asking, but a promise returns a promise so you can easily compose it.

13. Difference between apply(), watch() and digest()?

apply() is on an island compared to watch() and digest() with how it is used. you shouldn't be calling digest() directly whereas the others should be called directly.


14. Write a javascript program for closure?

any function or self executing function module can be considered a closure.


15. What is restrict option in directive? Can you define multiple restrict options on a directive?

It just limits the usage of the directive to certain types of html items (attribute, element, class). Multiple attributes can be used.


16. What will be the output?

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

console.log("a defined? " + (typeof a !== 'undefined'));
console.log("b defined? " + (typeof b !== 'undefined'));

the first console statement will return false since the scope of a only exists in the function (closure) the second console statment will return true since the scope of b will be global


17. Write a add method which will work properly when invoked using either syntax below.

console.log(add(2,3)); // Outputs 5 console.log(add(2)(3)); // Outputs 5

const add = function (num1, num2 = 0) {
  if(arguments.length === 2) return num1 + num2;
  return (anon_num) => num1 + anon_num;
}

18. Write a method to reverse the given string only by using built-in functions?

reverseString(“sample”) should return “elpmas”

const reverseString = (str) => str.split("").reverse().join("");

19. Write a javacsript code for the below problem

Input- aacaabbbzzddss

Output - a4b3c1d2s2z2

const chrCount = function(input) {
  let count = 0;
  let output = [];
  input.split("").sort().forEach((chr, index, arr) => {
    if(arr[index] === arr[index + 1]) {
      count++;
    } else {
      output.push(chr + ++count);
      count = 0;
    }
  });

  return output.join("");  
}

20. Write a custom directive for a drop down list which displays list of countries?

angular.module('MyModule', [])
.controller('Controller', ['$scope', function($scope) {}])
.directive('countryCodes', function() {
  return {
    restrict: 'A',
    template: `
      <select name="country" id="country">
        <option value="AF">Afghanistan</option>
        <option value="AL">Albania</option>
        ...
      </select>
    `
  };
});

21. Write custom filter to change rupees to dollars?

function MyModule($provide, $filterProvider) {
  $filterProvider.register('r2d', function() {
    return function(rupees) {
      return rupees * 0.02;
    };
  });
}

22. Write a login page using Angular with 2 input fields (username and password) and login button.

Authenticate the user from the following restful API by passing username and password as query string.

https://mysite.com/api/login?username= ? && password = ?

this seems a bit involved, gonna skip for now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment