Advanced Web Development 202403103520032
Practical 1
Aim: Write a JavaScript to demonstrate properties and methods of
String reference Type.
// stringDemo.js
// Introduction to String Reference Type in JavaScript console.log("Demonstrating
String Properties and Methods in JavaScript");
// Example of String Properties let exampleString = "Hello, World!";
console.log("Original String: " + exampleString); console.log("Length of
String: " + exampleString.length); // Property: length
// Example of String Methods console.log("Character at index 0: " +
exampleString.charAt(0)); // Method: charAt() console.log("Index of 'World': " +
exampleString.indexOf("World")); // Method: indexOf() console.log("Substring (slice): "
+ exampleString.slice(0, 5)); // Method: slice() console.log("Uppercase: " +
exampleString.toUpperCase()); // Method: toUpperCase() console.log("Lowercase: " +
exampleString.toLowerCase()); // Method: toLowerCase() console.log("Includes 'Hello':
" + exampleString.includes("Hello")); // Method: includes() console.log("Split into array:
" + exampleString.split(", ")); // Method: split()
Output:
Advanced Web Development 202403103520032
Practical 2
Aim: Write a JavaScript to demonstrate properties and methods of Array
Collection Reference Type.
// arrayDemo.js
// Introduction to Array Reference Type in JavaScript console.log("Demonstrating
Array Properties and Methods in JavaScript");
// Example of Array Properties let exampleArray = [1, 2, 3, 4, 5];
console.log("Original Array: " + exampleArray); console.log("Length of
Array: " + exampleArray.length); // Property: length
// Example of Array Methods exampleArray.push(6); //
Method: push() console.log("After push(6): " +
exampleArray); exampleArray.pop(); // Method: pop()
console.log("After pop(): " + exampleArray);
exampleArray.shift(); // Method: shift() console.log("After
shift(): " + exampleArray); exampleArray.unshift(0); //
Method: unshift() console.log("After unshift(0): " +
exampleArray); let slicedArray = exampleArray.slice(1, 3);
// Method: slice() console.log("Sliced Array (1, 3): " +
slicedArray); exampleArray.splice(1, 1, 10); // Method:
splice() console.log("After splice(1, 1, 10): " +
exampleArray);
exampleArray.forEach((element) => console.log("Element: " + element)); // Method:
forEach() let mappedArray = exampleArray.map(x => x * 2); // Method: map()
console.log("Mapped Array (x * 2): " + mappedArray); let filteredArray =
exampleArray.filter(x => x > 2); // Method: filter() console.log("Filtered Array (x > 2):
" + filteredArray);
Output:
Advanced Web Development 202403103520032
Advanced Web Development 202403103520032
Practical 3
Aim: Write a JavaScript to demonstrate properties and methods of Date
reference Type.
// dateDemo.js
// Introduction to Date Reference Type in JavaScript console.log("Demonstrating
Date Properties and Methods in JavaScript");
// Create a new Date object let currentDate
= new Date(); console.log("Current Date: "
+ currentDate);
// Example of Date Methods console.log("Date: " + currentDate.getDate()); //
Method: getDate() console.log("Day of the week (0-6): " + currentDate.getDay());
// Method: getDay() console.log("Full Year: " + currentDate.getFullYear()); //
Method: getFullYear() console.log("Month (0-11): " + currentDate.getMonth()); //
Method: getMonth()
console.log("Time in milliseconds since Jan 1, 1970: " + currentDate.getTime()); //
Method: getTime() // Set new values currentDate.setDate(15); // Method: setDate()
console.log("Updated Date: " + currentDate); currentDate.setFullYear(2025); // Method:
setFullYear() console.log("Updated Year: " + currentDate);
Output:
Advanced Web Development 202403103520032
Practical 4
Aim: Write a JavaScript to create a cart contains items of categories like
groceries, apparels, accessories and gadgets. Each category offers
discounts 10%, 20%, 5% and 50% discounts. Apply discount on the
times present in the cart and generate a Uinal bill. [Hint: Use objects
containing key-value pair]
// shoppingCart.js
// Introduction to Shopping Cart with Discounts
console.log("Shopping Cart with Discounts");
// Define categories with items and their prices
const categories = { groceries: { items:
{ apple: 1.00, bread: 2.00,
milk: 1.50},
discount: 0.10 // 10% discount },
apparels: {
items: { shirt:
20.00, jeans:40.00,
jacket: 60.00 },
discount: 0.20 // 20% discount },
accessories: { items:
{ watch: 100.00,
sunglasses: 50.00 },
discount: 0.05 // 5% discount },
gadgets: {
items: { phone:
500.00, tablet:
300.00 },
discount: 0.50 // 50% discount }};
Advanced Web Development 202403103520032
// Create a cart object to hold selected items
let cart = { groceries: ['apple', 'milk'],
apparels: ['shirt'], accessories: ['watch'],
gadgets: ['phone'] };
// Function to calculate total bill after applying discounts
function calculateTotal(cart) { let total = 0;
for (const category in cart) { if (categories[category]) {
cart[category].forEach(item => { if
(categories[category].items[item]) { const price =
categories[category].items[item]; const discount
= categories[category].discount; const
discountedPrice = price - (price * discount); total
+= discountedPrice; }});}
} return
total; }
// Generate and display the final bill const totalBill =
calculateTotal(cart); console.log("Total Bill after discounts: $"
+ totalBill.toFixed(2));
Output:
Advanced Web Development 202403103520032
Practical 5
Aim: Create a regular expression to find pattern from the given text as
follow:
1) All the words starting with 'A'.
2) All the words starting with consonants.
// regexPatterns.js
// Sample text to search for patterns
const sampleText = "Apples are amazing. Bananas are better than apples. Aardvark is an
animal.";
// Regular expression to find all words starting with 'A' const
wordsStartingWithA = sampleText.match(/\bA\w*/g);
console.log("Words starting with 'A':", wordsStartingWithA); //
Regular expression to find all words starting with consonants
const wordsStartingWithConsonants =
sampleText.match(/\b[BCDFGHJKLMNPQRSTVWXYZ]\w*/g);
console.log("Words starting with consonants:", wordsStartingWithConsonants);
Output:
Advanced Web Development 202403103520084
Practical 6
Aim: Take input from user through prompt box and store it in an array.
Print the Uibonacci series till the indexed number in the array.
// fibonacciSeries.js
// Function to generate Fibonacci series up to n
function fibonacci(n) { let fibSeries = [0, 1];
for (let i = 2; i < n; i++) { fibSeries[i] =
fibSeries[i - 1] + fibSeries[i - 2];
return fibSeries.slice(0, n);
// Taking input from the user through readline
const readline = require('readline'); const rl =
readline.createInterface({ input:
process.stdin, output: process.stdout
});
rl.question("Enter the number of terms for Fibonacci series (comma-separated): ",
(userInput) => { let inputArray = userInput.split(',').map(Number); // Convert input to an
array of numbers
// Print Fibonacci series for each indexed number in the array
inputArray.forEach(index => { console.log(`Fibonacci series up to
index ${index}:`, fibonacci(index)); });
rl.close(); // Close the readline interface
Advanced Web Development 202403103520132
});
Output:
Advanced Web Development 202403103520084
Practical 7
Aim:Take input from user through prompt box and store it in an array.
Calculate the Factorial of each number present in the array using
iterative method of array and generate the resultant array of factorial
Values.
// factorialCalculator.js
// Function to calculate factorial iteratively
function factorialIterative(n) { let result
= 1; for (let i = 2; i <= n; i++) {
result *= i;
return result;
// Function to calculate factorial recursively
function factorialRecursive(n) { if (n ===
0 || n === 1) { return 1;
return n * factorialRecursive(n - 1);
// Taking input from the user through readline
const readline = require('readline'); const rl =
readline.createInterface({ input:
process.stdin, output: process.stdout
});
rl.question("Enter numbers to calculate factorial (comma-separated): ", (userInput) => {
let inputArray = userInput.split(',').map(Number); // Convert input to an array of numbers
Advanced Web Development 202403103520132
// Calculate factorial for each number using iterative method let
factorialsIterative = inputArray.map(num => factorialIterative(num));
console.log("Factorials (Iterative):", factorialsIterative); // Calculate
factorial for each number using recursive method let factorialsRecursive =
inputArray.map(num => factorialRecursive(num)); console.log("Factorials
(Recursive):", factorialsRecursive); rl.close(); // Close the readline
interface
});
Output:
Advanced Web Development 202403103520084
Practical 8
Aim:a) Write a demonstrative JavaScript for i) Function
methods,ii)Function internals
b) Write a JavaScript to implement function overloading using function
internals
// functionDemonstration.js
// Demonstrating Function Methods // Function
to demonstrate 'call' method function
greet(greeting) { console.log(`${greeting},
${this.name}`); const person = { name: 'Alice'
}; greet.call(person, 'Hello'); // Output: Hello,
Alice // Function to demonstrate 'apply' method
function sum(a, b) { return a + b;
const numbers = [5, 10]; console.log("Sum using apply:", sum.apply(null, numbers)); //
Output: Sum using apply: 15
// Function to demonstrate 'bind' method
function multiply(factor) { return
this.value * factor;
const obj = { value: 10 }; const multiplyByTwo = multiply.bind(obj, 2);
console.log("Multiply using bind:", multiplyByTwo()); // Output: Multiply using bind:
20
// Demonstrating Function Internals
// Example of closure
function makeCounter() {
Advanced Web Development 202403103520132
let count = 0; return
function() {
count++; return
count;
};
const counter = makeCounter(); console.log("Counter:",
counter()); // Output: Counter: 1 console.log("Counter:",
counter()); // Output: Counter: 2
// Example of 'arguments' object function
showArguments() {
console.log("Arguments:", arguments);
Output:
Advanced Web Development 202403103520084
Practical 9
Aim:Write a demonstrative JavaScript for i) Map, wekamap and Set, ii)
Function Closure, and iii) Typed array with ArrayBuffers.
// advancedDemonstration.js
// Demonstrating Map, WeakMap, and Set
// Map example const map = new Map(); map.set(‘name’,
‘Alice’); map.set(‘age’, 30); console.log(“Map:”, map);
console.log(“Name from Map:”, map.get(‘name’)); // Output: Alice
// WeakMap example const
weakMap = new WeakMap();
let obj = {}; weakMap.set(obj, ‘This is a weak map entry’);
console.log(“WeakMap:”, weakMap.get(obj)); // Output: This is a weak map entry
// Set example const set = new Set();
set.add(1); set.add(2); set.add(2); // Duplicate
value, will not be added console.log(“Set:”,
set); // Output: Set: { 1, 2 }
// Demonstrating Function Closure
function createCounter() { let
count = 0; return function() {
count++;
return count;
};
const counter = createCounter(); console.log(“Counter:”, counter()); //
Output: Counter: 1 console.log(“Counter:”, counter()); // Output: Counter: 2
Advanced Web Development 202403103520132
// Demonstrating Typed Arrays with ArrayBuffers const buffer = new
ArrayBuffer(16); // Create a buffer of 16 bytes const int32View = new
Int32Array(buffer); // Create a view for 32-bit integers int32View[0] = 42;
int32View[1] = 84;
console.log(“Typed Array (Int32Array):”, int32View); // Output: Typed Array (Int32Array):
Int32Array(4) [ 42, 84, 0, 0 ]
Output:
Advanced Web Development 202403103520084
Practical 10
Aim:a) Write a JavaScript to list the properties of an object using iterator
and generator.
b) Write a JavaScript to delete the name property and change the
sclass property value from the following object. Also print the object
before and after the said operations. [Hint: Use data properties of a
JavaScript object.]
c) Write a JavaScript to update the rollno property value based
on the scalss value. Perform this using accessor property of a
JavaScript object. [Hint: for sclass: VI, the rollno is 12. If the sclass:
V, the rollno should be 13.] Sample object:
let student = { name : "David Rayy", sclass : "VI", rollno : 12 };
// objectManipulation.js // Sample object let student = { name:
"David Rayy", sclass: "VI", rollno: 12 }; // a) Listing properties
of an object using iterator and generator function*
objectProperties(obj) { for (let key in obj) { yield key;
const iterator = objectProperties(student);
console.log("Properties of the student object:");
for (let prop of iterator) { console.log(prop);
// b) Deleting the name property and changing the sclass property value
console.log("Before operations:", student); delete
student.name; // Deleting the name property
student.sclass = "V"; // Changing the sclass property value
console.log("After operations:", student);
Advanced Web Development 202403103520132
// c) Updating the rollno property value based on the sclass value
Object.defineProperty(student, 'rollno', { get: function() { return
this.sclass === "VI" ? 12 : this.sclass === "V" ? 13 : undefined;
},
set: function(value) { console.log("Cannot set rollno directly. Use
sclass to update rollno.");
});
// Testing the accessor property console.log("Updated rollno based on
sclass:", student.rollno); // Output: 13 student.sclass = "VI"; // Changing
sclass to VI console.log("Updated rollno based on sclass:", student.rollno);
// Output: 12
Output: