0% found this document useful (0 votes)
84 views2 pages

Leetcode Js

Uploaded by

Farjaad shah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
84 views2 pages

Leetcode Js

Uploaded by

Farjaad shah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

// increase the number of counter when ever the function call

let countNum = 1;

const counterIncrease = () =>{

return countNum++

}
[Link](counterIncrease());
[Link](counterIncrease());
[Link](counterIncrease());
[Link](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 === [Link] & val === [Link]){

return true
}
else {

return false
}

valCheck(["farjaad", 27])
[Link](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 ([Link] === expectedName && [Link] === 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


[Link](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 [Link]()
}
return {
increment,
decerement,
resetNum
}

const counter = createCounter();// if we dont use this

[Link]([Link]()); // Output: 1
[Link](createCounter().increment()); // Output: 1

You might also like