Lab No 2
Lab No 2
Gaindakot Nawalpur
LAB NO : 02
Project:Sample of condition & Function in Java Script
Submitted By : Submitted To :
Paribesh Sapkota Narayan Sapkota
Date: 2079/03/28
1|Page
Table of Contents
INTRODUCTION: ....................................................................................................................................... 3
Using javascript wap to check whether given number is odd or even. ......................................................... 4
Output: ...................................................................................................................................................... 5
Using java script wap to print following pattern. ....................................................................................... 6
Output ....................................................................................................................................................... 7
Using javascript wap to calculate the factorial of given number. .............................................................. 8
Output ....................................................................................................................................................... 9
Using java script wap to get Fibonacci sequence . .................................................................................... 10
Output: .................................................................................................................................................... 11
Using Java Script display the pattern of: ............................................................................................... 12
Output..................................................................................................................................................... 13
2|Page
INTRODUCTION:
Java Script is the world’s most popular programming language. It is a light – weight object oriented
programming language which is used by several websites for scripting the webpage. JavaScript, user can
build modern web application to interact directly without reloading the page every time.
Purpose of Javascript:
2 Control multimedia
3 Animate images
History of JavaScript
In 1995 Brendan Eich intending to implement and embed Scheme of programming language to the
browser. But before Brendan could start, the company merged with Sun Microsystem for adding java
into it navigator so that it complete with Microsoft over the web technologies and platforms.
Feature of JavaScript
3|Page
Using javascript wap to check whether given number is odd or even.
To tell if a number is odd or even in Js we use modulus operator along with if condition statement
first we add onclick event to submit button to run a function
Code:
<head>
<title>tester</title>
</head>
<body>
<p>check evenOrOdd</p>
<input id="userVal" type="text" value="">
<button type="button" onclick="evenOrOdd()">check</button>
<div id="results"></div>
<Script>
function evenOrOdd() {
var userInput = Number(document.getElementById("userVal").value);
if ((userInput % 2) == 0) {
document.getElementById("results").textContent = "The
Number is Even";
} else if ((userInput % 2) == 1) {
document.getElementById("results").textContent = "The Number
is Odd";
}
4|Page
else {
document.getElementById("results").textContent = "Nany";
}
}
</script>
</body>
</html>
Output:
5|Page
Using java script wap to print following pattern.
*
**
***
****
*****
<head>
<title>tester</title>
</head>
<body>
<p>pattern</p>
<script>
let n = 5;
let string = "";
for (let i = 1; i <= n; i++) {
for (let j = 0; j < i; j++) {
string += "*";
}
string += "\n";
}
console.log(string);
6|Page
</script>
</body>
</html>
Output
7|Page
Using javascript wap to calculate the factorial of given number.
Code:
8|Page
Output
9|Page
Using java script wap to get Fibonacci sequence .
Code:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<p> fibonacci</p>
<script>
var a=1,b=2, c,i;
var n=parseInt(prompt("enter a numberc"));
document.write("fibonacci series :");
for(i=1;i<=n;i++)
{
document.write("<br>"+a);
c=a+b;
a=b;
b=c;
</script>
</body>
</html>
10 | P a g e
Output:
When we execute the above program, it displays the given image. There is a prompt box to
define the Fibonacci series limits, and then click the OK button to continue.
11 | P a g e
Using Java Script display the pattern of:
NEPAL
NEPA
NEP
NE
N
Code:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<script>
var x = "NEPAL";
document.write(x[b]);
document.write("<br>");
</script>
</body>
</html>
12 | P a g e
Output
13 | P a g e