Write A JavaScript Program To Get The Current Date
Write A JavaScript Program To Get The Current Date
Assignment-3
Name- Prabhakar Priyadarshi
Registration Number- 12010230
Roll No.- RD2001B69
Set-A
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Get the current date</title>
</head>
<body>
</body>
</html>
JavaScript Code:
var curday = function(sp){
today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1; //As January is 0.
var yyyy = today.getFullYear();
if(dd<10) dd='0'+dd;
if(mm<10) mm='0'+mm;
return (mm+sp+dd+sp+yyyy);
};
console.log(curday('/'));
console.log(curday('-'));
Output Screen:
function leapyear(year)
{
return (year % 100 === 0) ? (year % 400 === 0) : (year % 4 ===
0);
}
console.log(leapyear(2016));
console.log(leapyear(2000));
console.log(leapyear(1700));
console.log(leapyear(1800));
console.log(leapyear(100));
Output Screen:
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>Write a JavaScript program to calculate days left until next
Christmas</title>
</head>
<body>
</body>
</html>
JavaScript Code:
today=new Date();
var cmas=new Date(today.getFullYear(), 11, 25);
if (today.getMonth()==11 && today.getDate()>25)
{
cmas.setFullYear(cmas.getFullYear()+1);
}
var one_day=1000*60*60*24;
console.log(Math.ceil((cmas.getTime()-today.getTime())/
(one_day))+
" days left until Christmas!");
Output Screen:
HTML Code:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JavaScript program to check the total marks of a student
in various examinations</title>
</head>
<body>
</body>
</html>
JavaScript Code:
function exam_status(totmarks,is_exam)
{
if (is_exam) {
return totmarks >= 90;
}
return (totmarks >= 89 && totmarks <= 100);
}
console.log(exam_status("78", " "));
console.log(exam_status("89", "true "));
console.log(exam_status("99", "true "));
Output Screen:
var array_length;
/* to create MAX array */
function heap_root(input, i) {
var left = 2 * i + 1;
var right = 2 * i + 2;
var max = i;
if (left < array_length && input[left] > input[max]) {
max = left;
}
if (right < array_length && input[right] > input[max]) {
max = right;
}
if (max != i) {
swap(input, i, max);
heap_root(input, max);
}
}
function swap(input, index_A, index_B) {
var temp = input[index_A];
input[index_A] = input[index_B];
input[index_B] = temp;
}
function heapSort(input) {
array_length = input.length;
for (var i = Math.floor(array_length / 2); i >= 0; i -= 1) {
heap_root(input, i);
}
for (i = input.length - 1; i > 0; i--) {
swap(input, 0, i);
array_length--;
heap_root(input, 0);
}
}
function divideBy()
{
num1 = document.getElementById("firstNumber").value;
num2 = document.getElementById("secondNumber").value;
document.getElementById("result").innerHTML = num1 / num2;
}
Output Screen:
console.log(primeFactorsTo(50));
Output Screen:
[1, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, and 47 ]
9. Write a JavaScript program to convert temperatures to and
from Fahrenheit, Celsius. Formula : c/5 = (f-32)/9 where c =
temperature in Celsius and f = temperature in Fahrenheit
HTML Code:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Write a JavaScript program to convert temperatures to
and from celsius, fahrenheit</title>
</head>
<body>
</body>
</html>
JavaScript Code:
function cToF(celsius)
{
var cTemp = celsius;
var cToFahr = cTemp * 9 / 5 + 32;
var message = cTemp+'\xB0C is ' + cToFahr + ' \xB0F.';
console.log(message);
}
function fToC(fahrenheit)
{
var fTemp = fahrenheit;
var fToCel = (fTemp - 32) * 5 / 9;
var message = fTemp+'\xB0F is ' + fToCel + '\xB0C.';
console.log(message);
}
cToF(60);
fToC(45);
Output Screen:
Output Screen: