CAP214 –
Assignment-3
Name- Prabhakar Priyadarshi
Registration Number- 12010230
Roll No.- RD2001B69
Set-A
1. Write a JavaScript program to get the current date.
HTML Code:
<!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:
2. Write a JavaScript program to determine whether a given
year is a leap year in the Gregorian calendar.
JavaScript Code:
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:
3. 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 "
otherwise display a message "Not mapped"
4. Write a JavaScript program to calculate days left until next
New Year 2021.
HTML Code:
<!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:
5. Write a JavaScript program to check the total marks of a
student in various examinations. The student will get A+
grade if the total marks are in the range 89..100 inclusive, if
the examination is "Final-exam." the student will get A+
grade and total marks must be greater than or equal to 90.
Return true if the student get A+ grade or false otherwise.
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:
6. Write a JavaScript program to sort a list of elements using
Heap sort.
JavaScript Code:
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);
}
}
var arr = [3, 0, 2, 5, -1, 4, 1];
heapSort(arr);
console.log(arr);
Output Screen:
7. Write a JavaScript program to calculate multiplication and
division of two numbers (input from user).
HTML Code:
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>JavaScript program to calculate multiplication and division
of two numbers </title>
<style type="text/css">
body {margin: 30px;}
</style>
</head>
<body>
<form>
1st Number : <input type="text" id="firstNumber" /><br>
2nd Number: <input type="text" id="secondNumber" /><br>
<input type="button" onClick="multiplyBy()" Value="Multiply" />
<input type="button" onClick="divideBy()" Value="Divide" />
</form>
<p>The Result is : <br>
<span id = "result"></span>
</p>
</body>
</html>
JavaScript Code:
function multiplyBy()
{
num1 = document.getElementById("firstNumber").value;
num2 = document.getElementById("secondNumber").value;
document.getElementById("result").innerHTML = num1 *
num2;
}
function divideBy()
{
num1 = document.getElementById("firstNumber").value;
num2 = document.getElementById("secondNumber").value;
document.getElementById("result").innerHTML = num1 / num2;
}
Output Screen:
8. Write a Java Script Program to print the prime numbers
from 1 to 50.
JavaScript Code:
function primeFactorsTo(max)
{
var store = [], i, j, primes = [];
for (i = 2; i <= max; ++i)
{
if (!store [i])
{
primes.push(i);
for (j = i << 1; j <= max; j += i)
{
store[j] = true;
}
}
}
return primes;
}
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:
10. Write a JavaScript program to get the difference between
a given number and 100, if the number is greater than 100
return double the absolute difference.
HTML Code:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>The difference between a given number and 100, if the
number is greater than 100 return double the absolute
difference.</title>
</head>
<body>
</body>
</html>
JavaScript Code:
function difference(n)
{
if (n <= 100)
return 100 - n;
else
return (n - 100) * 2;
}
console.log(difference(100))
Output Screen: