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

Exp 9

The document discusses JavaScript intrinsic functions, including Date, Math, Number, String, and Array functions. It provides examples of using these functions to get date/time values, perform mathematical operations, convert between data types, manipulate strings and arrays. The document also covers regular expression and form submission functions. It includes examples of programs that use these functions to check for prime numbers, validate palindromes, display the current date, remove duplicate array elements, merge and deduplicate two arrays, and submit/reset a form using intrinsic functions.

Uploaded by

labiot324
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

Exp 9

The document discusses JavaScript intrinsic functions, including Date, Math, Number, String, and Array functions. It provides examples of using these functions to get date/time values, perform mathematical operations, convert between data types, manipulate strings and arrays. The document also covers regular expression and form submission functions. It includes examples of programs that use these functions to check for prime numbers, validate palindromes, display the current date, remove duplicate array elements, merge and deduplicate two arrays, and submit/reset a form using intrinsic functions.

Uploaded by

labiot324
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Experiment No.

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.

Array Methods / Functions:


concat () Returns a new array comprised of this array joined with other array(s)
and/or value(s).
indexOf () Returns the first (least) index of an element within the array equal to the
specified value, or -1 if none is found.
join () Joins all elements of an array into a string.
pop () Removes the last element from an array and returns that element.
push () Adds one or more elements to the end of an array and returns the new
length of the array.
reverse () Reverses the order of the elements of an array -- the first becomes the
last, and the last becomes the first.
shift () Removes the first element from an array and returns that element.
sort () Sorts the elements of an array.

Regular Expression methods / functions:


test() Tests for a match in its string parameter.
match() Used to match a regular expression against a string.
Used to find a match between a regular expression and a string, and to replace
replace()
the matched substring with a new substring.
Executes the search for a match between a regular expression and a specified
search()
string.
exec() Executes a search for a match in its string parameter.
Special functions for form submit and reset:
JavaScript has a special set of functions called as intrinsic functions that mimic actions of the submit button
and reset button of a form. Intrinsic function is used to replace the submit button and reset button with
graphical images which are displayed on a form in place of these buttons.
<img> tag is used to display image. With onclick event appropriate intrinsic function is called and executed.

Syntax:
For submit function: - javascript:document.forms.form_name.submit()
For reset function: - javascript:document.forms.form_name.reset()

Programs:

Write a JavaScript program to check whether entered number is prime or not.


<html>
<body>
<script>
var i, chk=0;
var num = parseInt(prompt("Enter a positive number: "));
for(i=2; i<num; i++)
{
if(num%i==0)
{
chk++;
break;
}
}
if(chk==0)
document.write(num + " is a Prime Number");
else
document.write(num + " is not a Prime Number");
</script>
</body>
</html>

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>

var str = prompt ('Enter a string: ');


checkPalindrome(str);
</script>
</body>
</html>

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>

Program with inbuilt submit () and reset () functions:


<html>
<head>
</head>
<body>
<form name="f1" action="page1.html">
Enter username: <input type="text" name="username"><br>
Enter password: <input type="text" name="pwd"><br><br>
<img src="s1.jpg" width="50" height="50" onclick="javascript: document. forms. f1. submit ()">
<img src="s2.png" width="50" height="50" onclick="javascript: document. forms. f1. reset ()">
</form>
</body>
</html>

Conclusion:
With all the concepts based on JavaScript Intrinsic functions, successfully executed all programs with
correct output.

You might also like