// Online Javascript Editor for free
// Write, Edit and Run your Javascript code using JS Online Compiler
// function gift(callback) {
// const obj = {giftcard : "wellcome"}
// console.log("Most welcome in our restaurant",);
// menuList(orderFood, obj);
// }
// function menuList(callback, obj){
// obj = {...obj, menu : "menu list"}
// console.log("This are Menu list");
// orderFood(billing, obj);
// }
// function orderFood(callback, obj){
// obj = {...obj, food:"pasta"}
// console.log("please order a food");
// billing(review, obj)
// }
// function billing(callback, obj){
// obj = {...obj, bill : "450"}
// console.log("Your bill please");
// review(thankYou, obj)
// }
// function review(callback, obj){
// obj = {...obj, review : "Nice Food"}
// console.log("review regarding food");
// thankYou(obj);
// }
// function thankYou(obj){
// obj = {...obj, thankyou : "wellcome"}
// console.log("Thank you for your comming")
// console.log(obj);
// }
// gift(menuList)
// function gift(callback) {
// setTimeout(() => {
// console.log("Most welcome in our resotorent",);
// callback();
// },4000)
// }
// function menuList(callback){
// setTimeout(() => {
// console.log("This are Menu list", 1);
// callback();
// },2000)
// }
// function orderFood(callback){
// setTimeout(() => {
// console.log("please order a food", 2);
// callback()
// },1000)
// }
// function billing(callback){
// setTimeout(() => {
// console.log("Your bill please", 3);
// callback()
// }, 3000)
// }
// function review(callback){
// setTimeout(() => {
// console.log("review regarding food", 4);
// callback();
// },2000)
// }
// function thankYou(){
// setTimeout(() => {
// console.log("Thank you for your comming", 5)
// },100)
// }
// gift(() => {
// menuList(() => {
// orderFood(() => {
// billing(() => {
// review(() => {
// thankYou(() => {
// })
// })
// })
// })
// })
// })
// Assignment 1: Basic Callback Implementation
// Write a function greet(name, callback) that takes a name and a callback function as arguments. The function should log "Hello, {name}!" and then execute the callback function.
const greet = (name, callback) => {
callback(name);
}
const example = (name) => {
console.log("Hello", name)
}
greet("karan",example);
// Assignment 2: Callback with Parameters
// Write a function calculate(a, b, operation, callback) that takes two numbers (a and b), an operation function, and a callback function. The operation function should perform a mathematical operation (e.g., add, subtract, multiply, divide) on a and b. The result should be passed to the callback function, which logs the result.
const calculate = (a, b, operator, callback) => {
let res = 0;
if(operator === "add"){
res = a + b;
}else if(operator === "sub"){
res = a - b;
}else if(operator === "mul"){
res = a * b;
}else if(operator === "div"){
res = a / b;
}
callback(res);
}
const getResult = (res) => {
console.log("result", res)
}
calculate(10, 20, "sub", getResult );
// Assignment 3: Sequential Callbacks
// Write a function processTasks(tasks, callback) that takes an array of tasks (functions) and a callback. Each task should be executed one after the other, and the final callback should be called after all tasks are completed.
const task1 = (callback) => {
setTimeout(() => {
console.log("task 1")
callback();
}, 1000)
}
const task2 = (callback) => {
setTimeout(() => {
console.log("task 2")
callback();
},3000)
}
const task3 = (callback) => {
setTimeout(() => {
console.log("task 3")
callback();
}, 5000)
}
const task4 = (callback) => {
setTimeout(() => {
console.log("task 4")
}, 1000)
}
const allDone = () => {
console.log("all tasks are completed");
}
function processTasks(tasks, callback){
let index = 0;
const executeTask = () => {
if(index <= tasks.length){
const currentTask = tasks[index];
index++;
currentTask(executeTask);
}else{
callback();
}
}
executeTask()
}
processTasks([task1, task2, task3, task4], allDone);
// Assignment 4: Error Handling with Callbacks
// Write a function divide(a, b, callback) that divides a by b. If b is 0, the function should pass an error message to the callback. Otherwise, it should pass the result of the division.
const divide = (a, b, callback) => {
if(b === 0){
const error = "b should not be 0"
callback(error)
return;
}
console.log(a / b);
}
const errorMsg = (error) => {
console.log(error);
}
divide(10, 2, errorMsg);
// Assignment 5: Real-World Simulation (Restaurant Order System)
// Simulate a restaurant order system using callbacks. Write the following functions:
// takeOrder(order, callback) - Takes an order and calls the callback with the order.
// prepareFood(order, callback) - Prepares the food and calls the callback with the prepared food.
// serveFood(food, callback) - Serves the food and calls the callback to notify that the food is served.
// const takeOrder = (order, callback) => {
// setTimeout(() => {
// console.log("order of", order,"taken" );
// prepareFood(order);
// },2000)
// }
// const prepareFood = (order, callback) => {
// setTimeout(() => {
// console.log("Preparing the", order);
// serveFood(order);
// }, 5000)
// }
// const serveFood = (order, callback) => {
// setTimeout(() => {
// console.log("Serving the", order)
// served(order)
// }, 2000)
// }
// const served = (order) => {
// setTimeout(() => {
// console.log(order, "is served");
// }, 100)
// }
// takeOrder("Pasta", prepareFood )
const takeOrder = (order, callback) => {
setTimeout(() => {
console.log("order of", order,"taken" );
callback(order);
},2000)
}
const prepareFood = (order, callback) => {
setTimeout(() => {
console.log("Preparing the", order);
callback(order);
}, 5000)
}
const serveFood = (order, callback) => {
setTimeout(() => {
console.log("Serving the", order)
callback(order)
}, 2000)
}
const served = (order) => {
setTimeout(() => {
console.log(order, "is served");
}, 100)
}
takeOrder("pizza", (order) => {
prepareFood(order,(order) => {
serveFood(order,(order) => {
served(order)
})
})
})