JavaScript Coding Interview
Q. No. 01/10:
console.log("1"+"4"+"1")// 141
console.log(+"1"+"1"+"1")// 111
console.log(1 + + "1" + "1")// 21
console.log(1 - "1" + "1")// 01
console.log("A"-"B") // NAN
Q. No. 02/10:
// Reverse String Question:
// Write JavaScript code to reverse a given string.
// Ex: world => dlrow
let string = "world";
// let string = "racecar";
let reverseString = string.split("").reverse().join("")
// console.log(reverseString)
// Palindrome Check:
//check a given string is a palindrome (reads the same forwards and backwards).
// Ex: racecar=> racecar; // Output: true
console.log(string === reverseString)
Q. No. 03/10:
const array1 = [1, 2, 3, 4, 5];
const array2 = [3, 4, 5, 6, 7];
// Output: [1, 2, 3, 4, 5, 6, 7]
const combinedArray = [...array1,...array2]
// console.log(combinedArray)
const removeDuplicates = Array.from(new Set(combinedArray))
console.log(removeDuplicates)
Q. No. 04/10:
const employees = [
{ id: 1, name: 'Mahesh', department: 'HR', salary: 50000 },
{ id: 2, name: 'Hema', department: 'Engineering', salary: 60000 },
{ id: 3, name: 'Maruthi', department: 'Marketing', salary: 55000 },
];
// Can you explain how to retrieve an employee by their ID,
// filter employees by department,
// and calculate the average salary of all employees using JavaScript?"
function getEmployeeById(id){
return employees.find(employee=>employee.id === id)
}
// console.log(getEmployeeById(1))
function getEmployeesByDepartment(department){
return employees.filter(employee=>employee.department === department)
}
// console.log(getEmployeesByDepartment("HR"))
const totalSalary = employees.reduce((total,employee)=>total+employee.salary, 0)
console.log(totalSalary)
const getAverageSalary = totalSalary/employees.length;
console.log(getAverageSalary)
Q. No 05/10:
// Could you explain how the formatDate function works, especially regarding the
usage of methods like getDate(), getMonth(), getFullYear(), toString(), and
padStart()?"
function formatDate(date){
const day = date.getDate().toString().padStart(2, "0")
const month = (date.getMonth() + 1).toString().padStart(2, "0")
const year = date.getFullYear()
return `${day}/${month}/${year}`
}
console.log(formatDate(new Date()))
Q. No 06/10:
// "Could you explain the functionality of the calculator function, including how
it processes addition, subtraction, multiplication, and division operations
using a switch statement?"
function calculator(operator, num1, num2) {
switch (operator) {
case '+':
return num1 + num2;
case '-':
return num1 - num2;
case '*':
return num1 * num2;
case '/':
return num1 / num2;
default:
return "Invalid operator";
}
}
console.log(calculator("+", 6,3))
// Output: 9
Q. No 07/10:
// check prime Number
function isPrime(num) {
if (num <= 1) {
return false;
}
for (let i = 2; i <= Math.sqrt(num); i++) {
if (num % i === 0) {
return false;
}
}
return true;
}
console.log(isPrime(2))
// Output: true
Q. No 08/10:
// Factorial
function factorial(n) {
if (n === 0 || n === 1) {
return 1;
}
return n * factorial(n - 1);
}
console.log(factorial(5))
//Output : 120
Q. No 09/10:
function printDiamond(height){
// Upper part of the diamond
for(let i=1; i<= height;i++){
let row = " ";
for(let j=1; j<= height-i;j++){
row = row + " "
}
for(let k = 1; k<= 2*i-1; k++){
row = row + "*"
}
console.log(row)
}
// Lower part of the diamond
for(let i=height-1; i>=1;i--){
let row = " ";
for(let j=1; j<= height-i;j++){
row = row + " "
}
for(let k = 1; k<= 2*i-1; k++){
row = row + "*"
}
console.log(row)
}
}
printDiamond(5)
Q. No 10/10:
let string = "hema coding school";
let capitalizeWords = string.split(" ").map(word=>word.toUpperCase().charAt()
+ word.slice(1)).join(" ")
console.log(capitalizeWords)
No comments:
Post a Comment