Interview JavaScript - gits5213/InterviewProject GitHub Wiki

Add two numbers

function add(a, b){ return a+b; } console.log(add(2, 2));

//or

// function add(){ // var a = 3; // var b = 2; // return a + b; // } // console.log(add());

Find the duplicate value from the arrays

const listOfArray = [1,2,3,4,5,6,7,2,3]; function duplicateValue(arr){ let result = arr.filter((value, index, array) => array.indexOf(value) !== index); return result; } //console.log(duplicateValue(listOfArray)); console.log(duplicateValue(['a', 'a', 'b', 'c', 'b', 'd']));

Generate Factorial like 5!

function fact(x) { if(x==0) { return 1; } return x * fact(x-1); }

console.log(fact(5));

Generate Fibonacci Number

`var arr = [];  // Initialize array!`
`arr[0] = 0;`
`arr[1] = 1;`
`for(var i=2; i<=10; i++){`
    `arr[i] = arr[i-2] + arr[i-1]; // Next fibonacci number = previous + one before previous`
    `console.log(arr[i]);`
`}`

Find out it is Odd Or Even Number

function odd_even(){ const x = 5; if(x%2 == 0){ console.log("Even Number"); }else{ console.log("Odd Number"); } } odd_even();

or //Generate Odd and Even Number

for (var i = 1; i < 50; i++){ if ((i % 2) == 0) //Generate odd Number != console.log(i + ' '); }

Generate Prime Number

function findPrime(limit){ var parameter = [], a, b; loop: for(a=2; a<limit; a++){ for(b=2; b<a; b++){ if(a%b === 0 ){ continue loop; } } parameter.push(a); } return parameter; } console.log(findPrime(20));

How to get reverse string

function reverse(s){ return s.split("").reverse().join(""); } console.log(reverse("hello"))

Reverse String

//OR - How to get reverse string

function reverse(s) { var o = ''; for (var i = s.length - 1; i >= 0; i--) o += s[i]; return o; } console.log(reverse("hello"))

Square each number in an array in javascript

let list = [1,2,3]; result = list.map(a => a*a); //How to get doubble of the number a+a; How to get all One (1) : a/a console.log(result);

//OR - //Square each number in an array in javascript

var numbers = [1,2,3,4,5,6,7,8]; numbers.forEach((element, index, array) => array[index] = element* element); console.log(numbers);

//Or

function square(sq){ let result = sq.map(a=>a*a); return result; } console.log(square([1,2,3]));

Swaping Number

// var a = 5; // var b = 10; // [a, b] = [b, a]; // ES6

// console.log(a, b);

function swap(x, y) { var x, y, z; z = x; x = y; y = z; return [x, y]; }

console.log(swap(2, 3));

Triangle of Star

for(var i=1; i <= 5; i++){ for(var j=1; j<=i; j++){ console.log('*'); } console.log('<br/>'); }

A building has 100 floors. One of the floors is the highest floor an egg can be dropped from without breaking

Solution

We'll use the first egg to get a range of possible floors that contain the highest floor an 
egg can be dropped from without breaking. To do this, we'll drop it from increasingly higher 
floors until it breaks, skipping some number of floors each time.

When the first egg breaks, we'll use the second egg to find the exact highest floor an egg can 
be dropped from. We only have to drop the second egg starting from the last floor where the 
first egg didn't break, up to the floor where the first egg did break. But we have to drop 
the second egg one floor at a time.

With the first egg, if we skip the same number of floors every time, the worst case number of 
drops will increase by one every time the first egg doesn't break. To counter this and keep 
our worst case drops constant, we decrease the number of floors we skip by one each time we 
drop the first egg.

When we're choosing how many floors to skip the first time we drop the first egg, we know:

  1. We want to skip as few floors as possible, so if the first egg breaks right away we don't have a lot of floors to drop our second egg from.

  2. We always want to be able to reduce the number of floors we're skipping. We don't want to get towards the top and not be able to skip floors any more.

    Knowing this, we set up the following equation where nn is the number of floors we s

    kip the first time:

    n + (n-1) + (n-2) + \ldots + 1 = 100n+(n−1)+(n−2)+…+1=100

    This triangular series ↴ reduces to n * (n+1) / 2 = 100n∗(n+1)/2=100 which solves to give n = 13.651n=13.651. We round up to 14 to be safe. So our first drop will be from the 14th floor, our second will be 13 floors higher on the 27th floor and so on until the first egg breaks. Once it breaks, we'll use the second egg to try every floor starting with the last floor where the first egg didn't break. At worst, we'll drop both eggs a combined total of 14 times.

    For example:

    Highest floor an egg won't break from

    13

    Floors we drop first egg from

    14

    Floors we drop second egg from

    1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13

    Total number of drops

    14

    Highest floor an egg won't break from

    98

    Floors we drop first egg from

    14, 27, 39, 50, 60, 69, 77, 84, 90, 95, 99

    Floors we drop second egg from

    96, 97, 98

    Total number of drops

    14

Find the unique value from the arrays

function duplicateValue(arr){ let result = arr.filter((value, index, array) => array.indexOf(value) == index); return result; } const listOfArray = [1,2,3,4,5,6,7,2,3]; console.log(duplicateValue(listOfArray));

How to get First Unique Character from the String

function getFirstUniqueCharacter(str) { return str.split('').filter( (character, index, obj) => obj.indexOf(character) === obj.lastIndexOf(character)).shift(); } console.log(getFirstUniqueCharacter('abcbd'));

How to get only unique value not touching duplicate value

function uniq(arr){ let result = arr.split('').filter((value, index, array) => array.indexOf(value)==array.lastIndexOf(value)); return result; } console.log(uniq('aabdefcc'));

How to get first unique value

function uniq(arr){ let result = arr.split('').filter((value, index, array) => array.indexOf(value)==array.lastIndexOf(value)).shift(); return result; } console.log(uniq('teaabdefcc'));

Verify Age of the person

function verify(){ var x = 30; if(x<18){ console.log("Under 18"); }else{ console.log("You are Adult"); } } verify();

⚠️ **GitHub.com Fallback** ⚠️