Exercise-2 MSD Lab Programs
Exercise-2 MSD Lab Programs
a).Write a JavaScript to create an Employee class extending from a base class Person.
Hints:
(i) Create a class Person with name and age as attributes.
(ii) Add a constructor to initialize the values
(iii) Create a class Employee extending Person with additional attributes role
AIM:
a).Write a JavaScript to create an Employee class extending from a base class Person.
Hints:
(i) Create a class Person with name and age as attributes.
(ii) Add a constructor to initialize the values
(iii) Create a class Employee extending Person with additional attributes role
<!DOCTYPE html>
<html>
<head>
<title> Employee Details</title>
<style>
.mydiv {
text-decoration: bold;
text-align: left;
margin-top: 10px;
width: 700px;
height: 550px;
border: 6px solid purple;
text-align: center;
color: blue;
background-color: #c7f15e;
font-family: verdana;
font-size:25pt;
}
</style>
</head>
<body bgcolor="pink">
<center>
<h1 style="color:red;font-size:40pt;"><b>Creating and Inheriting Classes </b></h1>
<div class="mydiv">
<script type="text/javascript">
class Person
{
constructor(name, age,empid)
{
this.name = name;
this.age = age;
this.empid = empid;
}
}
class Employee extends Person
{
constructor(name, age,empid,job,salary,mobileno,emailid,role)
{
super(name, age,empid);
this.job=job;
this.salary=salary;
this.mobileno=mobileno;
this.emailid=emailid;
this.role = role;
}
getEmployeeDetails()
{
document.write("<br><br>*** EMPLOYEE INFORMATION ***<br>");
document.write(" <br> Name : "+this.name);
document.write(" <br> Age : ",this.age);
document.write("<br> Empid : ",this.empid);
document.write("<br> Job : ",this.job);
document.write("<br> Salary : ",this.salary);
document.write("<br>Mobile No : ",this.mobileno);
document.write("<br>Email Id : ",this.emailid);
document.write("<br> Job Role : ",this.role);
}
}
let employee1 = new Employee("Karth", 25,"Sacet180", "Software",100000,
9110307238,"Dr [email protected]","Software Manager");
employee1.getEmployeeDetails();
</script>
</div>
</body>
</html>
OUTPUT: 2.a
Exercise -2(b) : JavaScript – Modules
AIM:
b).Write a JavaScript to validate the user by creating a login module. Hints:
(i) Create a file login.js with a User class.
(ii) Create a validate method with username and password as arguments.
(iii) If the username and password are equal it will return "Login Successful"
else will return "Unauthorized access".
<!DOCTYPE html>
<html>
<head>
<title>Creating a Login Module</title>
<style>
.mydiv {
text-decoration: bold;
margin-top: 10px;
width: 600px;
height:400px;
border: 5px solid purple;
text-align: center;
color: blue;
background-color: #c7f15e;
font-family: verdana;
font-size:25pt;
padding: 20px;
}
</style>
</head>
<body style="background-color:#F0E68C;">
<center>
<h1 style="color:red;font-size:35pt;">Creating a Login Module</h1>
<div class="mydiv">
<script src="Login.js">
</script>
<form id="login-form">
<br><br>
<label for="username"><b>Username:</label>
<input type="text" id="username" name="username"><br><br>
<label for="password"><b>Password:</label>
<input type="password" id="password" name="password"><br><br>
<input type="submit" value="Login"
onclick="validate()"> <input type="reset" value="Reset">
<br><br>
<div id="result"></div>
</center>
</body>
</html>
Login.js
class User {
constructor(username, password)
{
this.username = username;
this.password = password;
}
validate() {
if (this.username === this.password) {
return "Login Successful!";
}
else {
return "Unauthorized Access!";
}
}
}
function validate() {
form = document.getElementById('login-form');
resultDiv = document.getElementById('result');
username = document.getElementById('username').value;
password = document.getElementById('password').value;
event.preventDefault();
const user = new User(username, password);
const result = user.validate();
resultDiv.innerHTML = result;
}
OUTPUT: 2.b