Css Class Test1 qb-1
Css Class Test1 qb-1
• The Properties and methods associated with any object can be accessed by using the object name with
a dot syntax (period), e.g. user.fname, user.lname and user.fullName().
• Dot syntax is used to describe to Javascript how to interact with the objects, properties, functions and
events in an application.
Extending an Object:
• Add a new property to previously created object is called extending the object. Following example is
showing the concept of extending an object.
Example
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
</head>
<script>
var student =
{
firstName: "Amit",
lastName: "Gupta",
id: 12,
fullName: function () {
return this.firstName + " " + this.lastName
}
};
student.class = "Fifth";
document.write("Student Id = " + student.id);
document.write("<br/>");
document.write("Student Class = " + student.class);
document.write("<br/>");
document.write("Student Name = " + student.fullName());
</script>
<body>
</body>
</html>
Output:
Student Id = 12
Student Class = Fifth
Student Name = Amit Gupta
• Here, student class is created using literal notation. After creating a student class we are adding a new
property i.e. class.
2. List and explain Logical operators in JavaScript.
3. 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>
<head>
<title>Fruits</title>
<script language="javascript">
<body>
</body>
</html>
4.
<html>
<head>
<title>Compute the average marks and grade</title>
</head>
<body>
<script>
var students = [['Advait', 80], ['Anay', 77], ['Manyata', 88], ['Saanvi', 95],
['Saachi', 68]];
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>
Output:
Average grade: 81.6
Grade : B
7. Write a Java script to create a person object with properties firstname, lastname, age, eyecolor,
delete eyecolor property and display remaining properties of person object.
<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>
8. Write a Java script that initializes an array called flowers with the names of 3 flowers. The
script then displays array elements.
<html>
<head>
<title>Display Array Elements</title>
</head>
<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>
<html>
<head>
<title>Calling function from HTML</title>
<script>
function welcome() {
alert("Welcome students");
}
function goodbye() {
alert("Bye");
}
</script>
</head>
</html>
10. Write a Javascript to design a form to accept values for user ID & password.
<html>
<body>
<form name="login">
Enter Username <input type="text" name="userid"><br>
Enter Password <input type="password" name="pswrd">
<input type="button" onclick="display()" value="Display">
</form>
<script language="javascript">
function display() {
document.write("User ID = " + login.userid.value + "<br>
Password = " + login.pswrd.value);
}
</script>
</body>
</html>
11. Explain prompt() and confirm() method of Java script with syntax and 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.
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>
confirm()
It displays the confirm dialog box. It has message with ok and cancel buttons. Returns Boolean indicating
which button was pressed
Syntax:
window.confirm("sometext");
Example :
<html>
<script type="text/javascript">
function msg() {
var v = confirm("Are u sure?");
if (v == true) {
alert("ok");
}
else {
alert("cancel");
}
}
</script>
<input type="button" value="delete record" onclick="msg()" />
</html>
12. Write the use of chatAt() and indexof() with syntax and example.
JavaScript String - charAt() Method
Description
charAt() is a method that returns the character from the specified index.
Characters in a string are indexed from left to right. The index of the first character is 0, and the index of the last
character in a string, called stringName, is stringName.length – 1.
Syntax
string.charAt(index);
Argument Details
index − An integer between 0 and 1 less than the length of the string.
Return Value
Example
<html>
<head>
<title>JavaScript String charAt() Method</title>
</head>
<body>
<script type="text/javascript">
var str = new String("This is string");
document.writeln("str.charAt(0) is:" + str.charAt(0));
document.writeln("<br />str.charAt(1) is:" + str.charAt(1));
document.writeln("<br />str.charAt(2) is:" + str.charAt(2));
document.writeln("<br />str.charAt(3) is:" + str.charAt(3));
document.writeln("<br />str.charAt(4) is:" + str.charAt(4));
document.writeln("<br />str.charAt(5) is:" + str.charAt(5));
</script>
</body>
</html>
Description
This method returns the index within the calling String object of the first occurrence of the specified value, starting
the search at fromIndex or -1 if the value is not found.
Syntax
string.indexOf(searchValue[, fromIndex])
Argument Details
● searchValue − A string representing the value to search for.
● fromIndex − The location within the calling string to start the search from. It can be any integer
between 0 and the length of the string. The default value is 0.
Return Value
Returns the index of the found occurrence, otherwise -1 if not found.
Example
<html>
<head>
<title>JavaScript String indexOf() Method</title>
</head>
<body>
<script type="text/javascript">
var str1 = new String("This is string one");
var index = str1.indexOf("string");
document.write("indexOf found String :" + index);
document.write("<br />");
var index = str1.indexOf("one");
document.write("indexOf found String :" + index);
</script>
</body>
</html>
13. Write a JavaScript that will replace following specified value with another value in
string.
String = “I will fail”
Replace “fail” by “pass”
<html>
<head>
<body>
<script>
var myStr = 'I will fail';
document.write("String before replacement = "+myStr + "<br>");
</html>
Output:
String before replacement = I will fail
String after replacement = I will pass
<html>
<head>
<title> Array</title>
</head>
<body>
<script>
var arr1 = ["Red", "red", "Blue", "Green"];
var arr2 = [1, 20, 3, 45, 44, 0.5];
document.write("Before sorting array1 = " + arr1);
document.write("<br>After sorting array1 = " + arr1.sort());
document.write("<br>Before sorting array2 = " + arr2);
document.write("<br>After sorting array2 = " +
arr2.sort(function(a,b){return a-b}));
</script>
</body>
</html>
15. Develop JavaScript to convert the given character to Unicode and vice versa.
<html>
<head>
<title>Convert the Given Character to Unicode and vice versa</title>
<script>
function convert() {
var c = document.getElementById("input1");
var u = document.getElementById("input2");
if (c.value != "") {
u.value = c.value.charCodeAt();
}
else {
c.value = String.fromCharCode(u.value);
}
}
</script>
</head>
<body>
<p>Enter either Character or Unicode and click on CONVERT</p>
Character <input type="text" id="input1" /><br />
Unicode <input type="text" id="input2" /><br />
<input type="button" value="CONVERT" onclick="convert()" />
</body>
</html>
18. Write a JavaScript code to find out first and last index of given character from string.
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
</head>
<script>
var str = "Hello World";
document.write("Starting index of character l = " + str.indexOf('l') +
"<br>");
document.write("Last index of character l = " + str.lastIndexOf('l'));
</script>
<body>
</body>
</html>
Output :
Starting index of character l = 2
Last index of character l = 9
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
</head>
<script>
var a = new Array();
a[0] = " C";
a[1] = " C++";
document.write("Original Length = " + a.length + "<br>");
a.splice(2, 0, " Java", " PHP");
document.write("New Length = " + a.length + "<br>");
for (var i = 0; i < a.length; i++) {
document.write(a[i]);
}
</script>
<body>
</body>
</html>
Output:
Original Length = 2
New Length = 4
C C++ Java PHP
Removing element from existing array
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
</head>
<script>
var a = new Array();
a[0] = "C";
a[1] = "C++";
a[2] = "Java";
a[3] = "VB";
document.write("Original Length = " + a.length + "<br>");
a.splice(2);
document.write("New Length = " + a.length + "<br>");
for (var i = 0; i < a.length; i++) {
document.write(a[i]);
}
</script>
<body>
</body>
</html>
Output:
Original Length = 4
New Length = 2
CC++
20. Explain getter and setter properties in Java script with suitable example.
<html>
<head>
<title>Functions</title>
<body>
<script language="Javascript">
var myCar = {
/* Data properties */
defColor: "blue",
defMake: "Toyota",
/* Accessor properties (getters) */
get color() {
return this.defColor;
},
get make() {
return this.defMake;
},
/* Accessor properties (setters) */
set color(newColor) {
this.defColor = newColor;
},
set make(newMake) {
this.defMake = newMake;
}
};
document.write("Car color:" + myCar.color + " Car Make: " + myCar.make)
/* Calling the setter accessor properties */
myCar.color = "red";
myCar.make = "Audi";
/* Checking the new values with the getter accessor properties */
document.write("<p>Car color: " + myCar.color); // red
document.write(" Car Make: " + myCar.make); //Audi
</script>
</head>
</body>
</html>