0% found this document useful (0 votes)
37 views4 pages

Basic JS Programs-1

The document contains JavaScript code that retrieves the current date and time, calculates the area of a triangle using Heron's formula, checks for leap years, and determines if January 1st falls on a Sunday between 2014 and 2050. It also includes functions for temperature conversion between Celsius and Fahrenheit, generates a random number for a guessing game, and defines functions for multiplying and dividing two numbers. The code demonstrates various programming concepts and functionalities in JavaScript.

Uploaded by

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

Basic JS Programs-1

The document contains JavaScript code that retrieves the current date and time, calculates the area of a triangle using Heron's formula, checks for leap years, and determines if January 1st falls on a Sunday between 2014 and 2050. It also includes functions for temperature conversion between Celsius and Fahrenheit, generates a random number for a guessing game, and defines functions for multiplying and dividing two numbers. The code demonstrates various programming concepts and functionalities in JavaScript.

Uploaded by

venkat Mohan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

// Get the current date and time

var today = new Date();

// Get the day of the week (0-6, where 0 is Sunday and 6 is Saturday)
var day = [Link]();

// Array of day names


var daylist = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];

// Display the current day


[Link]("Today is: " + daylist[day] + ".");

// Get the current hour, minute, and second


var hour = [Link]();
var minute = [Link]();
var second = [Link]();

// Determine if it's AM or PM
var prepand = (hour >= 12) ? " PM " : " AM ";

// Convert 24-hour format to 12-hour format


hour = (hour >= 12) ? hour - 12 : hour;

// Check for special cases when hour is 0


if (hour === 0 && prepand === ' PM ') {
if (minute === 0 && second === 0) {
hour = 12;
prepand = ' Noon';
} else {
hour = 12;
prepand = ' PM';
}
}

// Check for special cases when hour is 0


if (hour === 0 && prepand === ' AM ') {
if (minute === 0 && second === 0) {
hour = 12;
prepand = ' Midnight';
} else {
hour = 12;
prepand = ' AM';
}
}

// Display the current time


[Link] ("Current Time: " + hour + prepand + " : " + minute + " : " + second);
// Define the lengths of the three sides of a triangle
var side1 = 5;
var side2 = 6;
var side3 = 7;

// Calculate the semi-perimeter of the triangle


var s = (side1 + side2 + side3) / 2;

// Use Heron's formula to calculate the area of the triangle


var area = [Link](s * ((s - side1) * (s - side2) * (s - side3)));

// Log the calculated area to the console


[Link](area);

// Define a function to check if a given year is a leap year


function leapyear(year) {
// Return true if the year is divisible by 4 but not divisible by 100 unless it's also divisible by 400
return (year % 100 === 0) ? (year % 400 === 0) : (year % 4 === 0);
}

// Test the function with various years and log the results to the console
[Link](leapyear(2016)); // Expected output: true
[Link](leapyear(2000)); // Expected output: true
[Link](leapyear(1700)); // Expected output: false
[Link](leapyear(1800)); // Expected output: false
[Link](leapyear(100)); // Expected output: false

// Write a JavaScript program to find out if 1st January will be a Sunday between 2014 and
2050.
// Log a separator to visually distinguish the output
[Link]('--------------------');

// Loop through the years from 2014 to 2050 (inclusive)


for (var year = 2014; year <= 2050; year++) {
// Create a Date object for January 1st of the current year
var d = new Date(year, 0, 1);

// Check if January 1st is a Sunday (where Sunday corresponds to day index 0)


if ([Link]() === 0) {
// Log a message if January 1st is a Sunday for the current year
[Link]("1st January is being a Sunday " + year);
}
}

// Log another separator to conclude the output


[Link]('--------------------');

// Define a function to convert Celsius to Fahrenheit


function cToF(celsius) {
// Store the input Celsius temperature in a variable
var cTemp = celsius;
// Calculate the equivalent Fahrenheit temperature
var cToFahr = cTemp * 9 / 5 + 32;

// Create a message string describing the conversion result


var message = cTemp + '\xB0C is ' + cToFahr + ' \xB0F.';

// Log the message to the console


[Link](message);
}

// Define a function to convert Fahrenheit to Celsius


function fToC(fahrenheit) {
// Store the input Fahrenheit temperature in a variable
var fTemp = fahrenheit;

// Calculate the equivalent Celsius temperature


var fToCel = (fTemp - 32) * 5 / 9;

// Create a message string describing the conversion result


var message = fTemp + '\xB0F is ' + fToCel + '\xB0C.';

// Log the message to the console


[Link](message);
}

// Call the cToF function with a Celsius temperature of 60


cToF(60);

// Call the fToC function with a Fahrenheit temperature of 45


fToC(45);

// Generate a random integer between 1 and 10 (inclusive)


var num = [Link]([Link]() * 10);

// Log the generated random number to the console


[Link](num);

// Prompt the user to guess a number between 1 and 10 (inclusive)


var gnum = prompt('Guess the number between 1 and 10 inclusive');

// Check if the guessed number matches the generated random number


if (gnum == num)
// Log a message if the guessed number matches the random number
[Link]('Matched');
else
// Log a message if the guessed number does not match, and also provide the correct number
[Link]('Not matched, the number was ' + gnum);

// Define a function to multiply two numbers and display the result


function multiplyBy() {
// Get the values of the input fields with the ids "firstNumber" and "secondNumber"
num1 = [Link]("firstNumber").value;
num2 = [Link]("secondNumber").value;

// Set the inner HTML of the element with the id "result" to the product of the two numbers
[Link]("result").innerHTML = num1 * num2;
}

// Define a function to divide two numbers and display the result


function divideBy() {
// Get the values of the input fields with the ids "firstNumber" and "secondNumber"
num1 = [Link]("firstNumber").value;
num2 = [Link]("secondNumber").value;

// Set the inner HTML of the element with the id "result" to the quotient of the two numbers
[Link]("result").innerHTML = num1 / num2;
}

You might also like