How would you verify a prime number?
function isPrime(n){
var divisor = 2;
while (n > divisor){
if(n % divisor == 0){
return false;
}
else
divisor++;
}
return true;
}> isPrime(137);
= true
isPrime(237);
= false
How could you find all prime factors of a number?
function primeFactors(n){
var factors = [],
divisor = 2;
while(n>2){
if(n % divisor == 0){
factors.push(divisor);
n= n/ divisor;
}
else{
divisor++;
}
}
return factors;
}> primeFactors(69);
= [3, 23]
How do get nth Fibonacci number? - try 1
function fibonacci(n){
var fibo = [0, 1];
if (n <= 2) return 1;
for (var i = 2; i <=n; i++ ){
fibo[i] = fibo[i-1]+fibo[i-2];
}return fibo[n];
}
> fibonacci(12);
= 144
How do get nth Fibonacci number? - try 2
function fibonacci(n){
if(n<=1)
return n;
else
return fibonacci(n-1) + fibonacci (n-2);
}> fibonacci(12);
= 144
How would you find the greatest common divisor of two numbers?
function greatestCommonDivisor(a, b){
var divisor = 2,
greatestDivisor = 1;
//if u pass a -ve number this will not work. fix it dude!!
if (a < 2 || b < 2)
return 1;
while(a >= divisor && b >= divisor){
if(a %divisor == 0 && b% divisor ==0){
greatestDivisor = divisor;
}
divisor++;
}
return greatestDivisor;
}> greatestCommonDivisor(14, 21);
=7
greatestCommonDivisor(69, 169);
= 1
How would you merge two sorted array?
function mergeSortedArray(a, b){
var merged = [],
aElm = a[0],
bElm = b[0],
i = 1,
j = 1;
if(a.length ==0)
return b;
if(b.length ==0)
return a;
/*
if aElm or bElm exists we will insert to merged array
(will go inside while loop)
to insert: aElm exists and bElm doesn't exists
or both exists and aElm < bElm
this is the critical part of the example
*/
while(aElm || bElm){
if((aElm && !bElm) || aElm < bElm){
merged.push(aElm);
aElm = a[i++];
}
else {
merged.push(bElm);
bElm = b[j++];
}
}
return merged;
}> mergeSortedArray([2,5,6,9], [1,2,3,29]);
= [1, 2, 2, 3, 5, 6, 9, 29]
How would you swap two numbers without using a temporary variable?
function swapNumb(a, b){
console.log(‘before swap: ‘,’a: ‘, a, ‘b: ‘, b);
b = b -a;
a = a+ b;
b = a-b;
console.log(‘after swap: ‘,’a: ‘, a, ‘b: ‘, b);
}
> swapNumb(2, 3);
= before swap: a: 2 b: 3
= after swap: a: 3 b: 2
How would you reverse a string in JavaScript? - try 1
function reverse(str){
var rtnStr = '';
for(var i = str.length-1; i>=0;i--){
rtnStr +=str[i];
}
return rtnStr;
}> reverse(‘you are a nice dude’);
= “edud ecin a era uoy”
How would you reverse a string in JavaScript? - try 2
function reverse(str){
var rtnStr = [];
if(!str || typeof str != 'string' || str.length < 2 ) return str;
for(var i = str.length-1; i>=0;i--){
rtnStr.push(str[i]);
}
return rtnStr.join('');
}How would you reverse a string in JavaScript? - try 3
function reverse(str) {
str = str.split('');
var len = str.length,
halfIndex = Math.floor(len / 2) - 1,
revStr;
for (var i = 0; i <= halfIndex; i++) {
revStr = str[len - i - 1];
str[len - i - 1] = str[i];
str[i] = revStr;
}
return str.join('');
}How would you reverse words in a sentence?
//have a tailing white space
//fix this later
//now i m sleepy
function reverseWords(str){
var rev = [],
wordLen = 0;
for(var i = str.length-1; i>=0; i--){
if(str[i]==' ' || i==0){
rev.push(str.substr(i,wordLen+1));
wordLen = 0;
}
else
wordLen++;
}
return rev.join(' ');
}If you have a string like “I am the good boy”. How can you generate “I ma eht doog yob”? Please note that the words are in place but reverse.
function reverseInPlace(str){
return str.split(' ').reverse().join(' ').split('').reverse().join('');
}> reverseInPlace(‘I am the good boy’);
= “I ma eht doog yob”
How will you verify a word as palindrome?
function isPalindrome(str){
var i, len = str.length;
for(i =0; i isPalindrome('madam')
= true
> isPalindrome('toyota')
= falseIf you have a function that generate random number between 1 to 5 how could u generate random number 1 to 7 by using that function?
function rand5(){
return 1 + Math.random() * 4;
}
function rand7(){
return 5 + rand5() / 5 * 2;
}from a unsorted array of numbers 1 to 100 excluding one number, how will you find that number
function missingNumber(arr){
var n = arr.length+1,
sum = 0,
expectedSum = n* (n+1)/2;
for(var i = 0, len = arr.length; i < len; i++){
sum += arr[i];
}return expectedSum - sum;
}
> missingNumber([5, 2, 6, 1, 3]);
= 4
From a unsorted array, check whether there are any two numbers that will sum up to a given number?
function sumFinder(arr, sum){
var len = arr.length;
for(var i =0; i sumFinder([6,4,3,2,1,7], 9); = true > sumFinder([6,4,3,2,1,7], 2); = false
From a unsorted array, check whether there are any two numbers that will sum up to a given number? - try 2
function sumFinder(arr, sum){
var differ = {},
len = arr.length,
substract;
for(var i =0; i sumFinder([6,4,3,2,1,7], 9); = true > sumFinder([6,4,3,2,1,7], 2); = false
How would you find the largest sum of any two elements?
function topSum(arr){
var biggest = arr[0],
second = arr[1],
len = arr.length,
i = 2;if (len<2) return null;
if (biggest biggest){
second = biggest;
biggest = arr[i];
}
else if (arr[i]>second){
second = arr[i];
}}
return biggest + second;
}
Count Total number of zeros from 1 up to n?
function countZero(n){
var count = 0;
while(n>0){
count += Math.floor(n/10);
n = n/10;
}
return count;
}> countZero(2014);
= 223
How can you match substring of a sting?
function subStringFinder(str, subStr){
var idx = 0,
i = 0,
j = 0,
len = str.length,
subLen = subStr.length;for(; i subStringFinder(‘abbcdabbbbbck’, ‘ab’)
= 0
> subStringFinder(‘abbcdabbbbbck’, ‘bck’)
= 9
//doesn’t work for this one.
> subStringFinder(‘abbcdabbbbbck’, ‘bbbck’)
= -1
How would you create all permutation of a string?
function permutations(str){
var arr = str.split(''),
len = arr.length,
perms = [],
rest,
picked,
restPerms,
next;if (len == 0)
return [str];
for (var i=0; iCreate a function having no parameters declared and print all the arguments passed to it (key ‘in’ methods)
function func(){
for(let key in arguments){
console.log(arguments[key]);
}
}
// driver code func(1, "Hello", true);
Create a function having no parameters declared and print all the arguments passed to it (only value method)
function func(){
for(let value of arguments){
console.log(value);
}
}
// driver code func(1, "Hello", true);
Design a function which can recieve variable number of arguments in parameters and print them
function varArgsFunc(...params){
params.forEach(function(value, index){
console.log(index, ": ", value);
})
}
// driver code
varArgsFunc("Hello", ",", "World", "!!!");