[go: up one dir, main page]

0% found this document useful (0 votes)
13 views13 pages

Lab No 2

Uploaded by

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

Lab No 2

Uploaded by

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

Lumbini Ict Campus

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:

1 To create dynamically updating content

2 Control multimedia

3 Animate images

The uses of Java script:

 Client side validation


 Display time and date
 Open and close new window
 To display dialog box and popup window
 To change the appearance of Html document
 To validate the user input before submission of form

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

1. It is a light-weighted and interpreted language.


2. It is a case- sensitive language.
3. It is supported in serval operating system including window , macos etc.
4. It provide good control to the user over the web browsers.

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

Then use the value property.

Code:

<! DOCTYPE html>


<html>

<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.

*
**
***
****
*****

<! DOCTYPE html>


<html >

<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:

<! DOCTYPE html>


<html>
<head>
</head>
<body >
<h1> Factorial</h1>
Enter a number: <input id = "num">
<br><br>
<button onclick = "fact()"> Factorial </button>
<p id = "res"></p>
<script>
function fact(){
var i, num, f;
f = 1;
num = document.getElementById("num").value;
for(i = 1; i <= num; i++)
{
f = f * i;
}
i = i - 1;
document.getElementById("res").innerHTML = "The factorial of the number " + i
+ " is: " + f;
}
</script>
</body>
</html>

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.

After click on ok button we get the Fibonacci sequence

11 | P a g e
Using Java Script display the pattern of:
NEPAL
NEPA
NEP
NE
N
Code:

<!DOCTYPE html>

<html>

<head>

<title> pattern 2</title>

</head>

<body>

<p> show the pattern</p>

<script>

var x = "NEPAL";

for (a = 0; a < x.length; a++) {

for (b = 0; b< x.length - a; b++) {

document.write(x[b]);

document.write("<br>");

</script>

</body>

</html>

12 | P a g e
Output

13 | P a g e

You might also like