Js Exercises
Js Exercises
1. Write a JavaScript program to display the current day and time in the following format.
Today is : Friday.
Current time is : 4 PM : 50 : 22
Sample Solution : HTML Code :
view plaincopy to clipboardprint?
1. <!DOCTYPE html>
2.
<html>
3.
<head>
4.
<meta charset="utf-8">
5.
6.
</head>
7.
<body></body>
8. </html>
JavaScript Code :
Explanation :
Declaring a JavaScript date : In JavaScript Date objects are based on a time value that is
the number of milliseconds since 1 January, 1970 UTC. You can declare a date in the
following ways :
new Date();
new Date(value);
new Date(dateString);
new Date(year, month[, day[, hour[, minutes[, seconds[, milliseconds]]]]]);
The getDay() method is used to get the day of the week for the specified date according to
local time, where 0 represents Sunday. The value returned by getDay() is an integer
corresponding to the day of the week: 0 for Sunday, 1 for Monday, 2 for Tuesday, and so
on.
The getHours() method is used to get the hour for a given date, according to local time.
The value returned by getHours() is an integer between 0 and 23.
The getMinutes() method is used to get the minutes in the specified date according to local
time. The value returned by getMinutes() is an integer between 0 and 59.
The getSeconds() method is used to get the seconds in the specified date according to
local time. The value returned by getSeconds() is an integer between 0 and 59.
AM and PM : AM stands for 'ante meridiem', which means 'before noon' in Latin, while PM
stands for 'post meridiem', which means 'after noon' in Latin.
12-Hour Periods : Nowadays most clocks are 12-hour clocks they divide the 24 hours in
a day into two 12-hour periods.
Ante meridiem: Before noon - Between midnight (0:00) & noon (12:00)
Post meridiem: After noon Between noon (12:00) & midnight (0:00)
1. <!DOCTYPE html>
2. <html>
3. <head>
4. <meta charset=utf-8 />
5. <title>Print the current page.</title>
6. </head>
7. <body>
8. <p></p>
JavaScript Code :
1. function print_current_page()
2. {
3. window.print();
4. }
Explanation :
window.print() : The window object represents a window containing a DOM document; the
document property points to the DOM document loaded in that window, window.print() is
used to open the Print Dialog to print the current document.
1. <!DOCTYPE html>
2.
<html>
3.
<head>
4.
<meta charset="utf-8">
5.
6.
</head>
7.
<body>
8.
</body>
9.
</html>
10.
JavaScript Code :
dd='0'+dd;
9. }
10.
11. if(mm<10)
12. {
13.
mm='0'+mm;
14. }
15. today = mm+'-'+dd+'-'+yyyy;
16. console.log(today);
17. today = mm+'/'+dd+'/'+yyyy;
18. console.log(today);
19. today = dd+'-'+mm+'-'+yyyy;
20. console.log(today);
21. today = dd+'/'+mm+'/'+yyyy;
22. console.log(today);
Explanation :
Declaring a JavaScript date : In JavaScript Date objects are based on a time value that is
the number of milliseconds since 1 January, 1970 UTC. You can declare a date in the
following ways :
new Date();
new Date(value);
new Date(dateString);
new Date(year, month[, day[, hour[, minutes[, seconds[, milliseconds]]]]]);
The getDate() method is used to get the day of the month for the specified date according
to local time. The value returned by getDate() is an integer between 1 and 31.
The getMonth() method returns the month in the specified date according to local time, as
a zero-based value (where zero indicates the first month of the year). The value returned
by getMonth() is an integer between 0 and 11. 0 corresponds to January, 1 to February,
and so on.
The getFullYear() method is used to get the year of the specified date according to local
time. The value returned by the method is an absolute number. For dates between the
years 1000 and 9999, getFullYear() returns a four-digit number, for example, 1985.
4. Rotate the string 'w3resource' in right direction by periodically removing one letter from
the end of the string and attaching it to the front.
Sample Solution : HTML Code :
view plaincopy to clipboardprint?
1. <!DOCTYPE html>
2.
<html>
3.
<head>
4.
5.
<script type="text/javascript">
6.
</script>
7.
8.
9.
</body>
10. </html>
JavaScript Code :
1. function animate_string(id)
2. {
3.
4.
5.
6.
7. setInterval(function ()
8. {
9.
Explanation :
document.getElementById(id) : Returns a reference to the element by its ID; the ID is a
string which can be used to identify the element; it can be established using the id attribute
in HTML, or from script.
Parameters : id is a case-sensitive string representing the unique ID of the element being
sought.
element.childNodes[0] will produce the same result as the HTML content of the first child
node.
text.length : The length property represents the length of a string. It returns the number of
code units in the string.
5. Write a JavaScript program to find which 1st January is being a Sunday between 2014
and 2050.
Sample Solution : HTML Code :
view plaincopy to clipboardprint?
1. <!DOCTYPE html>
2. <html>
3. <head>
4. <meta charset=utf-8 />
5. <title>A Program to find 1st January <span class="heading">is being </span>a Sunday
between 2014 and 2050.</title>
6. </head>
7. <body>
8. </body>
9. </html>
JavaScript Code :
1. console.log('--------------------');
2. for (var year = 2014; year <= 2050; year++)
3.
4.
5.
if ( d.getDay() === 0 )
6.
7.
8. console.log('--------------------');
Explanation :
Declaring a JavaScript date : In JavaScript Date objects are based on a time value that is
the number of milliseconds since 1 January, 1970 UTC. You can declare a date in the
following ways :
new Date();
new Date(value);
new Date(dateString);
new Date(year, month[, day[, hour[, minutes[, seconds[, milliseconds]]]]]);
The getDay() method is used to get the day of the week for the specified date according to
local time, where 0 represents Sunday. The value returned by getDay() is an integer
corresponding to the day of the week: 0 for Sunday, 1 for Monday, 2 for Tuesday, and so
on.
6. Write a JavaScript program where the program takes a random integer between 1 to 10,
the user is then prompted to input a guess number. If the user input matches with guess
number, the program will display a message "Good Work" otherwise display a message
"Not matched"
Sample Solution : HTML Code :
view plaincopy to clipboardprint?
1. <!DOCTYPE html>
2. <html>
3. <head>
4. <meta charset=utf-8 />
5. <title>Guess a number</title>
6. </head>
7. <body>
8. </body>
9. </html>
JavaScript Code :
3.
4.
if (gnum == num)
5.
alert('Matched');
6.
7.
else
alert('Not matched, the number was ' + num);
Explanation :
The Math.ceil() function is used to get the smallest integer greater than or equal to a given
number.
The Math.random() function is used to get a floating-point, pseudo-random number in the
range [0, 1) that is, from 0 (inclusive) up to but not including 1 (exclusive), which you can
then scale to your desired range.
1. <!DOCTYPE html>
2. <html>
3. <head>
4. <meta charset="utf-8">
5. <title>Write a JavaScript program to get the website URL (loading page)</title>
6. </head>
7. <body>
8. </body>
9. </html>
JavaScript Code :
Explanation :
document.URL : The URL read-only property of the Document interface returns the
document location as a string.