Practical No :04 Roll No :
SY. Bsc[CS] SUB: Web Programming
Aim: Write JavaScript code for following. SEM:III
________________________________________________________________
Theory:
A] Performing various mathematical operations such as calculating:
a. Factorial:
Factorial of number is the product of all positive descending integers. Factorial of n is denoted by
n!. For example -
4! = 4 * 3 * 2 * 1 = 24
5! = 5 * 4 * 3 * 2 * 1 = 120
Here, 4! is pronounced as "4 factorial", it is also called "4 bang" or "4 shriek".
58.8M
1.2K
OOPs Concepts in Java
Next
Stay
In this practical, we are calculating the factorial of a number using JavaScript
Here, we are using two ways to find the factorial. The first one is the iterative approach, and
another one is the recursive approach.
Using Iterative approach
Here, we are iterating a loop over the sequence of numbers to get the factorial of a given number.
Using this approach, the consumption of memory is less than the recursive implementation. But
the code is lengthier than the recursive method.
b. Fibonacci:
Fibonacci series is a series that generates subsequent series of numbers by the addition of the two
previous numbers. The first two terms of the Fibonacci series are zero and one, respectively. And
the next terms are the addition of the two previous terms.
Representation of the Fibonacci series
Fn = (Fn -1) + (Fn - 2)
Practical No.04 Page 1
Fn represents the addition of the previous terms (Fn - 1) and (Fn - 2). Here Fn-1 is the first terms,
and Fn-2 is the second terms of the Fibonacci series.
c. Prime Number:
The condition number % i == 0 checks if the number is divisible by numbers other than 1 and
itself. If the remainder value is evaluated to 0, that number is not a prime number. The isPrime
variable is used to store a boolean value: either true or false.
d. Evaluating Expression:
The eval() function in JavaScript is used to evaluate the expression. It is JavaScirpt's global
function, which evaluates the specified string as JavaScript code and executes it.
The parameter of the eval() function is a string. If the parameter represents the statements, eval()
evaluates the statements. If the parameter is an expression, eval() evaluates the expression. If the
parameter of eval() is not a string, the function returns the parameter unchanged.
There are some limitations of using the eval() function, such as the eval() function is not
recommended to use because of the security reasons. It is not suggested to use because it is
slower and makes code unreadable.
Syntax
eval(string)
e. Reverse of Number:
JavaScript contains a built-in function called reverse(), which is used to reverse an array or a
string. We can also reverse a number through various methods. Either we could store a number in
the form of an array or a string and reverse the array using the built-in function, or we could
reverse a number using loops (for, while, do-while, etc.). We would be discussing how to reverse
an array, string, or a number in javascript
Logic to find out Reverse in JavaScript
Let us take, for instance, that we want to reverse a number entered by a user in javascript.
Reversing a number can also be named as palindrome implementation.
The program will ask you to enter a number and will reverse the number for you. This can be
performed in various languages as considered an important concept.
We can reverse a number either by arrow function or by regular function.
Regular function needs to be given with an explicit value that will be returned as an output,
whereas Arrow functions return an implicit value in nature.
B] Validating the various form element:
Practical No.04 Page 2
Forms are used in webpages for the user to enter their required details that further send it to the
server for processing. A form is also known as a web form or HTML form. Examples of form use
are prevalent in e-commerce websites, online banking, online surveys to name a few.
Validating a form: The data entered into a form needs to be in the right format and certain fields
need to be filled in order to effectively use the submitted form. Username, password, contact
information are some details that are mandatory in forms and thus need to be provided by the
user.
Below is a code in HTML, CSS, and JavaScript to validate a form.
HTML is used to create the form.
JavaScript to validate the form.
A. Performing various mathematical operations such as calculating:
Calculating factorial :
Source Code :
<html>
<head>
<script>
Output:
function factorial(){
Factorial is given no :120
var obj=document.getElementById("msg");
5
var txt=document.getElementById("num"); Factorial
var NUM=txt.value;
if(isNaN(NUM)){
obj.innerHTML="Input was incorrect";
else{
var f=1;
for(var i=2;i<=NUM;i++){
f=f*i;
Practical No.04 Page 3
obj.innerHTML="Factorial is given no :" +f;
</script>
</head>
<body>
<center>
<p id="msg">output show here</p>
<input type="text" value="" id="num"/>
<button onclick="factorial()">Factorial</button>
</center>
</body>
</html>
Finding Fibonacci Series:
Source Code: Output:
<html>
<head>
<title>Fibonacci Series</title>
</head>
<body>
<script>
var n1=0, n2=1, next_num, i;
var num= parseInt(prompt("Enter the limit for fibonacci series"));
document.write("Fibonacci Series");
for(i=1;i<=num;i++)
Practical No.04 Page 4
document.write("<br>" +n1);
next_num=n1+n2;
n1=n2;
n2=next_num;
</script>
</body>
</html>
Displaying Prime Numbers in a given range:
Source Code:
<html>
<head>
<script type="text/javascript">
var num,num1,i,f=0;
function prime()
num=Number(document.getElementById("a").value);
num1=Number(document.getElementById("b").value);
while(num<=num1) Output:
{ prime number
f=0;
Enter First number: 1
for(i=1;i<=num;i++) Enter First number: 10
{ Print_Prime
if(num%i==0)
2
{ 3
5
f++;
7
Practical No.04 Page 5
}}
if(f==2)
document.write(num+"<br>");
console.log(num);
num++;
}}
</script>
</head>
<body>
<center>
<h1>prime number</h1>
Enter First number: <Input type="number" id="a"><br>
Enter First number: <Input type="number" id="b"><br><br>
<Input type="Submit" value="Print_Prime" onclick="prime()">
</center>
</body>
</html>
Evaluating Expressions:
Source Code:
<html>
<head>
<script>
var a = 10, b = 20, c = 30, sum, mul, sub;
Practical No.04 Page 6
sum = eval(" a + b + c ");
mul = eval(" a * b * c");
sub = eval(" a - b"); Output:
document.write(sum + "<br>");
document.write(mul + "<br>");
document.write(sub);
</script>
</head>
<body>
</body>
</html>
Calculating reverse of a number:
Source Code:
<html>
<head>
<script>
function reverse()
var x,y,z,temp=0;
y=Number(document.getElementById("demo").value);
z=y;
while(y>0)
x=y%10;
y=parseInt(y/10);
Practical No.04 Page 7
Output:
temp=temp*10+x;
alert(temp);
</script>
</head>
<body>
Enter a Number: <input id="demo">
<button onclick="reverse()">Check</button>
</body>
</html>
B. Validating the various Form Element:
Source Code:
<!DOCTYPE html> Output:
<html>
<body>
<center>
<form action="#">
<label for="fname">First Name:</label>
<input type="text" name="fname" id="fname" required>
<br><br>
<label for="lname">Last Name:</label>
<input type="text" name="lname" id="lname" required>
<br><br>
<label for="email">Email Id:</label>
<input type="email" name="email" id="email"
required>
<br><br>
<input type="submit" value="Submit">
</form>
</center>
</body>
</html>
Practical No.04 Page 8