0% found this document useful (0 votes)
165 views

Css Answer

Differentiate between prompt() and alert() methods: 1. Prompt() is used to get input from the user, while alert() is used to display a message. 2. Prompt() returns the input value or null, while alert() always returns true. 3. Prompt() allows the user to click "OK" or "Cancel", while alert() only allows clicking "OK".

Uploaded by

14 - Prasad Bhot
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
165 views

Css Answer

Differentiate between prompt() and alert() methods: 1. Prompt() is used to get input from the user, while alert() is used to display a message. 2. Prompt() returns the input value or null, while alert() always returns true. 3. Prompt() allows the user to click "OK" or "Cancel", while alert() only allows clicking "OK".

Uploaded by

14 - Prasad Bhot
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

b) Differentiate between prompt() and alert() methods.

Alert Prompt

An alert box is used if we want A prompt box is used when we want the user to input
the information comes through a value before entering a page.
to the user.
Its syntax is -: Its syntax is -:
window.alert(“sometext”); window.prompt(“sometext”,”defaultText”);
It always return true we always We need to click “OK” or “Cancel” to proceed after
need to click on “OK” to proceed entering an input value when a prompt box pops up
further. on a webpage.
The alert box takes the focus If we click “OK” the box returns the input value.
away from the current window
and forces the browser to read
the message.
We need to click “OK” to If we click “Cancel” the box returns a null value.
proceed when an alert box pops
up.

c) State use of getters and setters

Getter:-When the Property is accessed the return value from the getter is used

o Get-A function without argument that works when a property is read

Setter:-When a value is set,the setter is called and passed the value that was set

o Set-a function with one argument that is called when the property is set

d) State and explain any two properties of array object

e) Write a JavaScript that displays first 20 even numbers on the document window.

<html>
<head>
<title>Display first 20 even numbers</title>
</head>
<body> <script type="text/javascript">
var i = 2;
document.write("First 20 even numbers <br/>");
while(i<=40)
{
document.write(i + " ");
i = i + 2;
}
</script>
</body>
</html>
a) write a program to print sum of even numbers between 1 to 100 using for loop.

<script>
let sum=0
for(i=1;i<=100;i++)
{
if(i%2==0)
{
sum=sum+i
}
}
document.write(sum)
</script>

b) Write a JavaScript function to insert a string within a string at a particular position

<script>
var a = "I want apple";
var b = " an";
var position = 6;
var output = [a.slice(0, position), b, a.slice(position)].join('');
document.write(output);
</script>

c) Generate college Admission form using html form tag

<html>
<head>
<title>College Admission Form</title>
</head>
<body>
<center>
<h1>Admission Form</h1>
<form>
<table>
<tr>
<td>Full Name:</td>
<td><input type="text" name="lname"/></td>
</tr>
<tr>
<td>Date of Birtd:</td>
<td><input type="text" name="dob"/></td>
</tr>
<tr>
<td>Gender:</td>
<td><input type="radio" name="gender" value="Male">Male</input>
<input type="radio" name="gender" value="Female">Female</input>
</td>
</tr>
<tr>
<td>Category:</td>
<td>
<select name="category">
<option selected>Open</option>
<option>OBC</option>
<option>SC</option>
<option>ST</option>
<option>VJ/NT</option>
</select>
</td>
</tr>
<tr>
<td>Email:</td>
<td><input type="text" name="email" /></td>
</tr>
<tr>
<td>Contact no.:</td>
<td><input type="text" name="phone" /></td>
</tr>
<tr>
<td>Address :</td>
<td><textarea rows="2" cols="20"></textarea>
</tr>
</table>
</form>
<input type="submit" value="Submit" />
<input type="reset" value="Cancel" />
</center>
</body>
</html>

d) State the use of following methods.

i. charCodeAt(): This function returns the unicode value of the character at specified position.

Syntax:-stringname.charCodeAt(X);

ii. fromCharCode(): This method convert a given Unicode number into a character.

Syntax:-String.fromCharCode(n1,n2,….nX)

<html>
<head>
<title>String Demo </title>
</head>
<script language="Javascript" type="text/javascript">
var strl="Welcome to JavaScript";
document.write("<br>Unicode="+strl.charCodeAt(4));
document.write("<br>Unicode to character ="+String.fromCharCode(111));
</script>
</head>
<body>
</body>
</html>

a) State the use of dot syntax in JavaScript with the help of suitable example.

✓ One can access an object properties and methods by using the dot syntax along with the object name
and its property or method.
✓ For example,the write method for a document:

document.write()

✓ A dot separates the name of the object from the property or method.
✓ The first part is the name of the object(document)and second part is either a property or
method(write) of the object.

b) List and explain Logical operators in JavaScript.


Operator Description
&& && is known as AND operator. It checks whether two operands are non-zero (0, false, undefined, null or “” are
considered as zero), if yes then returns 1 otherwise 0.
|| || is known as OR operator. It checks whether any one of the two operands is non-zero (0, false, undefined, null
or “” is considered as zero).
! ! is known as NOT operator. It reverses the boolean result of the operand (or condition)

d) Write a JavaScript that initializes an array called “Fruits” with names of five fruits. The script then displays
the array in a message box.

<html><body>
<script>
var fruits = new Array();
fruits[0] = 'Mango ';
fruits[1] = 'Apple';
fruits[2] = 'orange';
fruits[3] = 'cherry';
fruits[4] = 'Watermelon';
for (var i = 0; i < fruits.length; i++)
{
alert(fruits[i] );
}
</script>
</body>
</html>

f) Enlist and explain the use of any two Intrinsic JavaScript functions.

a) Write syntax of and explain prompt method in JavaScript with the help of suitable example.

prompt() :The prompt () method displays a dialog box that prompts the visitor for input.The prompt () method
returns the input value if the user clicks "OK". If the user clicks "cancel" the method returns null.

Syntax: window.prompt (text, defaultText)

Example:-

<html>

<script type="text/javascript">

function msg(){

var v= prompt("Who are you?");


alert("I am "+v);

</script>

<input type="button" value="click" onclick="msg()"/>

</html>

b) Write a JavaScript program which compute, the average marks of the following students Then, this average
is used to determine the corresponding grade. Student Name Marks

Student Name Marks


Sumit 80
Kalpesh 77
Amit 88
Tejas 93
Abhishek 65
The grades are computed as follows :

Range Grade
<60 E
<70 D
<80 C
<90 B
<100 A

<html>
<head>
<title>Compute the average marks and grade</title>
</head>
<body>
<script>
var students = [['Summit', 80], ['Kalpesh', 77], ['Amit', 88], ['Tejas', 93],
['Abhishek', 65]];
var Avgmarks = 0;
for (var i=0; i < students.length; i++) {
Avgmarks += students[i][1];
}
var avg = (Avgmarks/students.length);
document.write("Average grade: " + (Avgmarks)/students.length);
document.write("<br>");
if (avg < 60){
document.write("Grade : E");
}
else if (avg < 70) {
document.write("Grade : D");
}
else if (avg < 80) {
document.write("Grade : C");
} else if (avg < 90) {
document.write("Grade : B");
} else if (avg < 100) {
document.write("Grade : A");
}
</script>
</body>
</html>

d) Write a JavaScript function that checks whether a passed string is palindrome or not.

<script>
function checkPalindrome(string)
{

// convert string to an array


const arrayValues = string.split('');

// reverse the array values


const reverseArrayValues = arrayValues.reverse();

// convert array to string


const reverseString = reverseArrayValues.join('');

if(string == reverseString) {
document.write('It is a palindrome');
}
else {
document.write('It is not a palindrome');
}
}

//take input
const string ="nitin" //prompt('Enter a string: ');

//checkPalindrome(string);
</script>

a) Differentiate between concat() and join() methods of array object.


concat() join()
Array elements can be combined by using concat() Array elements can be combined by using join()
method of Array object. method of Array object.
The concat() method separates each value with a The join() method also uses a comma to separate
comma. values, but you can specify a character other than
a comma to separate values.
Eg: var str = cars.concat() The value of str is 'BMW, Eg: var str = cars.join(' ') The value of str in this
Audi, Maruti' case is 'BMW Audi Maruti'

b) Write a JavaScript function to count the number of vowels in a given string.

<script>
var str="language";
var cnt=0;
for(var i=0;i<str.length;i++)
{
if(str[i]=='a'||str[i]=='e'||str[i]=='i'||str[i]=='o'||str[i]=='u')
{
cnt++;
}
}
document.write("number of vowel in a string:"+cnt);
</script>

c) Write a JavaScript that find and displays number of duplicate values in an array.

d) Develop JavaScript to convert the given character to Unicode and vice versa.

<html>
<head>
<title>String Demo </title>
</head>
<script language="Javascript" type="text/javascript">
var strl="Welcome to JavaScript";
document.write("<br>Unicode="+strl.charCodeAt(4));
document.write("<br>Unicode to character ="+String.fromCharCode(111));
</script>
</head>
<body>
</body>
</html>

(a) List any four features of Java script.

Features of java

Case sensitive
Control statement
Client side Technology
Interpreter based
In-built function
If-else statement
Light weighted
Looping statement
Scripting language
(b) List the comparison operators in Java script.

Operator Description
== Compares the equality of two operands without considering type
!= Compares inequality of two operands
> greater than
< less than
>= greater than or equal to
>= less than or equal to
(d) Write a Java script that initializes an array called flowers with the names of 3 flowers. The script then
displays array elements.

<html><body>
<script>
var flowers = new Array();
flowers[0] = 'Rose ';
flowers[1] = 'Mogra';
flowers[2] = 'Hibiscus';
for (var i = 0; i < flowers.length; i++)
{
document.write(flowers[i] + '<br>');
}
</script>
</body>
</html>

(e) Write Java script to call function from HTML.

<html>
<head>
<script>
function open()
{
alert("WElCOME");
}
function close()
{
alert("THANK YOU");
}
</script>
</head>
<body onload="open()" onunload="close()">

</body>
</html>

(f) Write a Java script to design a form to accept values for user ID & password.

4 marks

(a) Explain getter and setter properties in Java script with suitable example.
Property getters and setters
1. The accessor properties. They are essentially functions that work on getting and setting a
value.
2. Accessor properties are represented by “getter” and “setter” methods. In an object literal
they are denoted by get and set.
let obj = {
get propName()
{
// getter, the code executed on getting obj.propName
},
set propName(value) {
// setter, the code executed on setting obj.propName = value
} };
3. An object property is a name, a value and a set of attributes. The value may be replaced by
one or two methods, known as setter and a getter.
4. When program queries the value of an accessor property, Javascript invoke getter
method(passing no arguments). The retuen value of this method become the value of the
property access expression.
5. When program sets the value of an accessor property. Javascript invoke the setter method,
passing the value of right-hand side of assignment.
This method is responsible for setting the property value.
• If property has both getter and a setter method, it is read/write property.
• If property has only a getter method , it is read-only property.
• If property has only a setter method , it is a write-only property.
6. getter works when obj.propName is read, the setter – when it is assigned.
Example:

Getter:

<html>
<script>
var car={color:"blue",
brand:"ford",
get company(){
return this.color;
}

};
document.write("Car Color:"+car.company);
</script>
</html>

Setter:

<html>
<script>
var car={color:"blue",
brand:"ford",
set company(value){
this.color=value;
}
};
document.write("Car Color:"+car.color);
car.company="red";
document.write("Car Color:"+car.color);

</script>

</html>

(d) Write the use of CharAt() and indexof() with syntax and example.

charAt(): The charAt() method requires one argument i.e is the index of the character that you want to copy.
Syntax: var SingleCharacter = NameOfStringObject.charAt(index);

Example: var FirstName = 'Bob';

var Character = FirstName.charAt(0); //o/p B

indexOf(): The indexOf() method returns the index of the character passed to it as an argument. If the
character is not in the string, this method returns –1.

Syntax: var indexValue = string.indexOf('character');

Example: var FirstName = 'Bob'; var IndexValue = FirstName.indexOf('o'); //o/p index as 1

(a) Differentiate between concat () and join () methods of array object.


concat() join()
Array elements can be combined by using concat() Array elements can be combined by using join()
method of Array object. method of Array object.
The concat() method separates each value with a The join() method also uses a comma to separate
comma. values, but you can specify a character other than
a comma to separate values.
Eg: var str = cars.concat() The value of str is 'BMW, Eg: var str = cars.join(' ') The value of str in this
Audi, Maruti' case is 'BMW Audi Maruti'

(c) Write a Java script code to display 5 elements of array in sorted order

<html>
<script>
var arr=[34,56,78,90,36] ;
document.write(arr) ;
arr.sort();
document.write("<br>" + "Sorted Array"+arr) ;

</script>
</html>

You might also like