Software Packages 2
Software Packages 2
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