Programming Questions
=====================
Use a programming language of your choice
-----------------------------------------
### 1. Complete the function caffeineBuzz, which takes a non-zero integer as it's one argument.
If the integer is divisible by 3, return the string "Java".
If the integer is divisible by 3 and divisible by 4, return the string"Coffee"
If the integer is one of the above and is even, add "Script" to the end of the string.
Otherwise, return the string "mocha_missing!"
caffeineBuzz(1) => "mocha_missing!"
caffeineBuzz(3) => "Java"
caffeineBuzz(6) => "JavaScript"
caffeineBuzz(12) => "CoffeeScript"
```javascript
function caffeineBuzz(number) {
let message = "mocha_missing";
if(number%3 == 0) {
message = 'Java';
if(number%4 == 0) {
message = "Coffee";
}
if(number%2 == 0) {
message += 'Script';
}
}
return message;
}
```
### 2. What are closures? Give an example. Why would it be usefull in a programming language.
Answer:
The most simple way to think of a closure is a function that can be stored as a variable , that has a special ability to access other variables local to the scope it was created in.
```javascript
function BaseCounter() {
// will become private variable
let counter = 0;
function upFucntion() {
//rember the variable;
counter++;
}
function downFunction() {
counter--;
}
function showFunction() {
return counter;
}
return{
up: upFucntion,
down: downFunction,
show: showFunction
};
}
var myCounter = BaseCounter();
myCounter.up();
myCounter.up();
myCounter.show(); //Other code can't change the value of counter by accident. as counter is so common name;
//2
```
### 3. Identify and remove duplicates from an array/list.
```javascript
//solution 1
// es 5
var arrayIndetifier = {};
var duplicates =[];
list = list.filter(function(el) {
if(arrayIndetifier[el]){
duplicates.push(el);
return false;
}else{
arrayIndetifier[el] = 1;
return true;
}
});
//solution 2
//es-6 to remove duplicates
list = Array.from(new Set(list));
```
### 4. Given 2 arrays find the values not present in the second one. eg [1,2,3,4,5] [4,5,6,7,8] = 1,2,3
```javascript
//Solution 1
//es-6
firstArray.filter(el=> !secondArray.includes(el));
//es-5
firstArray.filter(function(el) {
return (secondArray.indexOf(el) === -1);
});
```
### 5. Reverse a string in-place, i.e., change "I am a good" to "doog a ma I"
```javascript
//es-6
[...string].reverse().join('')
```
### 6. Write a function to verify a word as a palindrome.
```javascript
//es-2015 solution one
var isPalindrom = str => str === [...str].reverse().join('');
//es-5 solution 2
function isPalindrom(str) {
var len = Math.floor(str.length / 2);
for (var i = 0; i < len; i++)
if (str[i] !== str[str.length - i - 1])
return false;
return true;
}
```
### 7. An unsorted array has 1 to 1000 numbers, except one number. Find the missing number.
```javascript
//There are many solution for this particular problem.
// I choose the addition and math formula for this.
var unsortedArray = [];
// sum of all nutular numbers be
var totalSumShouldBe = 1000 * 1001 /2
// n(n+1) /2
var totalSum = unsortedArray.reduce( val, el=> val+el);
var missingNumber = totalSumShouldBe - totalSum;
```
Alternate language what I like `ruby`.
solution
```ruby
missingNumbersArray = (1..1000).to_a - unsortedArray;
```
### 8. Extend the previous function to find 2 missing numbers. can it be extended to find n missing numbers?
Yeah, from `ruby` above solution will work for `n` missing numbers. let's try to solve this in javascript. :)
Some, math will be involved in the finding missing 2 numbers.
for the above solution we have found.
`x = sum of all - (sum of all -x)`, we got `x`. Here we need to find 2 let they are `x` and `y`.
we need two equation to get the 2 unknown. Note are are going for summation of powers not multiplecation of all numbers as there my may b a max number overflow.
sum of squares = a1^2 + a2^2+....
x^2 + y^2 = sumOfSquare of all - sum of square with missings.
So, x = sum of x+y - y
```javascript
// from the above solution.
// sum of all nutular numbers be
var totalSumShouldBe = 1000 * 1001 /2
// n(n+1) /2
var totalSum = unsortedArray.reduce( (val, el)=> val+el);
var missingTwo = totalSumShouldBe - totalSum;
// sum of squares
// n(n+1)(2n+1)/6
var squaresSumShouldBe = (1000 * 1001 * 2001)/6 ;
var squaresSum = unsortedArray.reduce( (val,el) => val+ el*el, 0);
var missingTwoSq = squaresSumShouldBe - squaresSum;
// x2+y2 = missingTwoSq
// x+y = missingTwo
// (x+y)2 = x2+y2+2xy
// xy = (missingTwo^2 - missingTwoSq)/2
var missingTwoProduct = (missingTwo*missingTwo - missingTwoSq) /2
// now x+y and xy are known so y = missingTwoProduct/x so, we can calculate both now.
```
If there are `n` element missing than the above approach will become pain.
```javascript
//sorting required..
const getMissingElement = (array)=>{
let storedArray = unsortedArray.sort(); //first step
let n = Math.max.apply(null, arr); // get the maximum
let result = [];
for (let i=1 ; i')
.then((response)=>response.json())
.then(
//do what to do.
)
.catch();
// Angular
// we can use both `$http` and `$resource`. i will use `$resource` as it is REST oriented.
// Note this depends on `ngResource` module.
//
$resource('/nexsales/users/:id', {'gender':"@gender"});
//Jquery
$.ajax( "/nexsales/data" )
.done(function() {
alert( "success" );
})
.fail(function() {
alert( "error" );
})
.always(function() {
alert( "complete" );
```
### 8. Is JavaScript a class-based or prototype-based language? How can you implement inheritance in JavaScript?
Answer ::
JavaScript is prototype based language.
```javascript
// es-5 way to implement inheritance
function Base(){
}
function Child(){
Base.call(this,args);
}
Child.prototype = Object.create(Base.prototype);
//reset the constructor
Child.prototype.constructor = Child;
// es-6 way
class Base{
}
class Child extendes Base{
super(args); //can call constructor of parent class.
}
```
### 9. What is the difference between 'undefined' and 'null'?
Answer:
Both are primitive datatypes.
but type of `null` is `object`.
`undefined` is a placeholder if we don't assign the value to variable.
`null` is a real value and we need to assing to the value.
```javascript
var x;
console.log(x); // undefined
x = null;
console.log(x); // null
if(undefined || null){ //falsy
console.log("will not print");
}
```
`undefined` and `null` both are considered as falsy values.