0% found this document useful (0 votes)
6 views7 pages

Ui22cs59lab6 We

The document outlines Lab 6 of Web Engineering, focusing on implementing four essential JavaScript functions: debounce, sorting an array of objects by a key, deep cloning, and merging sorted arrays. Each function is accompanied by code examples and explanations of their functionality. The conclusion emphasizes the practical applications of these functions and the reinforcement of key programming concepts.

Uploaded by

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

Ui22cs59lab6 We

The document outlines Lab 6 of Web Engineering, focusing on implementing four essential JavaScript functions: debounce, sorting an array of objects by a key, deep cloning, and merging sorted arrays. Each function is accompanied by code examples and explanations of their functionality. The conclusion emphasizes the practical applications of these functions and the reinforcement of key programming concepts.

Uploaded by

Jugal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Web Engineering

Lab 6 :Essential JavaScript Functions: Debounce, Sorting, Cloning, and


Merging

Aim:- 1. Implement a debounce function in JavaScript that limits the frequency of a function’s execution
when it’s called repeatedly within a specified time frame.

2. Write a function that takes an array of objects and a key, and returns a new array sorted based on the
values of that key in ascending order.

3. Implement a deep clone function in JavaScript that creates a copy of a nested object or array without
any reference to the original.

4. Implement a function that takes two sorted arrays and merges them into a single sorted array without
using any built-in sorting functions.

Code :-

// 1. Debounce function

function debounce(func, delay) {

let timer;

return function (...args) {

clearTimeout(timer);

timer = setTimeout(() => func.apply(this, args), delay);

};
}

const logMessage = debounce((message) => console.log(message), 1000);

logMessage("Hello, World!");

// 2. Sort an array of objects by a key

function sortByKey(array, key) {

return array.slice().sort((a, b) => {

if (a[key] < b[key]) return -1;

if (a[key] > b[key]) return 1;

return 0;

});

const objectsArray = [

{ id: 3, name: "Alice" },


{ id: 1, name: "Bob" },

{ id: 2, name: "Charlie" }

];

console.log(sortByKey(objectsArray, "id"));

// 3. Deep clone function

function deepClone(obj) {

if (obj === null || typeof obj !== 'object') {

return obj;

if (Array.isArray(obj)) {

return obj.map(item => deepClone(item));

const clonedObj = {};


for (const key in obj) {

if (obj.hasOwnProperty(key)) {

clonedObj[key] = deepClone(obj[key]);

return clonedObj;

const nestedObject = { a: 1, b: { c: 2, d: [3, 4] } };

const clonedObject = deepClone(nestedObject);

console.log(clonedObject);

console.log(clonedObject === nestedObject);

// 4. Merge two sorted arrays


function mergeSortedArrays(arr1, arr2) {

const mergedArray = [];

let i = 0, j = 0;

while (i < arr1.length && j < arr2.length) {

if (arr1[i] < arr2[j]) {

mergedArray.push(arr1[i]);

i++;

} else {

mergedArray.push(arr2[j]);

j++;

}
while (i < arr1.length) {

mergedArray.push(arr1[i]);

i++;

while (j < arr2.length) {

mergedArray.push(arr2[j]);

j++;

return mergedArray;

const sortedArray1 = [1, 3, 5];

const sortedArray2 = [2, 4, 6];


console.log(mergeSortedArrays(sortedArray1, sortedArray2));

Output :-

Conclusion :- In this lab, we successfully implemented and tested four essential JavaScript functions:
debounce, sorting by key, deep cloning, and merging sorted arrays. These functions demonstrate practical
applications of closures, recursion, and algorithmic problem-solving. By completing this, we reinforced
key programming concepts and their real-world utility.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy