├── resolvepromise.js ├── Json.js ├── filterrow.js ├── deserilizejsonstring.js ├── setInterval.js ├── highlight.js ├── singledeep.js ├── falsy.js ├── insertionsort.js ├── tablehtml.js ├── squashobj.js ├── singleReadstring.js ├── memoziedsingle.js ├── mergerows.js ├── debounce.js ├── recursivevalue.js ├── memoized.js ├── deepcopy.js ├── keysuppercase.js ├── Nasynctask.js ├── promisesettled.js ├── deepequal.js ├── Promiseany.js ├── turtle2d.js ├── promiseOverride.js └── resumableInterval.js /resolvepromise.js: -------------------------------------------------------------------------------- 1 | // Implement a function to resolve a given value to a Promise 2 | 3 | function resolveToPromise(value) { 4 | return new Promise((resolve) => { 5 | resolve(value); 6 | }); 7 | } 8 | 9 | // Example usage: 10 | const resolvedPromise = resolveToPromise("Resolved Value"); 11 | 12 | resolvedPromise.then((result) => { 13 | console.log("Resolved:", result); 14 | }); 15 | -------------------------------------------------------------------------------- /Json.js: -------------------------------------------------------------------------------- 1 | function convertToJSON(value) { 2 | try { 3 | const jsonString = JSON.stringify(value); 4 | return jsonString; 5 | } catch (error) { 6 | console.log("Error converting to json", error); 7 | return null; 8 | } 9 | } 10 | 11 | const myObject1 = { key1: "value1", key2: "value2", key3: ["a,b,c"] }; 12 | const jsonString = convertToJSON(myObject1); 13 | console.log(jsonString); 14 | -------------------------------------------------------------------------------- /filterrow.js: -------------------------------------------------------------------------------- 1 | const data = [ 2 | { id: 1, name: "Alice", age: 25 }, 3 | { id: 2, name: "Bob", age: 30 }, 4 | { id: 3, name: "Charlie", age: 22 }, 5 | { id: 4, name: "David", age: 35 }, 6 | ]; 7 | function filterRows(data, condition) { 8 | return data.filter((row) => condition(row)); 9 | } 10 | //exmaple 11 | // Filtering rows where age is greater than 25 12 | const filteredData = filterRows(data, (row) => row.age > 25); 13 | console.log(filteredData); 14 | -------------------------------------------------------------------------------- /deserilizejsonstring.js: -------------------------------------------------------------------------------- 1 | // Implement a function to deserialize a JSON string. 2 | function deserializeJSON(jsonString) { 3 | try { 4 | const parsedObject = JSON.parse(jsonString); 5 | return parsedObject; 6 | } catch (error) { 7 | console.error("Error while deserializing JSON:", error); 8 | return null; 9 | } 10 | } 11 | 12 | // Example usage: 13 | const jsonString = '{"name": "John", "age": 30, "city": "New York"}'; 14 | const deserializedObject = deserializeJSON(jsonString); 15 | console.log(deserializedObject); 16 | -------------------------------------------------------------------------------- /setInterval.js: -------------------------------------------------------------------------------- 1 | // Implement a function that acts like setInterval but returns a function to cancel the Interval. 2 | function customSetInterval(callback, interval) { 3 | const intervalId = setInterval(callback, interval); 4 | 5 | const cancel = () => { 6 | clearInterval(intervalId); 7 | }; 8 | 9 | return cancel; 10 | } 11 | 12 | function sayHello() { 13 | console.log("Hello!"); 14 | } 15 | 16 | const cancelInterval = customSetInterval(sayHello, 1000); 17 | 18 | setTimeout(() => { 19 | cancelInterval(); 20 | }, 5000); 21 | -------------------------------------------------------------------------------- /highlight.js: -------------------------------------------------------------------------------- 1 | // Implement a function to highlight text if searched terms appear within it. 2 | 3 | function highlightText(text, searchTerm) { 4 | const regex = new RegExp(searchTerm, "gi"); 5 | return text.replace( 6 | regex, 7 | (match) => `${match}` 8 | ); 9 | } 10 | 11 | // Example usage: 12 | const originalText = "This is a sample text to highlight certain words."; 13 | const searchTerm = "highlight"; 14 | 15 | const highlightedText = highlightText(originalText, searchTerm); 16 | console.log(highlightedText); 17 | -------------------------------------------------------------------------------- /singledeep.js: -------------------------------------------------------------------------------- 1 | // Implement a function that recursively flattens an array into a single level deep. 2 | function flattenArray(arr) { 3 | const flattened = []; 4 | 5 | arr.forEach((item) => { 6 | if (Array.isArray(item)) { 7 | flattened.push(...flattenArray(item)); 8 | } else { 9 | flattened.push(item); 10 | } 11 | }); 12 | 13 | return flattened; 14 | } 15 | 16 | // Example usage: 17 | const nestedArray = [1, [2, [3, 4], 5], 6, [7, 8]]; 18 | const flattenedArray = flattenArray(nestedArray); 19 | console.log(flattenedArray); 20 | -------------------------------------------------------------------------------- /falsy.js: -------------------------------------------------------------------------------- 1 | // Implement a function that returns an object with all falsey values removed. 2 | function removeFalseyValues(obj) { 3 | const newObj = {}; 4 | 5 | for (const key in obj) { 6 | if (obj[key]) { 7 | newObj[key] = obj[key]; 8 | } 9 | } 10 | 11 | return newObj; 12 | } 13 | 14 | // Example usage: 15 | const myObject = { 16 | a: 0, 17 | b: false, 18 | c: "", 19 | d: 42, 20 | e: "Hello", 21 | f: null, 22 | g: undefined, 23 | }; 24 | 25 | const filteredObject = removeFalseyValues(myObject); 26 | console.log(filteredObject); 27 | -------------------------------------------------------------------------------- /insertionsort.js: -------------------------------------------------------------------------------- 1 | function insertionSort(arr) { 2 | const length = arr.length; 3 | 4 | for (let i = 1; i < length; i++) { 5 | let current = arr[i]; 6 | let j = i - 1; 7 | 8 | while (j >= 0 && arr[j] > current) { 9 | arr[j + 1] = arr[j]; 10 | j--; 11 | } 12 | 13 | arr[j + 1] = current; 14 | } 15 | 16 | return arr; 17 | } 18 | 19 | // Example usage: 20 | const arrayToSort = [5, 2, 4, 6, 1, 3]; 21 | console.log("Original Array:", arrayToSort); 22 | const sortedArray = insertionSort(arrayToSort); 23 | console.log("Sorted Array:", sortedArray); 24 | -------------------------------------------------------------------------------- /tablehtml.js: -------------------------------------------------------------------------------- 1 | // Implement a function to construct a table of contents from an HTML document. 2 | 3 | function generateTableOfContents() { 4 | const headings = document.querySelectorAll("h1, h2, h3, h4, h5, h6"); 5 | const tableOfContents = []; 6 | 7 | headings.forEach((heading) => { 8 | const headingText = heading.textContent; 9 | const headingLevel = parseInt(heading.tagName.charAt(1)); 10 | 11 | tableOfContents.push({ 12 | text: headingText, 13 | level: headingLevel, 14 | }); 15 | }); 16 | 17 | return tableOfContents; 18 | } 19 | 20 | // Example usage: 21 | const toc = generateTableOfContents(); 22 | console.log(toc); 23 | -------------------------------------------------------------------------------- /squashobj.js: -------------------------------------------------------------------------------- 1 | function squashObject(obj, prefix = "") { 2 | const result = {}; 3 | 4 | for (const key in obj) { 5 | if (typeof obj[key] === "object" && obj[key] !== null) { 6 | const nestedObj = squashObject(obj[key], `${prefix}${key}.`); 7 | Object.assign(result, nestedObj); 8 | } else { 9 | result[`${prefix}${key}`] = obj[key]; 10 | } 11 | } 12 | 13 | return result; 14 | } 15 | 16 | // Example usage: 17 | const nestedObj = { 18 | a: 1, 19 | b: { 20 | c: 2, 21 | d: { 22 | e: 3, 23 | }, 24 | }, 25 | f: 4, 26 | }; 27 | 28 | const squashedObj = squashObject(nestedObj); 29 | console.log(squashedObj); 30 | -------------------------------------------------------------------------------- /singleReadstring.js: -------------------------------------------------------------------------------- 1 | // Implement a function that formats a list of items into a single readable string. 2 | function formatList(items) { 3 | if (!Array.isArray(items)) { 4 | return "Invalid input. Expected an array."; 5 | } 6 | 7 | if (items.length === 0) { 8 | return "The list is empty."; 9 | } 10 | 11 | const formattedString = items.join(", "); 12 | return `List: ${formattedString}`; 13 | } 14 | 15 | // Example usage: 16 | const list1 = ["apple", "banana", "orange"]; 17 | const list2 = []; // Empty list 18 | const notAList = "This is not an array"; 19 | 20 | console.log(formatList(list1)); 21 | console.log(formatList(list2)); 22 | console.log(formatList(notAList)); 23 | -------------------------------------------------------------------------------- /memoziedsingle.js: -------------------------------------------------------------------------------- 1 | // Implement a function that returns a memoized version of a function which accepts a single argument. 2 | 3 | function memoize(fn) { 4 | const cache = new Map(); 5 | 6 | return function (arg) { 7 | if (cache.has(arg)) { 8 | return cache.get(arg); 9 | } 10 | 11 | const result = fn(arg); 12 | cache.set(arg, result); 13 | return result; 14 | }; 15 | } 16 | 17 | // Example usage: 18 | function square(x) { 19 | console.log("Calculating square..."); 20 | return x * x; 21 | } 22 | 23 | const memoizedSquare = memoize(square); 24 | 25 | console.log(memoizedSquare(5)); 26 | console.log(memoizedSquare(5)); 27 | console.log(memoizedSquare(7)); 28 | console.log(memoizedSquare(7)); 29 | -------------------------------------------------------------------------------- /mergerows.js: -------------------------------------------------------------------------------- 1 | // Implement a function to merge rows of data from the same user. 2 | function mergeUserData(data) { 3 | const mergedData = {}; 4 | 5 | data.forEach((row) => { 6 | const userId = row.userId; 7 | 8 | if (!mergedData[userId]) { 9 | mergedData[userId] = { ...row }; 10 | } else { 11 | mergedData[userId] = { ...mergedData[userId], ...row }; 12 | } 13 | }); 14 | 15 | return Object.values(mergedData); 16 | } 17 | 18 | // Example usage: 19 | const userData = [ 20 | { userId: 1, name: "Alice", age: 25 }, 21 | { userId: 2, name: "Bob", age: 30 }, 22 | { userId: 1, city: "New York" }, 23 | { userId: 2, city: "San Francisco" }, 24 | ]; 25 | 26 | const mergedUserData = mergeUserData(userData); 27 | console.log(mergedUserData); 28 | -------------------------------------------------------------------------------- /debounce.js: -------------------------------------------------------------------------------- 1 | // Implement a debounce function that comes with a cancel method to cancel delayed invocations. 2 | 3 | function debounce(callback, delay) { 4 | let timeoutId; 5 | 6 | function cancel() { 7 | clearTimeout(timeoutId); 8 | } 9 | 10 | function debounced(...args) { 11 | cancel(); // Clear previous timeout 12 | timeoutId = setTimeout(() => { 13 | callback(...args); 14 | }, delay); 15 | } 16 | 17 | debounced.cancel = cancel; 18 | 19 | return debounced; 20 | } 21 | 22 | // Example usage: 23 | function greet(name) { 24 | console.log(`Hello, ${name}!`); 25 | } 26 | 27 | const delayedGreet = debounce(greet, 500); 28 | 29 | delayedGreet("Alice"); 30 | delayedGreet("Bob"); 31 | delayedGreet.cancel(); 32 | 33 | delayedGreet("Charlie"); 34 | -------------------------------------------------------------------------------- /recursivevalue.js: -------------------------------------------------------------------------------- 1 | // Implement a function to recursively transform values. 2 | function recursivelyTransform(obj) { 3 | if (typeof obj === "number") { 4 | return obj * 2; 5 | } else if (Array.isArray(obj)) { 6 | return obj.map((item) => recursivelyTransform(item)); 7 | } else if (typeof obj === "object" && obj !== null) { 8 | const transformedObj = {}; 9 | for (const key in obj) { 10 | transformedObj[key] = recursivelyTransform(obj[key]); 11 | } 12 | return transformedObj; 13 | } else { 14 | return obj; 15 | } 16 | } 17 | 18 | // Example usage: 19 | const data = { 20 | a: 1, 21 | b: [2, 3, { c: 4 }], 22 | d: { 23 | e: 5, 24 | f: [6, 7], 25 | }, 26 | }; 27 | 28 | const transformedData = recursivelyTransform(data); 29 | console.log(transformedData); 30 | -------------------------------------------------------------------------------- /memoized.js: -------------------------------------------------------------------------------- 1 | // Implement a function that returns a memoized version of a function which accepts any number of arguments. 2 | function memoize(func) { 3 | const cache = new Map(); 4 | 5 | return function (...args) { 6 | const key = args.join("-"); 7 | 8 | if (cache.has(key)) { 9 | return cache.get(key); 10 | } 11 | 12 | const result = func(...args); 13 | cache.set(key, result); 14 | return result; 15 | }; 16 | } 17 | 18 | // Example usage: 19 | function sum(...args) { 20 | console.log("Calculating sum..."); 21 | return args.reduce((acc, val) => acc + val, 0); 22 | } 23 | 24 | const memoizedSum = memoize(sum); 25 | 26 | console.log(memoizedSum(1, 2, 3)); 27 | console.log(memoizedSum(1, 2, 3)); 28 | console.log(memoizedSum(2, 3, 4)); 29 | console.log(memoizedSum(1, 2, 4)); 30 | -------------------------------------------------------------------------------- /deepcopy.js: -------------------------------------------------------------------------------- 1 | // Implement a function that performs a deep copy of a value, but also handles circular references. 2 | 3 | function deepCopyWithCircular(obj, visited = new Map()) { 4 | if (obj === null || typeof obj !== "object") { 5 | return obj; 6 | } 7 | 8 | if (visited.has(obj)) { 9 | return visited.get(obj); 10 | } 11 | 12 | const newObj = Array.isArray(obj) ? [] : {}; 13 | 14 | visited.set(obj, newObj); 15 | 16 | for (const key in obj) { 17 | if (Object.prototype.hasOwnProperty.call(obj, key)) { 18 | newObj[key] = deepCopyWithCircular(obj[key], visited); 19 | } 20 | } 21 | 22 | return newObj; 23 | } 24 | 25 | // Example usage: 26 | const obj = { 27 | a: 1, 28 | b: { 29 | c: 2, 30 | }, 31 | }; 32 | obj.circularRef = obj; 33 | 34 | const copiedObj = deepCopyWithCircular(obj); 35 | console.log(copiedObj); 36 | -------------------------------------------------------------------------------- /keysuppercase.js: -------------------------------------------------------------------------------- 1 | // Implement a function to convert all the keys in an object to camel case. 2 | function keysToCamelCase(obj) { 3 | if (typeof obj !== "object" || obj === null) { 4 | return obj; 5 | } 6 | 7 | const camelCaseObj = {}; 8 | 9 | for (const key in obj) { 10 | if (Object.prototype.hasOwnProperty.call(obj, key)) { 11 | const camelCaseKey = key.replace(/_([a-z])/g, (_, letter) => 12 | letter.toUpperCase() 13 | ); 14 | camelCaseObj[camelCaseKey] = keysToCamelCase(obj[key]); 15 | } 16 | } 17 | 18 | return camelCaseObj; 19 | } 20 | 21 | // Example usage: 22 | const snakeCaseObj = { 23 | first_name: "John", 24 | last_name: "Doe", 25 | contact_info: { 26 | email_address: "john@example.com", 27 | phone_number: "123-456-7890", 28 | }, 29 | }; 30 | 31 | const camelCaseObj = keysToCamelCase(snakeCaseObj); 32 | console.log(camelCaseObj); 33 | -------------------------------------------------------------------------------- /Nasynctask.js: -------------------------------------------------------------------------------- 1 | // Implement a function to execute N async tasks in series. 2 | 3 | async function executeTasksInSeries(tasks) { 4 | for (const task of tasks) { 5 | await task(); 6 | } 7 | } 8 | 9 | // Example usage: 10 | const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms)); 11 | 12 | const task1 = async () => { 13 | await delay(1000); 14 | console.log("Task 1 executed"); 15 | }; 16 | 17 | const task2 = async () => { 18 | await delay(1500); 19 | console.log("Task 2 executed"); 20 | }; 21 | 22 | const task3 = async () => { 23 | await delay(500); 24 | console.log("Task 3 executed"); 25 | }; 26 | 27 | // Array of async tasks 28 | const tasksArray = [task1, task2, task3]; 29 | 30 | // Execute tasks in series 31 | executeTasksInSeries(tasksArray) 32 | .then(() => { 33 | console.log("All tasks completed"); 34 | }) 35 | .catch((err) => { 36 | console.error("Error:", err); 37 | }); 38 | -------------------------------------------------------------------------------- /promisesettled.js: -------------------------------------------------------------------------------- 1 | // Implement the functionality behaviour of Promise.allSettled() 2 | function promiseAllSettled(promises) { 3 | return Promise.all( 4 | promises.map((promise) => 5 | Promise.resolve(promise) 6 | .then((value) => ({ status: "fulfilled", value })) 7 | .catch((reason) => ({ status: "rejected", reason })) 8 | ) 9 | ); 10 | } 11 | 12 | // Example usage: 13 | const promise1 = new Promise((resolve) => 14 | setTimeout(resolve, 1000, "Promise 1 resolved") 15 | ); 16 | const promise2 = new Promise((_, reject) => 17 | setTimeout(reject, 500, "Promise 2 rejected") 18 | ); 19 | const promise3 = new Promise((resolve) => 20 | setTimeout(resolve, 1500, "Promise 3 resolved") 21 | ); 22 | 23 | promiseAllSettled([promise1, promise2, promise3]) 24 | .then((results) => { 25 | console.log("Results:", results); 26 | }) 27 | .catch((error) => { 28 | console.error("Error:", error); 29 | }); 30 | -------------------------------------------------------------------------------- /deepequal.js: -------------------------------------------------------------------------------- 1 | // Implement a function that determines if two values are deep equal 2 | function deepEqual(val1, val2) { 3 | if (val1 === val2) { 4 | return true; 5 | } 6 | 7 | if ( 8 | typeof val1 !== "object" || 9 | val1 === null || 10 | typeof val2 !== "object" || 11 | val2 === null 12 | ) { 13 | return false; 14 | } 15 | 16 | const keys1 = Object.keys(val1); 17 | const keys2 = Object.keys(val2); 18 | 19 | if (keys1.length !== keys2.length) { 20 | return false; 21 | } 22 | 23 | for (const key of keys1) { 24 | if (!val2.hasOwnProperty(key) || !deepEqual(val1[key], val2[key])) { 25 | return false; 26 | } 27 | } 28 | 29 | return true; 30 | } 31 | 32 | // Example usage: 33 | const obj1 = { a: 1, b: { c: 2 } }; 34 | const obj2 = { a: 1, b: { c: 2 } }; 35 | const obj3 = { a: 1, b: { c: 3 } }; 36 | 37 | console.log(deepEqual(obj1, obj2)); // Output: true 38 | console.log(deepEqual(obj1, obj3)); // Output: false 39 | -------------------------------------------------------------------------------- /Promiseany.js: -------------------------------------------------------------------------------- 1 | function promiseAny(promises) { 2 | return new Promise((resolve, reject) => { 3 | let errors = []; 4 | 5 | for (const promise of promises) { 6 | Promise.resolve(promise) 7 | .then((result) => { 8 | resolve(result); // Resolve with the first resolved promise's value 9 | }) 10 | .catch((error) => { 11 | errors.push(error); // Collect errors if all promises are rejected 12 | 13 | if (errors.length === promises.length) { 14 | reject(new AggregateError("All promises were rejected", errors)); 15 | } 16 | }); 17 | } 18 | }); 19 | } 20 | 21 | // Example usage: 22 | const promise1 = new Promise((resolve) => setTimeout(resolve, 100, "one")); 23 | const promise2 = new Promise((_, reject) => setTimeout(reject, 200, "two")); 24 | const promise3 = new Promise((resolve) => setTimeout(resolve, 300, "three")); 25 | 26 | promiseAny([promise1, promise2, promise3]) 27 | .then((result) => { 28 | console.log("Resolved:", result); // Output: Resolved: one 29 | }) 30 | .catch((error) => { 31 | console.log("Rejected:", error); // This will not execute in this example 32 | }); 33 | -------------------------------------------------------------------------------- /turtle2d.js: -------------------------------------------------------------------------------- 1 | // Implement a Turtle class that moves a turtle on a 2D plane. 2 | class Turtle { 3 | constructor() { 4 | this.x = 0; // Initial x-coordinate 5 | this.y = 0; // Initial y-coordinate 6 | this.angle = 0; // Initial angle (in degrees) 7 | } 8 | 9 | // Move the turtle forward by a certain distance 10 | forward(distance) { 11 | const radians = (this.angle * Math.PI) / 180; // Convert angle to radians 12 | const deltaX = distance * Math.cos(radians); 13 | const deltaY = distance * Math.sin(radians); 14 | this.x += deltaX; 15 | this.y += deltaY; 16 | } 17 | 18 | right(angle) { 19 | this.angle -= angle; 20 | } 21 | 22 | left(angle) { 23 | this.angle += angle; 24 | } 25 | 26 | setPosition(x, y) { 27 | this.x = x; 28 | this.y = y; 29 | } 30 | 31 | getPosition() { 32 | return { x: this.x, y: this.y }; 33 | } 34 | 35 | getAngle() { 36 | return this.angle; 37 | } 38 | } 39 | 40 | // Example usage: 41 | const myTurtle = new Turtle(); 42 | 43 | myTurtle.forward(50); 44 | myTurtle.right(90); 45 | myTurtle.forward(30); 46 | s; 47 | 48 | console.log(myTurtle.getPosition()); 49 | console.log(myTurtle.getAngle()); 50 | -------------------------------------------------------------------------------- /promiseOverride.js: -------------------------------------------------------------------------------- 1 | // Implement a promisify function that allows the original function to override the return value. 2 | 3 | function promisify(fn) { 4 | return function (...args) { 5 | return new Promise((resolve, reject) => { 6 | const originalReturnValue = fn(...args); 7 | 8 | if (originalReturnValue instanceof Promise) { 9 | originalReturnValue.then(resolve).catch(reject); 10 | } else { 11 | resolve(originalReturnValue); 12 | } 13 | }); 14 | }; 15 | } 16 | 17 | // Example usage: 18 | function syncFunction(arg) { 19 | return arg * 2; 20 | } 21 | 22 | function asyncFunction(arg) { 23 | return new Promise((resolve) => { 24 | setTimeout(() => { 25 | resolve(arg * 3); 26 | }, 1000); 27 | }); 28 | } 29 | 30 | const promisifiedSyncFunction = promisify(syncFunction); 31 | const promisifiedAsyncFunction = promisify(asyncFunction); 32 | 33 | // Using the promisified functions 34 | promisifiedSyncFunction(5) 35 | .then((result) => { 36 | console.log("Sync Function Result:", result); // Output: 10 37 | }) 38 | .catch((err) => { 39 | console.error("Error:", err); 40 | }); 41 | 42 | promisifiedAsyncFunction(7) 43 | .then((result) => { 44 | console.log("Async Function Result:", result); 45 | }) 46 | .catch((err) => { 47 | console.error("Error:", err); 48 | }); 49 | -------------------------------------------------------------------------------- /resumableInterval.js: -------------------------------------------------------------------------------- 1 | // Implement a function that creates a resumable interval object. 2 | 3 | function ResumableInterval(callback, interval) { 4 | let timerId; 5 | let startTime; 6 | let elapsedTime = 0; 7 | let isPaused = false; 8 | 9 | this.start = function () { 10 | if (isPaused) { 11 | startTime = Date.now() - elapsedTime; 12 | timerId = setTimeout(runCallback, interval - elapsedTime); 13 | } else { 14 | startTime = Date.now(); 15 | timerId = setTimeout(runCallback, interval); 16 | } 17 | isPaused = false; 18 | }; 19 | 20 | this.pause = function () { 21 | clearTimeout(timerId); 22 | elapsedTime = Date.now() - startTime; 23 | isPaused = true; 24 | }; 25 | 26 | this.stop = function () { 27 | clearTimeout(timerId); 28 | elapsedTime = 0; 29 | isPaused = false; 30 | }; 31 | 32 | const runCallback = () => { 33 | callback(); 34 | this.start(); 35 | }; 36 | 37 | this.start(); 38 | } 39 | 40 | // Example usage: 41 | function myFunction() { 42 | console.log("Interval callback executed!"); 43 | } 44 | 45 | const interval = new ResumableInterval(myFunction, 1000); 46 | 47 | // Pause after 3 seconds 48 | setTimeout(() => { 49 | interval.pause(); 50 | console.log("Interval paused after 3 seconds"); 51 | }, 3000); 52 | 53 | // Resume after 2 seconds 54 | setTimeout(() => { 55 | interval.start(); 56 | console.log("Interval resumed after 2 seconds"); 57 | }, 5000); 58 | 59 | // Stop after 5 seconds 60 | setTimeout(() => { 61 | interval.stop(); 62 | console.log("Interval stopped after 5 seconds"); 63 | }, 8000); 64 | --------------------------------------------------------------------------------