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

CSS QB CT1 STARTING 10

The document provides a series of JavaScript programming exercises and explanations, covering topics such as array sorting, objects, properties, methods, assignment operators, getters and setters, form events, mouse events, and the prompt and confirm methods. Each section includes code examples and descriptions of JavaScript features and functionalities. It serves as a practical guide for understanding and implementing various JavaScript concepts.

Uploaded by

mohitrohramusic
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

CSS QB CT1 STARTING 10

The document provides a series of JavaScript programming exercises and explanations, covering topics such as array sorting, objects, properties, methods, assignment operators, getters and setters, form events, mouse events, and the prompt and confirm methods. Each section includes code examples and descriptions of JavaScript features and functionalities. It serves as a practical guide for understanding and implementing various JavaScript concepts.

Uploaded by

mohitrohramusic
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

CSS QB SOLUTION CT 1

1) Write a program using the sort method of array


objects.(CO2/A)

<html>
<body>
<script>
var array =[5,1,9,7,5];
// sortng the array
sorted = array.sort();
document.write(sorted);
</script>
</body>
</html>
2) State the use of Object, Method and Property in
JavaScript.(CO1/R)

Object:

 In JavaScript, an object is used to represent standalone


en ty, with proper es and type.
 Each object has its unique iden ty based on fields,
bu ons, interface elements, etc
 For example, two forms placed on web page can have
different elements and interface with respect to their
use.
 So, each form can have unique name or id that can be
referenced by JavaScript.

Example of object:

var person = {
firstName: "John",
lastName: "Doe",
age: 50,
eyeColor: "blue"
};
Property:
 A property is a value that is associated with an object.
The proper es of an object are used to define the
characteris cs of the object
 You access the proper es of an object with a simple dot
nota on.
 For example, A form object in a web page can have
proper es like width, height, etc.

Example:-
<html>
<body>
<h2>JavaScript Objects and Property usage</h2>
<script>
var person = { };
person['firstname'] = 'Maanav';
person['lastname'] = 'Desai';
document.write(person.firstname + " "+person.lastname
+"<P>");
person = {'firstname': 'John', 'lastname': 'Patel'}
document.write(person['firstname'] +" "+
person['lastname']);
</script>
</body>
</html>
Method:
 A method is used to define a func on associated with an
object to perform a specific task. Methods are defined
the way normal func ons are defined, except that they
have to be assigned as the property of an object.
 For example, A submit bu on placed on a form is an
object. Clicking on submit bu on causes the bu on to
process a method i.e. when a click event occurs an
ac on is performed and method executes.

<html> <body>
<h2>JavaScript Objects</h2>
<p>An object method is a func on defini on, stored as a
property value.</p>
<script>
// Create an object: method.html
var person = {
firstName: "John",
lastName : "Doe",
id : 5566,
fullName : func on() {
return this.firstName + " " + this.lastName +" has id
"+this.id;
} };
// Display data from the object:
document.write( person.fullName());
</script> </body> </html>
3)List various assignment operators supported by
JavaScript, explain any two with the help of suitable
examples.(CO1/R)

Assignment Operators in JavaScript


1. = (Assignment Operator)
o Purpose: Assigns a value to a variable.

o Example: var num = 10;

 This statement creates a variable named num

and assigns it the value 10.


2. += (Addi on Assignment Operator)
o Purpose: Adds the right-hand value to the variable

and then assigns the result back to the variable.


o Example: num += 10;

 If num was previously 10, then a er this

opera on, num becomes 20

Example:
<script>
// = is used to assign a value to a variable
var num = 10;
document.write(num); // num 10
/* += first add the value to the exis ng value of the
variable then assign it the new added
value */
num +=10;

4)Explain the use of ge ers and se ers.(CO1/U)

example:

<!DOCTYPE html>
<html>
<body>
<script>
var car = {
brand: "Toyota",
color: "Blue",

get getBrand () {
return this.brand;
},

get getColor () {
return this.color;},
set setBrand (newBrand)
{
this.brand = newBrand;
},
set setColor (newColor) {
this.color = newColor; }
};
document.write("Car Brand: " + car.brand + "Car Color: " +
car.color);
car.setBrand = "Tesla";
car.setColor = "Red";
document.write("Car Brand: " + car.brand + "Car Color: " +
car.color);
</script>
</body>
</html>

5) List and explain any 4 events of the form.(CO3/R)

a) onload:
It executes when the browser finishes loading a window or all
frames within a frameset
Usage It is Commonly used to execute code a er a web page
or element has fully loaded.

b) onunload:
It executes when the browser removes a document from a
window or a frame
Usage It isO en used to handle cleanup tasks or confirm user
ac ons before leaving the page.

c) onclick:
It executes when a mouse bu on is clicked over an element
Usage Commonly used to handle user interac ons such as
bu on clicks.
d) ondblclick:
executes when the mouse bu on is double clicked on the
element
Usage: Used to handle ac ons that should happen on a
double-click, such as edi ng text or opening addi onal
informa on.

6) List different mouse events.(CO3/R)


7) Write a JavaScript that ini alizes an array called
“Fruits” with names of five fruits. The script then
displays the array in a message box.(CO2/A)

<html>
<head>
< tle>Display Array Elements</ tle>
</head>
<body>
<script>
var fruits = new Array();
fruits[0] = ‘mango ';
fruits[1] = 'apple';
fruits [2] = 'kiwi';
fruits[3] = 'cherry';
fruits[4] = 'banana';
alert(fruits);
</script>
</body>
</html>
8) State the features of Javascript.(CO2/R)

1. It is an object-based scrip ng language.


2. It gives the user more control over the browser.
3. It is light weighted. 4. Client – Side Technology
5. JavaScript is interpreter based scrip ng language.
6. JavaScript is case sensi ve.
7. JavaScript is object based language as it provides
predefined objects.

9)Write Java script to create a person object with


proper es firstname, last name, age, eye color, delete
eye color property and display remaining proper es of
person object.(CO2/A)

<html>
<body>
<script>
var person = {
firstname:"John",
lastname:"Doe",
age:50,
eyecolor:"blue"
};
delete person.eyecolor; //delete person eyecolor
document.write("A er delete "+ person.firstname +" "+
person.lastname +" "
+person.age +" "+ person.eyecolor);
</script>
</body>
</html>

10) Explain prompt () and confirm () method of Java


script with syntax and give one example.(CO1/U)

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">
funcƟon msg(){
var v= prompt("Who are you?");
alert("I am "+v);
}
</script>
<input type="buƩon" value="click" onclick="msg()"/>
</html>
confirm()
 It displays the confirm dialog box. It has message with
ok and cancel bu ons.
 Returns Boolean indicaƟng which buƩon was pressed
 Syntax: window.confirm("sometext");

 Example :
<html>
<script type="text/javascript">
funcƟon msg(){
var v= confirm("Are u sure?");
if(v==true){
alert("ok");
}
else{
alert("cancel");
}
}
</script>
<input type="buƩon" value="delete record"
onclick="msg()"/>
</html>

You might also like