Interview Questions- Part-5

Guess the output- freeze vs seal

				
					let employee = {
  firstname: "anurag",
  lastname: "nayak",
};

employee.age = 32; //you can add age
console.log(employee);
//////////////////////////

				
			
				
					let employee1 = {
  firstname: "anurag",
  lastname: "nayak",
};

Object.freeze(employee1);
//Uncaught TypeError: Cannot add property age, object is not extensible
// employee1.age = 32;

//index.js:963 Uncaught TypeError: Cannot assign to read only
//property 'firstname' of object '#<Object>'

employee1.firstname = "Ábhishek";
console.log(employee1);
				
			
				
					let employee2 = {
  firstname: "anurag",
  lastname: "nayak",
};

Object.seal(employee2);
//employee2.age = 32; //you cannot add property
//you can change name if you dont add age
employee2.firstname = "Ábhishek";//name you can change
console.log(employee2);
				
			

Panagram

				
					
//USING INCLUDES OF String
const test1 = (str) => {
  const strUpper = str.toUpperCase();
  for (let i = 65; i <= 90; i++) {
    if (!strUpper.includes(String.fromCharCode(i))) {
      return false;
    }
  }
  return true;
};

//USING ARRAY////
const test2 = (str) => {
  str = str.toUpperCase();
  const array = new Array(26).fill(false);
  for (let i = 0; i < str.length; i++) {
    if (str[i] >= "A" && str[i] <= "Z") {
      let index = str.charCodeAt(i) - "A".charCodeAt(0);
      array[index] = true;
    }
  }
  let result = array.every((i) => i);
  return result;
};

///USING MAP////
const test3 = (str) => {
  str = str.toUpperCase();
  const alphabets = new Map();
  for (let i = 0; i < str.length; i++) {
    if (str[i] >= "A" && str[i] <= "Z") {
      alphabets.set(str[i], true);
    }
  }
  if (alphabets.size === 26) {
    return true;
  }
  return false;
};

///USING SET////
const test4 = (str) => {
  str = str.toUpperCase();
  const alphabets = new Set();
  for (let i = 0; i < str.length; i++) {
    if (str[i] >= "A" && str[i] <= "Z") {
      alphabets.add(str[i], true);
    }
  }
  if (alphabets.size === 26) {
    return true;
  }
  return false;
};

console.log(test1("TThe quick /// browns fox jump over the lazy dog"));
console.log(test2("TThe quick ,... browns fox jump over the lazy dog"));
console.log(test3("TThe quick ADD@ browns fox jump over the lazy dog"));
console.log(test4("TThe quick ADD@ browns fox jump over the lazy dog"));

				
			

Guess the output !!

				
					console.log(+true); //1
console.log(true); //true
console.log(typeof +true); //number
console.log(typeof true); //boolean
				
			

Guess the output !!

				
					console.log(!"anurag"); //false
console.log(!""); //true
console.log("" === false); //false
console.log("" == false); //true
				
			

Guess the output !!

				
					let x = 3;
let num = new Number(3);
console.log(x === num); //false
console.log(x == num); //true
				
			

Guess the output !!

				
					let x = 3;
let num = new Number(3);
console.log(x === num); //false
console.log(x == num); //true
				
			

Guess the output !!

				
					let obj = {
  name: "Anurag",
  lastname: "Nayak",
  name: "Abhishek",
};

console.log(obj.name); //Abhishek
				
			

Guess the output !!

				
					let num = [1, 2, 3, 4];
num[10] = 50;
console.log(num);
//[1, 2, 3, 4, empty × 6, 50]

num.length = 0;
console.log(num);
//output :- []
				
			

Guess the output !!

				
					console.log(!!1); //true
console.log(!1); //false
console.log(!null); //true
console.log(!!null); //false
				
			

Function Expression vs Function Declaration !!

				
					//Function Declaration vs Function Expression

console.log(functionDeclaration());
//index.js:1118 Uncaught ReferenceError: Cannot access 'functionExpression1' before initialization
console.log(functionExpression1());
//index.js:1118 Uncaught ReferenceError: Cannot access 'functionExpression2' before initialization
console.log(functionExpression2());

function functionDeclaration() {
  console.log("function declaration");
}

const functionExpression1 = function () {
  console.log("function expression1");
};
const functionExpression2 = () => {
  console.log("function expression2");
};

				
			

Divisible by 3 -FIZZ, Divisible by 5 -BUZZ,
Divisible by 3 and 5 - FIZZBUZZ

				
					const fizbuzz = () => {
  for (let i = 1; i <= 100; i++) {
    if (i % 5 === 0 && i % 3 === 0) console.log(i + "FIZZBUZZ");
    else if (i % 3 === 0) console.log(i + "FIZZ");
    else if (i % 5 === 0) console.log(i + "BUZZ");
    else console.log(i);
  }
};

fizbuzz();

				
			
				
					const fizbuzz = () => {
  for (let i = 1; i <= 100; i++) {
    if (i % 5 === 0 && i % 3 === 0) {
      console.log(i + "FIZZBUZZ");
      i++;
    }
    if (i % 3 === 0) {
      console.log(i + "FIZZ");
      i++;
    }
    if (i % 5 === 0) {
      console.log(i + "BUZZ");
    } else {
      console.log(i);
    }
  }
};

fizbuzz();
				
			

GCD of two numbers

				
					const gcd = (a, b) => {
  let smallnum = 0;
  let bignum = 0;

  a > b ? ([bignum, smallnum] = [a, b]) : ([bignum, smallnum] = [b, a]);

  for (let i = smallnum; i >= 1; i--) {
    if (bignum % i === 0 && smallnum % i === 0) {
      return i;
    }
  }
};
let c = gcd(15, 5);
console.log(c); 
//output:- 5
				
			

Leave a Comment