Arrays & Date
Arrays & Date
1. Write the difference between object based programming and Object oriented programming?
Object based programming does not support user defined classes. It does not support the concept of
Inheritance also. JavaScript is an object based scripting language.
Object Oriented Programming supports Classes and Inheritance. Java and C++ are examples of
OOP.
2. What is an Object?
An Object is a programming abstraction that groups data with the code that operates on it. Objects
have its own properties and methods. An object can be created using the new keyword and its
constructor.
Constructor is a function with the same name of the class. Dot operator is used to access an
object’s property or method.
var ar=new Array( );
here ar is the object . Array( ) is the constructor
3. Name 3 Native JavaScript classes.
ARRAYS
1. What are scalar variables?
Variables that can be assigned with a single value is called as scalar variables.
A=10;
B=”hello”
2. What is an Array?
An array is a variable which can hold a group of values in it. They are built in Objects in java script.
Each value in an array is called as an Element. Each Element is referenced by an index or subscript.
If there are n elements in an array, their indices range from 0 to n-1
Use the following JavaScript statements to create an array
var ar=new Array();
var ar1=new Array(3);
var marks=new Array(11,44,56);
To create an array object the new keyword is used along with the constructor (Array())
11 44 56
marks[0] marks[1] marks[2]
Array&Date 1 AnupA
3. Explain the length property of an Array object
length returns the number of elements in the array
var names=new Array("ann", "arun", "cathy");
document.write(names.length)
output
4. Array methods
concat(array2) – It concatenates 2 arrays and returns the new array. The method is used on the first
array and pass the name of the second array as its parameter.
output
11,44,55,2,77,99,100
join ( delimiter) – It joins all the elements in an array into a single string separated by the given
delimiter
output
11*44*55
Ar1=new Array(11,44,55)
document. write (Ar1.reverse())
output
55,44,11
Array&Date 2 AnupA
sort( ) – It sorts the elements in the array in ascending order according to the Unicode character
output
output
tostring( ) – Converts the array elements into a string where the array elements are separated by
commas
output
Ar1 =new Array(11,41,5, 25)
document. write (Ar1.tostring( ) ) 11,41,5,25
5. Write a program to initialize an array marks with 5 subjects marks of a student. Find the total and
average marks of the student.
<SCRIPT language="JavaScript">
var marks = new Array(78.5, 81, 87,65.5, 93)
var total=0, avg=0;
for(i=0;i<ar.length;i++)
{
total=total+ ar [i];
}
avg= total/5;
document.write(“total marks=”+total+”<br>”);
document.write(“average marks=”+avg+”<br>”);
Array&Date 3 AnupA
for – in loop
This loop works with Arrays or objects
for ..in loop is similar to for loop but instead of repeating a loop for a number of
times, it repeats the loop for each element of a specified collection of items
<html>
<body>
<script>
var names=new Array ("ann", "ben", "cathy")
for(i in names)
{
document.write(names[i]+"<BR>");
}
</script>
</body>
</html>
Output
ann
ben
cathy
Array&Date 4 AnupA
Date Object
1. How to create a Date Object?
Date is a built in class in java script. A Date Object can be created using the new key
word and a constructor as follows
To print the current date and time
var d1 =new Date( );
document. write(d1); //will print the current date and time
var d2=new Date(“January 1 2019”); //it will set only the date
document. write(d2);
var d3= new Date(year, month, date, hours, minutes, seconds, milliseconds);
Array&Date 5 AnupA
var d=new Date( ) // d will store the current date and time
document.write (d.getYear( ) ); //it counts from 1900
document.write (d.getFullYear( ) ); //retrieves the year as a four digit number
119
2019
5, 23, 50
Write a JavaScript code to calculate the date 12 months from now and display it on
the web page
var d=new Date( );
document.write(d+"<br>")
d.setMonth(d.getMonth( )+12)
document.write(d)
Array&Date 6 AnupA