Sunday, December 17, 2023

Day 11 || Function in JavaScript || Object Methods in JavaScript

   JavaScript Topics 

Day 11 :


  • Function
  • Object Method

Function:

function addition(x,y){
    return x + y;
}
console.log(addition(2,5))

var addition = function(x,y){
    return x + y
}
console.log(addition(5,5))

var juiceMixer = function(fruit1,fruit2){
    return "Delicious " + fruit1 + " and " + fruit2 + " juice is ready"
}

console.log(juiceMixer("mango","strawberry"))
console.log(juiceMixer("apple","orange"))
console.log(juiceMixer("blueberry","orange"))


Object Method:


var magicalToolBox = {
    compartments:{
        screws:["wood screws", "sheet metal screws", "machine screws"],
        wires: ["red wires", "black wires", "green wires"],
        gears:["small gears - 12 teeth", "medium gears - 24 teeth", "large gears - 36 teeth"]
    },
   openToolBox:function(){
    return "The magical toolbox opens";
   },
   closeToolBox:function(){
    return "The magical toolbox closes"
   }
   
}
console.log(magicalToolBox.openToolBox())
console.log(magicalToolBox.closeToolBox())
console.log(magicalToolBox.compartments.gears)
console.log(Object.keys(magicalToolBox))
console.log(Object.values(magicalToolBox))
console.log(Object.entries(magicalToolBox))


Tasks:

Function Task1: Calculate Area of a Rectangle

Create a simple program to calculate the area of a rectangle using a function.

Define a function named calculateArea that takes two parameters, length and width.
Inside the function, calculate the area of the rectangle using the formula area = length * width.
Return the calculated area from the function.
Call the function with specific values for length and width, and print the result to the console.

Object Method Task2: Employee Information Management

Create a program to manage employee information using objects.

Define an object named employee that represents an employee in a company.
The employee object should have properties such as name, age, designation, and salary.
Implement a function within the object to update the employee's salary based on a percentage increase.
Create multiple instances of the employee object to represent different employees in the company.
Use the function to update the salary for at least one employee, and then print the updated information for that employee.

Interview Questions:

1. Explain the difference between function declarations and function expressions
2. What is the purpose of the Object.keys(), Object.values(), and Object.entries() methods?
3. What is the purpose of the hasOwnProperty() method?

No comments:

Post a Comment

Top 10 | JavaScript Coding Interview Question | Beginner Level

               JavaScript Coding Interview  Q. No. 01/10:  console . log ( "1" + "4" + "1" ) // 141 console . ...