Interview Questions- Part-18

Get Weekdays based on input number

				
					//Get weekdays
//if you pass 0 --> Sunday
//if you pass 1 --> Monday and so on...if 8 --> undefined ...
const Weekdays = {
  Sunday: 0,
  Monday: 1,
  Tuesday: 2,
  Wednesday: 3,
  Thursday: 4,
  Friday: 5,
  Saturday: 6,
};
Object.freeze(Weekdays);
//you cannot add new properties nor you can alter the existing properties...

console.log(`%c ${Weekdays}`, "color:red;font-size:20px");
console.log(`%c ${Weekdays["Wednesday"]}`, "color:red;font-size:20px");

//Getting key based on value .. if id is 4--> so if value is 4 --> Output should be -->Thursday

const getWeekdays = (id) => {
  let day = Object.keys(Weekdays).find((key) => Weekdays[key] === id);
  console.log(`%c ${day}`, "color:red;font-size:20px");
};

getWeekdays(4); //Thursday
getWeekdays(14); //undefined
				
			

Get Weekdays based on input number- Key-value is swapped

				
					const Weekdays = {
  0: "Sunday",
  1: "Monday",
  2: "Tuesday",
  3: "Wednesday",
  4: "Thursday",
  5: "Friday",
  6: "Saturday",
};
Object.freeze(Weekdays);

console.log(`%c ${Weekdays[2]}`, "color:red;font-size:20px");

const getWeekdays = (id) => {
  console.log(`%c ${Weekdays[id]}`, "color:red;font-size:20px");
};

getWeekdays(2); //Tuesday
getWeekdays(12); //undefined

				
			

Switch Statement - direct return

				
					const getWeekdays = (id) => {
  switch (id) {
    case 0:
      return "Sunday";
    case 1:
      return "Monday";
    case 2:
      return "Tuesday";
    case 3:
      return "Wednesday";
    case 4:
      return "Thursday";
    case 5:
      return "Friday";
    case 6:
      return "Saturday";
    default:
      break;
  }
};

console.log(`%c ${getWeekdays(3)}`, "color:red;font-size:20px");
				
			

Switch Statement - using break

				
					const getWeekdays = (id) => {
  let weekday = "";
  switch (id) {
    case 0:
      weekday = "Sunday";
      break;
    case 1:
      weekday = "Monday";
      break;
    case 2:
      weekday = "Tuesday";
      break;
    case 3:
      weekday = "Wednesday";
      break;
    case 4:
      weekday = "Thursday";
      break;
    case 5:
      weekday = "Friday";
      break;
    case 6:
      weekday = "Saturday";
      break;
    default:
      break;
  }
  return weekday;
};

console.log(`%c ${getWeekdays(3)}`, "color:red;font-size:20px");

				
			

Capitalize the first letter

				
					//Capitalize the first letter
//Input :- this is random sentence
//Expected Output:- This is random sentence

let randomSentence = "this is random sentence";
//randomSentence.substring(1) will skip first letter and give rest part..
//his is random sentence
console.log(
  `%c ${randomSentence[0].toUpperCase() + randomSentence.substring(1)}`,
  "color:red;font-size:20px"
);

				
			

check for undeined/empty/null/false string

				
					let x = null;
x = undefined;
x = "";
x = 0;
x = Number.NaN;
x = false;

console.log(`%c ${x}`, "color:red;font-size:20px");

if (x) {
  console.log(`%c I have value`, "color:red;font-size:20px");
} else {
  console.log(`%c I am undefined/null/empty`, "color:red;font-size:20px");
}

				
			

check for undeined/empty/null/false string - Handle false value

				
					//what it is value is made false forcefully, it should not go inside else block
let x = null;
x = undefined;
x = "";
x = 0;
x = Number.NaN;
x = false;

console.log(`%c ${x}`, "color:red;font-size:20px");

if (x || x === false) {
  console.log(`%c I have value`, "color:red;font-size:20px");
} else {
  console.log(`%c I am undefined/null/empty`, "color:red;font-size:20px");
}

				
			

Check if object is empty

				
					let obj = {};

if (obj && Object.keys(obj).length === 0) {
  console.log(`%c I am empty`, "color:red;font-size:20px");
}

//Explanation
/*
1. first condition .. to check if it exists 
   even if its empty --> it will be true
   Imagine the condition is not there 
   let obj ={}
   obj =null;
   if obj is made null ..Object.keys will throw error

2.checks keys length if 0 ? empty array doesn't have keys ..so valid
*/
				
			

getPrototypeOf Object

				
					//case 1
const proto = {};
const obj = Object.create(proto);
Object.getPrototypeOf(proto) === Object.prototype;
//true

// Object.prototype
// {constructor: ƒ, __defineGetter__: ƒ, __defineSetter__: ƒ, hasOwnProperty: ƒ, __lookupGetter__: ƒ, …}

// Object.getPrototypeOf(proto)
// {constructor: ƒ, __defineGetter__: ƒ, __defineSetter__: ƒ, hasOwnProperty: ƒ, __lookupGetter__: ƒ, …}

//case 2
const proto1 = {};
const obj1 = Object.create(proto1);
Object.getPrototypeOf(obj1) === Object.prototype;
//false;

//case 3
const proto2 = {};
const obj2 = Object.create(proto2);
Object.getPrototypeOf(obj) === proto2;
// true

				
			

Leave a Comment