0% found this document useful (0 votes)
80 views

Software Packages 2

The document contains 3 coding exercises for creating console-based Node.js applications: 1. A program to check if a user is eligible to sign in based on age by prompting for name, age, email, and mobile number. It returns a message if the age is less than 18 or displays the sign in details. 2. A program to create a book object using object literal syntax with properties for title, author, and publish year. It prints the book object details to the console. 3. A program to create an array of product objects with name, inventory, and unit price properties. It defines functions to list the product names and calculate the total value of the products by multiplying inventory by unit

Uploaded by

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

Software Packages 2

The document contains 3 coding exercises for creating console-based Node.js applications: 1. A program to check if a user is eligible to sign in based on age by prompting for name, age, email, and mobile number. It returns a message if the age is less than 18 or displays the sign in details. 2. A program to create a book object using object literal syntax with properties for title, author, and publish year. It prints the book object details to the console. 3. A program to create an array of product objects with name, inventory, and unit price properties. It defines functions to list the product names and calculate the total value of the products by multiplying inventory by unit

Uploaded by

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

Practical – 2

Console based node.js applications

1. Write a Node.js program to create a console application to check whether a user is


eligible to sign-in or not based on age.
Description:
Prompt the user to enter following data using ReadLine package:
Enter Your Name
Enter Your Age
Enter Your Email ID
Enter Your Mobile No
Rules, Logic and Condition in Entry:
 If Age is less than < 18 it will return and display the message:
"Minimum required 18 years and your age is 16 , You should wait at least 2 year(s)
more."
 If Age is greater than > 18 it will return and display the message:
“Great you can sign in.”
“User Name : ”
“Age : “
“Email ID : ”
“Mobile : ”

Code:
var r1=require('readline');
var r2=r1.createInterface(process.stdin, process.stdout);
r2.question("Enter Name: ", function(Name) {
    r2.question("Enter Age: ", function(Age) {
        r2.question("Enter Email ID: ", function(email) {
            r2.question("Enter Mobile No: ", function(mobileno)
            {
                if(Age<18)
                {
                    console.log("\nMinimum required 18 years and your age is "+ Age +" , You sho
uld wait at least "+ (18-Age) +" year(s) more.")

1
                }
                else
                {
                    console.log("\nGreat "+ Name +" you can sign in.")
                    console.log("User Name : "+Name)
                    console.log("Age : "+Age)
                    console.log("Email ID : "+email)
                    console.log("Mobile : "+mobileno)
                }
                //console.log("Your Name : "+Name+"\n Your Age: "+Age);
                process.exit();
            });
        });
    });
});

Output:

2. Write a Node.js program to create an object named book using object literal syntax.
Add book_title, author and publish_year as properties to the book object and assign
it’s appropriate values. Now create function print_info() to print the book object to
the console so the final output looks as below:

2
title: Harry Potter and the Sorcerer's Stone
author: J.K. Rowling
publish_year: 1997
Code:
const book={
    title:"Harry Potter and the Sorcerer's Stone",
    author: "J.K. Rowling",
    publish_year: 1997,
};

function book_info(title, author, publish_year) {
    for (let book_property in book) {
        console.log(book_property + ': ' + book[book_property]);
    }
}
book_info()

Output:

3. Create an array named products. Add objects to the array. Each object should be a
single product, with 3 properties: name, inventory and unit_price. Create two
functions named listProducts() and totalValue(). A listProducts() function accepts a
parameter -- the array of products and it should return an array of the names of the
products. A function named totalValue() should accept a parameter -- the array of
products and it should return the total value of all of the products in the array. To

3
calculate the total value of one product multiply the inventory value with the
unit_price.
Code:
var products=[
    {name:'pen',inventory:5,unit_price:10},
    {name:'book',inventory:2,unit_price:70},
    {name:'bag',inventory:1,unit_price:500}
];

function listProducts()
{
    for(var i=0;i<products.length;i++)
    {
        console.log(products[i].name)
    }
}
console.log("Name of the Product: ")
listProducts()

function totalValue()
{       
    let final_price = 0;
    for (let i = 0; i < products.length; i++) 
    {
        final_price += products[i].inventory * products[i].unit_price;
    }
    return final_price;
}
console.log("\nPrice: "+totalValue(products));
Output:

4
5

You might also like