Interview JavaScript - gits5213/InterviewProject GitHub Wiki
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());
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']));
function fact(x) {
if(x==0) {
return 1;
}
return x * fact(x-1);
}
console.log(fact(5));
`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]);`
`}`
function odd_even(){
const x = 5;
if(x%2 == 0){
console.log("Even Number");
}else{
console.log("Odd Number");
}
}
odd_even();
for (var i = 1; i < 50; i++){
if ((i % 2) == 0) //Generate odd Number !=
console.log(i + ' ');
}
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));
function reverse(s){
return s.split("").reverse().join("");
}
console.log(reverse("hello"))
function reverse(s) {
var o = '';
for (var i = s.length - 1; i >= 0; i--)
o += s[i];
return o;
}
console.log(reverse("hello"))
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);
var numbers = [1,2,3,4,5,6,7,8];
numbers.forEach((element, index, array) => array[index] = element* element);
console.log(numbers);
function square(sq){
let result = sq.map(a=>a*a);
return result;
}
console.log(square([1,2,3]));
// 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));
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
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.
-
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.
-
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.
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.
13
14
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13
14
98
14, 27, 39, 50, 60, 69, 77, 84, 90, 95, 99
96, 97, 98
14
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));
function getFirstUniqueCharacter(str) {
return str.split('').filter( (character, index, obj) => obj.indexOf(character) === obj.lastIndexOf(character)).shift();
}
console.log(getFirstUniqueCharacter('abcbd'));
function uniq(arr){
let result = arr.split('').filter((value, index, array) => array.indexOf(value)==array.lastIndexOf(value));
return result;
}
console.log(uniq('aabdefcc'));
function uniq(arr){
let result = arr.split('').filter((value, index, array) => array.indexOf(value)==array.lastIndexOf(value)).shift();
return result;
}
console.log(uniq('teaabdefcc'));
function verify(){
var x = 30;
if(x<18){
console.log("Under 18");
}else{
console.log("You are Adult");
}
}
verify();