Interview Questions- Part-7

Guess the output

				
					const employee = {
  name: "anurag",
  getName: () => {
    console.log(this.name);
  },
};

employee.getName();

/*index.js:1308 Uncaught TypeError: Cannot read properties of undefined (reading 'name')
    at Object.getName (index.js:1308:22)
    at index.js:1312:10*/
				
			

Guess the output

				
					const employee = {
  name: "anurag",
  getName: function () {
    console.log(this.name);
  },
};

employee.getName();

//output:- anurag
				
			

Number with maximum repititions

				
					//output shoule be - number with maximum repition
// 5 --> 5
// 5 is repeated 5 times
const number = [1, 5, 5, 5, 5, 5, 4, 4, 4, 3, 2];
let map = new Map();
let count = 0;
let max = 0;
let maxNum = 0;
number.forEach((i) => {
  if (map.has(i)) {
    count = map.get(i);
  }
  map.set(i, ++count);
  if (count > max) {
    max = count;
    maxNum = i;
  }
  count = 0;
});
console.log(maxNum + " is repeated ::" + max + " times");
				
			

Maximum number in Array and its repititions

				
					//Find the maximum value.. Find out the repition of that number
//8 is repeated 6 times
//1st solution
const dataset = [8, 8, 1, 5, 5, 5, 5, 5, 4, 4, 4, 3, 2, 8, 8, 8, 8];
let max = Math.max(...dataset);
var count = dataset.reduce(function (counter, value) {
  return counter + (value === max);
}, 0);

//2nd solution
let count = dataset.filter(x => x === max).length;

console.log(max + " is repeated ::" + count + " times");
				
			

Guess the output

				
					const arr = [2, 4, 6, 8, 10000];
for (let i = 0; i < arr.length; i++) {
  setTimeout(() => {
    console.log(arr[i]);
  }, arr[i]);
}

//output:- 2 4 6 8 10000
				
			

AMPM time to HH:MM format

				
					let timeAMOrPM = "10:32AM";
let amOrpm = timeAMOrPM.substring(timeAMOrPM.length - 2, timeAMOrPM.length);
let time = timeAMOrPM.substring(0, timeAMOrPM.length - 2);
let hour = time.split(":")[0];
let min = time.split(":")[1];
if (amOrpm === "PM") {
  hour = 12 + Number.parseInt(hour);
}
console.log("final time -->" + hour + ":" + min);
				
			

Make changes to get expected output

				
					//what has to be done to do this as below ?
//human.eat().play().sleep();

let human = {
  eat() {
    console.log("I am eating");
  },
  play() {
    console.log("I am eating");
  },
  sleep() {
    console.log("I am sleeping");
  },
};

///////////////////////////////////

let human = {
  eat() {
    console.log("I am eating");
    return this;
  },
  play() {
    console.log("I am eating");
    return this;
  },
  sleep() {
    console.log("I am sleeping");
    return this;
  },
};

human.eat().play().sleep();
				
			

Password Validity Question

				
					function checkPassword(password) {
  let onelowercaseLetter = /(?=.+[a-z])/;
  let oneUppercaseLetter = /(?=.+[A-Z])/;
  let oneDigit = /(?=.+[0-9])/;
  let oneSpecialCharacter = /(?=.+[!@#$%^&*])/;
  let minEightDigit = /(?=.{8,})/;
  let isValid =
    onelowercaseLetter.test(password) &&
    oneUppercaseLetter.test(password) &&
    oneDigit.test(password) &&
    oneSpecialCharacter.test(password) &&
    minEightDigit.test(password);

  return isValid ? "passoword is valid" : "password is invalid";
}

console.log(checkPassword("aZ#1333ddd3"));
console.log(checkPassword("anurag"));

				
			

Change the background color

				
					 //HTML part
 <button id="blue">blue</button>
 <button id="green">green</button>
 <button id="yellow">yellow</button>
    
    
//Javascript part    
const changeColor = (color) => {
  document.body.style.background = color;
};

document.querySelector("#blue").addEventListener("click", () => changeColor("blue"));
document.querySelector("#green").addEventListener("click", () => changeColor("green"));
document.querySelector("#yellow").addEventListener("click", () => changeColor("yellow"));
				
			

Remove duplicate nested array

				
					let foodItems = [
  ["Chicken Tikka", "Paneer Masala"],
  ["Chicken Tikka", "Mix Veg"],
  ["Paneer Masala"],
];

//output should be
//[Chicken Tikka , Paneer Masala , Mix Veg]

let final = [...new Set(foodItems.flat())];
console.log(final);
				
			

Combine two different array

				
					//Expected output
//[1,"one",2,"two",3,"three"]
const nums = [1, 2, 3];
const strs = ["one", "two", "three"];
const mapped = nums.map((val, index) => [val, strs[index]]);
console.log(mapped.flat(1));

const mapped1 = nums.flatMap((val, index) => [val, strs[index]]);
console.log(mapped1);
				
			

Change the output

				
					const myReverseName = ["g", "a", "r", "u", "n", "a"];
let final = myReverseName.reduceRight((c, v) => c + v);
console.log(final);
				
			

Leave a Comment