// increase the number of counter when ever the function call
let countNum = 1;
const counterIncrease = () =>{
return countNum++
}
console.log(counterIncrease());
console.log(counterIncrease());
console.log(counterIncrease());
console.log(counterIncrease());
=============================================================================
check the value of the object if the is same so return true otherwise false.
let val = {
name:"farjaad",
age: 27
}
const valCheck = () =>{
if(val === val.name & val === val.age){
return true
}
else {
return false
}
valCheck(["farjaad", 27])
console.log(valCheck, "true")
correct version
let val = {
name: "farjaad",
age: 27
};
const valCheck = (expectedName, expectedAge) => {
// Check if the name and age match the expected values
if (val.name === expectedName && val.age === expectedAge) {
return true; // Return true if both conditions are met
} else {
return false; // Return false if the conditions are not met
}
};
// Call the function with the expected values
console.log(valCheck("farjaad", 27)); // Output: true
===================================================================================
=======
Write a function createCounter. It should accept an initial integer init. It should
return an object with three functions.
The three functions are:
increment() increases the current value by 1 and then returns it.
decrement() reduces the current value by 1 and then returns it.
reset() sets the current value to init and then returns it.
let initNum = 1
const createCounter = () =>{
let count = initNum
const increment = () =>{
return count++
}
const decerement = () =>{
return count--
}
const resetNum = () =>{
return count.reset()
}
return {
increment,
decerement,
resetNum
}
const counter = createCounter();// if we dont use this
console.log(counter.increment()); // Output: 1
console.log(createCounter().increment()); // Output: 1