Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

CSS CT1 all

The document provides various JavaScript programming tasks and explanations, including the use of array sorting, objects, properties, methods, assignment operators, getters and setters, form events, mouse events, and JavaScript features. It includes code examples for creating and manipulating objects, handling user input with prompt and confirm methods, and displaying arrays. Additionally, it discusses the deletion of object properties and the use of event handlers in web development.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

CSS CT1 all

The document provides various JavaScript programming tasks and explanations, including the use of array sorting, objects, properties, methods, assignment operators, getters and setters, form events, mouse events, and JavaScript features. It includes code examples for creating and manipulating objects, handling user input with prompt and confirm methods, and displaying arrays. Additionally, it discusses the deletion of object properties and the use of event handlers in web development.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, 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)

The array.sort() is an inbuilt method in JavaScript which is used


to sort the array.
Syntax:
array.sort();

<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


entity, with properties and type.
 Each object has its unique identity based on fields,
buttons, 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 properties of an object are used to define the
characteristics of the object
 You access the properties of an object with a simple dot
notation.
 For example, A form object in a web page can have
properties 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 function associated with an
object to perform a specific task. Methods are defined
the way normal functions are defined, except that they
have to be assigned as the property of an object.
 For example, A submit button placed on a form is an
object. Clicking on submit button causes the button to
process a method i.e. when a click event occurs an
action is performed and method executes.

<html> <body>
<h2>JavaScript Objects</h2>
<p>An object method is a function definition, stored as a
property value.</p>
<script>
// Create an object: method.html
var person = {
firstName: "John",
lastName : "Doe",
id : 5566,
fullName : function() {
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. += (Addition 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 after this

operation, 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 existing value of the
variable then assign it the new added
value */
num +=10;

4)Explain the use of getters and setters.(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 after 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 isOften used to handle cleanup tasks or confirm user
actions before leaving the page.
c) onclick:
It executes when a mouse button is clicked over an element
Usage Commonly used to handle user interactions such as
button clicks.
d) ondblclick:
executes when the mouse button is double clicked on the
element
Usage: Used to handle actions that should happen on a
double-click, such as editing text or opening additional
information.

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


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

<html>
<head>
<title>Display Array Elements</title>
</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 scripting 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 scripting language.
6. JavaScript is case sensitive.
7. JavaScript is object based language as it provides
predefined objects.

9)Write Java script to create a person object with


properties firstname, last name, age, eye color, delete
eye color property and display remaining properties 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("After 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 buttons.
 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