[go: up one dir, main page]

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

Mayuri

Uploaded by

shubhamd2940
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)
12 views7 pages

Mayuri

Uploaded by

shubhamd2940
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

Class:MCA ll Name: Mayuri Pandurang Patil

Subject:Advanced WebTechnology Roll No: 185


PRACTICAL NO.07

Q. Introduction to Node.js

a) Create a node.js script file that displays Hostname & Platform details of current
system on the console[Hint: Use OS module]

Source Code:

Var os = require('os');
console.log("Platform of os:"+os.platform());
console.log("Hostname of os "+os.hostname());

OUTPUT:

b) Create a user defined mode Math with four functions Addition, Subtraction,
Multiplication,
Division & export them Import Math module from other Node.js script file and
invoke all the four functions to perform operations on given input .

Source Code:

Math
Module

// Math.js

// Function to perform addition


function addition(a, b) {
return a + b;
}

// Function to perform subtractionfunction


subtraction(a, b) {
return a - b;
}
Class:MCA ll Name: Mayuri Pandurang Patil

Subject:Advanced WebTechnology Roll No: 185

// Function to perform multiplication


function multiplication(a, b) {
return a * b;
}

// Function to perform division


function division(a, b) {

if (b === 0) {
throw new Error("Division by zero is not allowed");
}
return a / b;
}

// Exporting
functions
module.exports = {
addition,
subtraction,
multiplicati
on,division
};

New.js

// app.js

// Importing Math module


const Math = require('./Math');

// Invoking all the four functions to perform operationsconst


a = 10;
const b = 5;

console.log("Addition:", Math.addition(a, b)); console.log("Subtraction:",


Math.subtraction(a, b)); console.log("Multiplication:",
Math.multiplication(a, b));console.log("Division:", Math.division(a, b));
Class:MCA ll Name: Mayuri Pandurang Patil

Subject:Advanced WebTechnology Roll No: 185


Output :

C:\Users\DYPATU>no
de New.jsAddition: 15
Subtraction: 5
Multiplication: 50

Division: 2

c) Create a Node.js script that display a message Welcome to Node JS through


loop ,with delay in between the iteration i using SetTimeOut().
Source Code:

for(vari = 0; i<10; i++)


{
setTimeout(function()
{ console.log('Welcome to Node JS');},1000);
}
Output:
Class:MCA ll Name: Mayuri Pandurang Patil

Subject:Advanced WebTechnology Roll No: 185

d) Create a user defined data module that can give you the current date and time.

Source Code:

dmodule.js

// dmodule.js

// Function to get the current date and time


function getCurrentDateTime() {

const currentDate = new Date();


return currentDate;
}

// Exporting the getCurrentDateTime function


module.exports = getCurrentDateTime;
date.js

// date.js

// Importing the DateModule


const getCurrentDateTime = require('./dmodule');

// Getting the current date and time using the imported functionconst
currentDateAndTime = getCurrentDateTime();

// Displaying the current date and time console.log("Current Date


and Time:", currentDateAndTime);

Output :

C:\Users\DYP-ATU> node date.js

Current Date and Time: 2024-05-10T07:51:31.877Z

C:\Users\DYP-ATU>
Class:MCA ll Name: Mayuri Pandurang Patil

Subject:Advanced WebTechnology Roll No: 185


e) Create a days Till Custom Module. It should be able to give you the number of days
till Christmas and the number of days till Mothers day. Number of days till you
Birthday.
[Hint: Subtract both the dates to get difference in number of millisecond.]

Source Code:
daysTill.js

// daysTill.js

function daysUntilChristmas() {
const today = new Date();
const christmas = new Date(today.getFullYear(), 11, 25);if
(today.getMonth() === 11 && today.getDate() > 25) {
christmas.setFullYear(christmas.getFullYear() + 1);
}
const oneDay = 1000 * 60 * 60 * 24;
const daysLeft = Math.ceil((christmas - today) / oneDay);return
daysLeft;
function daysUntilMothersDay() {const
today = new Date();
const year = today.getFullYear();
// Find the second Sunday in May
const mothersDay = new Date(year, 4, 1);while
(mothersDay.getDay() !== 0) {
mothersDay.setDate(mothersDay.getDate() + 1);
}
mothersDay.setDate(mothersDay.getDate() + 7);
const oneDay = 1000 * 60 * 60 * 24;
const daysLeft = Math.ceil((mothersDay - today) /
oneDay);return daysLeft;
}

module.exports = {
daysUntilChristmas,
daysUntilMothersDay
};

Custome.js

const daysTill = require('./daysTill');


Class:MCA ll Name: Mayuri Pandurang Patil

Subject:Advanced WebTechnology Roll No: 185

const daysUntilChristmas = daysTill.daysUntilChristmas(); const


daysUntilMothersDay = daysTill.daysUntilMothersDay();

console.log(`Days until Christmas: ${daysUntilChristmas}`); console.log(`Days


until Mother's Day: ${daysUntilMothersDay}`);

Output :

C:\Users\DYP-ATU>node custome.js
Days until Christmas: 229
Days until Mother's Day: 2

You might also like