MST Ex-5
MST Ex-5
Description:
Creating an array:- Using an array literal is the easiest way to
create a JavaScript Array.
Syntax:
constarray_name = [item1, item2, ...];
Array Methods
In JavaScript, there are various array methods available that makes it
easier to perform useful calculations.
Program:
<!DOCTYPE html>
<html>
<body bgcolor="cyan">
<center><h1><i>ShopTime</i></h1>
<header>
<nav align="center"><h3>
3
</nav>
</header></center>
<I><h2>JavaScript Arrays</h2></I>
<B><h1 id="demo1"></h1></B>
<p id="demo2"></p>
<p id="demo3"></p>
<p id="demo4"></p>
<script>const
Movie = [ "The
Martian",
"English",
"10",
"Matt Damon",
];
document.getElementById("demo1").innerHTML = "Movie:
"+Movie[0]; document.getElementById("demo2").innerHTML =
"Language: "+Movie[1];
document.getElementById("demo3").innerHTML = "Rating:
"+Movie[2]; document.getElementById("demo4").innerHTML =
"Starring: "+Movie[3];
</script>
</body>
</html>
4
Output:
Syntax:
myInterval = setInterval(function, milliseconds);
To stop the execution of setInterval function
Syntax:clearInterval(myInterval);
To create a Promise we have to use to following Syntax
Syntax:
Program:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible"
content="IE=edge"><meta name="viewport"
content="width=device-width, initialscale=1.0">
<title>Exp__5b</title>
</head>
<body>
<script>
let c=0;
conststock=setInterval(
stokc,3000);
function stokc(){
var myPromise = new Promise(function (resolve, reject)
{
setTimeout(function ()
resolve(a);
},
3000);
7
});
myPromise.then(
function (data)
{
console.log(data);
},
function (error) {
console.log(error);
}
);
c+=1;
if(c==5)
{
Stop();
}
function Stop() {
clearInterval(stock);
</script>
</body>
</html>
Output:
8
(vi) Create an validateUser.js file which imports login module and invokes
validate method of User class.
(vii) On submit of the button in HTML the validate method of the User
class should be invoked.
(viii) Implement the validate method to send the username and
password details entered by the user and capture the return
value to display in the alert.
Program:
<!DOCTYPE html>
<html lang = “en”>
<head>
<meta charset=”UTF-8/>
<meta http-euiv=”X-UA-Compatible” content=”IE=edge”/>
<meta name = “viewname” content=”width = device-width, initial-
scale=1.0”/>
<title>Document</title>
</head>
<body>
<input type = “text” name=”name” id=”name” placeholder=”Enter
your user name here”/>
<input type = “password” name=”pass” id=”password”
placeholder=”Enter your password”/>
<button type = “submit” id =”btn”> LOGIN</button>
<script src = “validateUser.js” type=”module”></script>
< script src = “login.js” type=”module”></script>
</body>
</html>
ValidateUser.js
document.writeln(user1.validateUser(username,password));
});
Login.js
Class User{
Constructor(name,pass)
{
this.username = name;
this.password = pass;
}
validateUser(name,pass)
{
return name = this.name && pass==this.password) ? ”Login
Successful” : “Unauthorized access”;
}
}
Output: