Professional Documents
Culture Documents
Web Unit 3 Solved QP
Web Unit 3 Solved QP
JAVASCRIPT
---------------------------------------------
2 MARKS QUESTIONS AND ANSWERS
3. What is JavaScript?
JavaScript is a client-side as well as server side scripting language that can be inserted into
HTML pages and is understood by web browsers. JavaScript is also an Object based
Programming language.
Undeclared variables are those that do not exist in a program and are not declared. If the
program tries to read the value of an undeclared variable, then a runtime error is encountered.
Undefined variables are those that are declared in the program but have not been given any
value. If the program tries to read the value of an undefined variable, an undefined value is
returned.
A prompt box is a box which allows the user to enter input by providing a text box. Label
and box will be provided to enter the text or number.
Yes JavaScript does support automatic type conversion, it is the common way of type
conversion used by JavaScript developers
For
While
do-while loops
UNIT III
JAVASCRIPT
---------------------------------------------
10. What would be the result of 3+2+"7"?
Since 3 and 2 are integers, they will be added numerically. And since 7 is a string, its
concatenation will be done. So the result would be 57.
12. What are all the types of Pop up boxes available in JavaScript?
Alert
Confirm and
Prompt
Load time errors: Errors which come up when loading a web page like improper syntax
errors are known as Load time errors and it generates the errors dynamically.
Run time errors: Errors that come due to misuse of the command inside the HTML
language.
Logical Errors: These are the errors that occur due to the bad logic performed on a function
which is having different operation.
JavaScript provides different data types to hold different types of values. There are two
types of data types in JavaScript.
1. Primitive data type
2. Non-primitive (reference) data type
JavaScript is a dynamic type language, means we don't need to specify type of the variable
because it is dynamically used by JavaScript engine. You need to use var here to specify the
data type. It can hold any type of values such as numbers, strings etc. For example:
1. var a=40;//holding number
2. var b="Rahul";//holding string
There are five types of primitive data types in JavaScript. They are as follows:
Data Description
Type
Number:
A number data type can be an integer, a floating point value, an exponential value, a
‘NaN’ or a ‘Infinity’.
1 var a=250; // integer value
2 var b=25.5; // a number containing a decimal
3 var c = 10e4 // an exponential value which evaluates to 10*10000;
String:
The string data type in JavaScript can be any group of characters enclosed by a single or
double-quotes or by backticks.
Boolean:
The boolean data type has only two values, true and false. It is mostly used to check a logical
condition. Thus Booleans are logical data types which can be used for comparison of two
variables or to check a condition. The true and false implies a ‘yes’ for ‘true’ and a ‘no’ for
‘false in some places when we check a condition or the existence of a variable or a value.
When we check the data type of ‘true’ or ‘false’ using typeof operator, it returns a boolean.
1 typeof(true) // returns boolean
2 typeof(false) // returns Boolean
example of comparison statement:
1 var a =5;
2 var b=6;
UNIT III
JAVASCRIPT
---------------------------------------------
3 a==b // returns false
JavaScript can be implemented using JavaScript statements that are placed within
the <script>... </script> HTML tags in a web page.
We can place the <script> tags, containing your JavaScript, anywhere within your web
page, but it is normally recommended that you should keep it within the <head> tags.
The <script> tag alerts the browser program to start interpreting all the text between
these tags as a script. A simple syntax of your JavaScript will appear as follows.
<script ...>
JavaScript code
</script>
The script tag takes two important attributes −
Language − This attribute specifies what scripting language you are using. Typically, its
value will be javascript. Although recent versions of HTML (and XHTML, its successor)
have phased out the use of this attribute.
Type − This attribute is what is now recommended to indicate the scripting language in
use and its value should be set to "text/javascript".
So our JavaScript segment will look like −
<script language = "javascript" type = "text/javascript">
JavaScript code
</script>
Example
Let us take a sample example to print out "Hello World". We added an optional HTML
comment that surrounds our JavaScript code. This is to save our code from a browser that does
not support JavaScript. The comment ends with a "//-->". Here "//" signifies a comment in
JavaScript, so we add that to prevent a browser from reading the end of the HTML comment as
a piece of JavaScript code. Next, we call a function document.write which writes a string into
our HTML document.
This function can be used to write text, HTML, or both. Take a look at the following code.
<html>
<body>
<script language = "javascript" type = "text/javascript">
<!--
document.write("Hello World!")
//-->
</script>
</body>
</html>
This code will produce the following result −
Hello World!
UNIT III
JAVASCRIPT
---------------------------------------------
4. Explain all types of Pop up boxes available in JavaScript?
1. alert Box:
The alert method opens a dialog window and displays its parameter in that window.
It also displays an OK button.
The string parameter of alert is not XHTML code; it is plain text. Therefore, the string
parameter of alert may include \n but never should include <br />.
2. confirm Box
The confirm method opens a dialog window in which the method displays its string parameter,
along with two buttons: OK and Cancel.
confirm returns a Boolean value that indicates the user’s button input: true for OK and
false for Cancel. This method is often used to offer the user the choice of continuing some
process.
var question = confirm(“Do you want to continue this download?”);
After the user presses one of the buttons in the confirm dialog window, the script can test
the variable, question, and react accordingly.
3. prompt Box:
The prompt method creates a dialog window that contains a text box used to collect a string of
input from the user, which prompt returns as its value.
UNIT III
JAVASCRIPT
---------------------------------------------
5. What is an array in javascript? And explain its types to create an arrays.
An array is a special variable, which can hold more than one value at a time.
JavaScript arrays are used to store multiple values in a single variable
As the syntax is not correct of the JavaScript it will affect only the thread that is under this
JavaScript and the rest of the code in other threads gets executed as nothing in them depends on
the code containing the error.
Logical error: It is the most difficult error to be traced as it is the error on the logical part of the
coding or logical error is a bug in a program that causes it to operate incorrectly and terminate
abnormally (or crash).
Runtime Error: A runtime error is an error that occurs during the running of the program, also
known as the exceptions. In the example that is given below the syntax is correct, but at runtime,
it is trying to call a method that does not exist.
Example:
<script type="text/javascript">
// An runtime error here
window.printme();
</script>
As in runtime error, there are exceptions and this exception is correct by the help of the
try and catch method
UNIT III
JAVASCRIPT
---------------------------------------------
Ten Marks Questions and answers
1. Explain functions JavaScript in detail with an example.
Defn :
A function is a set of statements that take inputs, do some specific computation and
produces output. Basically, a function is a set of statements that performs some tasks or does
some computation and then returns the result to the user.
JavaScript functions are used to perform operations. We can call JavaScript function
many times to reuse the code.
Function Definition
Before, using a user-defined function in JavaScript we have to create one. We can use the
above syntax to create a function in JavaScript. Function definition is sometimes also termed as
function declaration or function statement.
Below are the rules for creating a function in JavaScript:
Every function should begin with the keyword function followed by,
A user defined function name which should be unique,
A list of parameters enclosed within parenthesis and separated by commas,
A list of statement composing the body of the function enclosed within curly braces { }.
Example:
function calcAddition(number1, number2)
{
return number1 + number2;
}
In the above example, we have created a function named calcAddition, this function
accepts two numbers as parameters and returns the addition of these two numbers
Function Parameters
Parameters are additional information passed to a function. For example, in the above
example, the task of the function calcAddition is to calculate addition of two numbers. These
two numbers on which we want to perform the addition operation are passed to this function
as parameters.
UNIT III
JAVASCRIPT
---------------------------------------------
The parameters are passed to the function within parentheses after the function name and
separated by commas. A function in JavaScript can have any number of parameters and also
at the same time a function in JavaScript cannot have a single parameter.
Calling Functions:
After defining a function, the next step is to call them to make use of the function. We can call a
function by using the function name separated by the value of parameters enclosed between
parenthesis and a semicolon at the end. Below syntax shows how to call functions in JavaScript:
Syntax
/pattern/modifiers;
Example
var patt = /w3schools/i;
Example explained:
/w3schools/i is a regular expression.
w3schools is a pattern (to be used in a search).
i is a modifier (modifies the search to be case-insensitive).
Similar to this, we can test if the string ends with snow using snow$:
PATTERN MODIFIERS
The modifiers are specified as letters just after the right delimiter of the pattern.
The i modifier makes the letters in the pattern match either uppercase or lowercase letters
in the string.
For example, the pattern /Apple/i matches ‘APPLE’, ‘apple’, ‘APPle’, and any other
combination of uppercase and lowercase spellings of the word “apple.”
The x modifier allows white space to appear in the pattern.
In JavaScript, regular expressions are often used with the two string.
methods: search() and replace().
The search( ) method uses an expression to search for a match, and returns the position
of the match.
The replace( ) method returns a modified string where the pattern is replaced.
Using String search() With a Regular Expression :
Use a regular expression to do a case-insensitive search for “WelcomeToBCA” in a
string:
UNIT III
JAVASCRIPT
---------------------------------------------
3. Explain Errors in JavaScript.
ERRORS IN SCRIPTS:
The JavaScript interpreter is capable of detecting various errors in scripts.
Debugging a script is a bit different from debugging a program in a more typical
programming language, mostly because errors that are detected by the JavaScript
interpreter are found while the browser is attempting to display a document.
In some cases, a script error causes the browser not to display the document and does not
produce an error message.
Without a diagnostic message, you must simply examine the code to find the problem
Notice the syntax in the while statements. The browser display of what happens when an
attempt is made to debugdemo.js
Internet Explorer
Mozilla Firefox
UNIT III
JAVASCRIPT
---------------------------------------------
Some Important Programs for exams so read from
your journal
Lab 4)
Create a HTML form containing Textboxes to enter name of
student, roll number, course, and grade obtained. When
the form runs in the Browser fill the textboxes with
data. Write JavaScript code that verifies that all
textboxes has been filled. If a textboxes has been left
empty, popup an alert indicating which textbox has been
left empty.
Lab 5)
Develop and demonstrate a XHTML file that includes
JavaScript script for receiving a number n as input and
outputting first n Fibonacci numbers.
Lab 6)
Write a JavaScript code to compute the sum of n natural
numbers.
Lab 7)
Create a HTML form containing textbox to enter text.
Write a JavaScript code block, which checks the contents
entered in a form’s Text element. If the text entered is
in the lower case, convert to upper case. Make use of
function to Uppercase().