[go: up one dir, main page]

0% found this document useful (0 votes)
99 views4 pages

Ajp Practical 4

This document discusses functions in JavaScript. It defines a function as consisting of four parts: the name, parentheses, a code block, and an optional return keyword. It also provides examples of function definitions and calls. It asks multiple choice questions about function definitions and scope. It then provides two examples of calculating the factorial of a number by calling a function with and without arguments.

Uploaded by

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

Ajp Practical 4

This document discusses functions in JavaScript. It defines a function as consisting of four parts: the name, parentheses, a code block, and an optional return keyword. It also provides examples of function definitions and calls. It asks multiple choice questions about function definitions and scope. It then provides two examples of calculating the factorial of a number by calling a function with and without arguments.

Uploaded by

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

Theory: -

Defining a Function

A function must be defined before it can be called in a JavaScript statement.


The best place to defi ne a function is at the beginning of a JavaScript that is
inserted in the <head> tag, because then all subsequent JavaScripts on the
web page will know the definition of that function.

A function definition consists of four parts: the name, parentheses, a code


block, and an optional return keyword.

Practical related questions:


1. A comma must separate arguments in a function definition.
a. True b. False

2. A code block is used in a


a. Function call b. Function definition c. Return value d. Argument

3. True or False.
A function can be called by HTML code in a web page.
a. True b. False

4.A variable is out of scope when:


a. The statement that calls a function ignores the value returned by the function
b. The variable cannot be accessed by a statement
c. A variable isn’t defined in a function
d. A variable is passed to a function

5.A local variable can be accessed,


a. Only by functions defined within the JavaScript
b. Only outside of a function
c. Only by the function that defined it
d. From anywhere in the JavaScript

192020058
Exercise:
Calculate the factorial of a number by
1) calling function without argument

Code

<html>
<head>
<title>
factorial of a given number
</title>
</head>
<body>
<center>
<h2>Javascript Factorial</h2>
<button onclick="myFunction()">Promt Button</button>
<script>
function myFunction() {
var i;
var fact = 1;
var number = prompt("Please enter a number")
for(i = 1;i <= number;i++) {
fact = fact * i;
}
document.write("Factorial of "+number+" is "+fact);
}
</script>
</body>
</html>

Output

192020058
2) calling function with argument.

code

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

Output: -

192020058
Conclusion:
We have learnt about how to print message and how to perform Factorial
operation using alert and prompt.

Marks Obtained Dated Signature of Teacher


Process Related Product Total
(15) Related (50)
(35)

192020058

You might also like