Exp 9
Exp 9
9
Aim: Develop a web page using Intrinsic JavaScript Function.
Theory:
Intrinsic function is built-in function available in JavaScript for programmer’s use. JavaScript provides
intrinsic objects such as Array, Boolean, Date, Error, Function, Math, Number, Object, RegExp, String.
Date functions:
Date () Gets system date and time
getDate () Gets day from system date day as a number (1-31)
getMonth () Gets month from system date as a number (0-11)
getFullYear () Gets year from system data as a four-digit number (yyyy)
getTime () Gets system time
getHours () Gets hour as a number (0-23)
getMinutes () Gets minutes from system time (0-59)
getSeconds () Gets seconds from system time (0-59)
getMilliseconds () Gets milliseconds from system time (0-999)
getUTCDate () Returns the day (date) of the month in the specified date according to
universal time
setDate () Sets the day of the month for a specified date according to local time.
setMonth () Sets the month for a specified date according to local time.
setFullYear () Sets the full year for a specified date according to local time.
Sets the Date object to the time represented by a number of milliseconds
setTime ()
since January 1, 1970, 00:00:00 UTC.
setHours () Sets the hours for a specified date according to local time.
setMinutes () Sets the minutes for a specified date according to local time.
setSeconds () Sets the seconds for a specified date according to local time.
setMilliseconds () Sets the milliseconds for a specified date according to local time.
setUTCDate () Sets the day of the month for a specified date according to universal time.
setUTCMonth () Sets the month for a specified date according to universal time.
setUTCFullYear () Sets the full year for a specified date according to universal time.
setUTCHours () Sets the hour for a specified date according to universal time.
setUTCMinutes () Sets the minutes for a specified date according to universal time.
setUTCMilliseconds () Sets the milliseconds for a specified date according to universal time.
toGMTString () Converts a date to a string, using the Internet GMT conventions.
toUTCDate () Converts a date to a string, using the UTC conventions.
Math functions:
abs() Returns the absolute value of a number.
pow() Returns base to the exponent power, that is, base exponent.
sqrt() Returns the square root of a number.
max() Returns the largest of zero or more numbers.
min() Returns the smallest of zero or more numbers.
ceil() Returns the smallest integer greater than or equal to a number.
floor() Returns the largest integer less than or equal to a number.
round() Returns the value of a number rounded to the nearest integer.
log() Returns the natural logarithm (base E) of a number.
random() Returns a pseudo-random number between 0 and 1.
cos() Returns the cosine of a number.
sin() Returns the sine of a number.
tan() Returns the tangent of a number.
Number conversion methods / functions:
parseFloat () Parses a string and returns a floating-point number
parseInt () Parses a string and returns an integer
number () Converts string to number.
String functions:
charAt() Returns the character at the specified index.
Returns a number indicating the Unicode value of the character at the
charCodeAt()
given index.
concat() Combines the text of two strings and returns a new string.
Returns the index within the calling String object of the first occurrence
indexOf()
of the specified value, or -1 if not found.
Returns the index within the calling String object of the last occurrence of
lastIndexOf()
the specified value, or -1 if not found.
length() Returns the length of the string.
slice() Extracts a section of a string and returns a new string.
Splits a String object into an array of strings by separating the string into
split()
substrings.
Returns the characters in a string beginning at the specified location
substr()
through the specified number of characters.
substring() Returns the characters in a string between two indexes into the string.
toLowerCase() Returns the calling string value converted to lower case.
toString () Returns a string representing the specified object.
toUpperCase () Returns the calling string value converted to uppercase.
Syntax:
For submit function: - javascript:document.forms.form_name.submit()
For reset function: - javascript:document.forms.form_name.reset()
Programs:
Write a JavaScript to checks whether a passed string is palindrome or not using function.
<html>
<head>
<script type="text/javascript">
function checkPalindrome(str)
{
var arr = str.split('');
var reversearr = arr.reverse();
var reversestr = reversearr.join('');
if(str == reversestr)
{
document.write('It is a palindrome');
}
else
{
document.write('It is not a palindrome');
}
}
</script>
</head>
<body>
<script>
Write a JavaScript program that will display current date in DD/MM/YYYY format.
<html>
<body>
<script type="text/javascript">
var d=new Date();
var currentDate=d.getDate()+'/'+(d.getMonth()+1)+'/'+d.getFullYear()
document.write(currentDate)
</script>
</body>
</html>
Write a JavaScript program that will remove the duplicate element from an array.
<html>
<body>
<script>
var arr = ["scale", "happy", "strength", "peace", "happy", "happy"];
function removeDuplicates(arr)
{
var unique = [];
for (i = 0; i < arr.length; i++)
{
if (unique.indexOf(arr[i]) === -1)
{
unique.push(arr[i]);
}
}
return unique;
}
document.write(removeDuplicates(arr));
</script>
</body
</html>
Write a JavaScript function to merge two array & removes all duplicate values.
<html>
<body>
<script>
function mergearr(arr1, arr2)
{
var arr = arr1.concat(arr2);
var uniqueArr = [];
for(var i of arr)
{
if(uniqueArr.indexOf(i) === -1)
{
uniqueArr.push(i);
}
}
document.write(uniqueArr);
}
var array1 = [1,2,3,6,8];
var array2 = [2,3,5,56,78,3]
mergearr(array1, array2);
</script>
</body>
</html>
Conclusion:
With all the concepts based on JavaScript Intrinsic functions, successfully executed all programs with
correct output.