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

Javascript Basic: Example of Output: Today Is: Tuesday

This document contains 4 JavaScript code examples: 1. A program to display the current day and time. 2. A program to print the contents of the current window. 3. A program to get the current date in different formats. 4. A program to calculate the area of a triangle given the lengths of its 3 sides.

Uploaded by

Mey Vaio
Copyright
© © All Rights Reserved
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

Javascript Basic: Example of Output: Today Is: Tuesday

This document contains 4 JavaScript code examples: 1. A program to display the current day and time. 2. A program to print the contents of the current window. 3. A program to get the current date in different formats. 4. A program to calculate the area of a triangle given the lengths of its 3 sides.

Uploaded by

Mey Vaio
Copyright
© © All Rights Reserved
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 3

JavaScript basic

1. Write a JavaScript program to display the current day and time in the following format.

Example of output: Today is: Tuesday.


The current time is: 22:00: 30: 38

JS

var today = new Date();


var day = today.getDay();
var daylist = ["Sunday","Monday","Tuesday","Wednesday ","Thursday","Friday","Saturday"];
console.log("Today is : " + daylist[day] + ".");
var hour = today.getHours();
var minute = today.getMinutes();
var second = today.getSeconds();
var prepand = (hour >= 12)? " PM ":" AM ";
hour = (hour >= 12)? hour - 12: hour;
if (hour===0 && prepand===' PM ')
{
if (minute===0 && second===0)
{
hour=12;
prepand=' Noon';
}
else
{
hour=12;
prepand=' PM';
}
}
if (hour===0 && prepand===' AM ')
{
if (minute===0 && second===0) Resualt :
{
hour=12; Today is : Saturday.
prepand=' Midnight'; Current Time : 8 AM : 38 : 37
}
else
{
hour=12;
prepand=' AM';
}
}
console.log("Current Time : "+hour + prepand + " : " + minute + " : " + second);
2. Write a JavaScript program to print the contents of the current window.

HTML

<body>
<p></p>
<p>Click the button to print the current page.</p>
<button onclick="print_current_page()">Print this page</button>
</body>

JS

function print_current_page()
{
window.print();
}

Resualt:

Click the button to print the current page.

Print this page

3. Write a JavaScript program to get the current date.


Expected output:
mm-dd-yyyy, mm / dd / yyyy or dd-mm-yyyy, dd / mm / yyy

JS

var today = new Date();


var dd = today.getDate();

var mm = today.getMonth()+1;
var yyyy = today.getFullYear();
if(dd<10)
{
dd='0'+dd;
}

if(mm<10)
{
mm='0'+mm;
}
today = mm+'-'+dd+'-'+yyyy;
console.log(today);
today = mm+'/'+dd+'/'+yyyy;
console.log(today);
today = dd+'-'+mm+'-'+yyyy;
console.log(today);
today = dd+'/'+mm+'/'+yyyy;
console.log(today);

Resualt:
01-15-2022
01/15/2022
15-01-2022
15/01/2022

4.Write a JavaScript function to find the area of a triangle where the lengths of the three sides are 5,
6, 7.

var side1 = 5;
var side2 = 6;
var side3 = 7;
var perimeter = (side1 + side2 + side3)/2;
var area = Math.sqrt(perimeter*((perimeter-side1)*(perimeter-side2)*(perimeter-side3)));
console.log(area);

Resualt:

14.696938456699069

You might also like