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

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)
4 views

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
You are on page 1/ 4

// 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 = today.getDay();

// Array of day names


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

// Display the current day


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

// Get the current hour, minute, and second


var hour = today.getHours();
var minute = today.getMinutes();
var second = today.getSeconds();

// 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


console.log ("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 = Math.sqrt(s * ((s - side1) * (s - side2) * (s - side3)));

// Log the calculated area to the console


console.log(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
console.log(leapyear(2016)); // Expected output: true
console.log(leapyear(2000)); // Expected output: true
console.log(leapyear(1700)); // Expected output: false
console.log(leapyear(1800)); // Expected output: false
console.log(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
console.log('--------------------');

// 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 (d.getDay() === 0) {
// Log a message if January 1st is a Sunday for the current year
console.log("1st January is being a Sunday " + year);
}
}

// Log another separator to conclude the output


console.log('--------------------');

// 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


console.log(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


console.log(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 = Math.ceil(Math.random() * 10);

// Log the generated random number to the console


console.log(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
console.log('Matched');
else
// Log a message if the guessed number does not match, and also provide the correct number
console.log('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 = document.getElementById("firstNumber").value;
num2 = document.getElementById("secondNumber").value;

// Set the inner HTML of the element with the id "result" to the product of the two numbers
document.getElementById("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 = document.getElementById("firstNumber").value;
num2 = document.getElementById("secondNumber").value;

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

You might also like