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

Javascript Project File

The document contains 10 JavaScript programs that demonstrate various date and time functions, string manipulation, mathematical calculations, and user input/output. The programs each contain the full code to display the output of short JavaScript tasks.

Uploaded by

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

Javascript Project File

The document contains 10 JavaScript programs that demonstrate various date and time functions, string manipulation, mathematical calculations, and user input/output. The programs each contain the full code to display the output of short JavaScript tasks.

Uploaded by

tyhxondukd
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

1.

Write a JavaScript program to display the current day and


time .

<!DOCTYPE html>
<html lang="en">
<head>
<title>Display Current Day and Time</title>
</head>
<body>

<h1>Display Current Day and Time</h1>


<div id="output"></div>

<script>
function displayCurrentDayAndTime() {
let now = new Date();
let days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday",
"Saturday"];
let day = days[now.getDay()];
let time = now.toLocaleTimeString();
document.getElementById('output').innerText = `Today is: ${day}\nCurrent time is:
${time}`;
}
</script>

</body>
</html>

OUTPUT :
2. Write a JavaScript program to print the contents of the
current window.

<!DOCTYPE html>
<html lang="en">
<head>
<title>Print Current Window</title>
</head>
<body>

<h1>Print Current Window</h1>


<button onclick="printCurrentWindow()">Print</button>

<script>
function printCurrentWindow() {
window.print();
}
</script>

</body>
</html>

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

<!DOCTYPE html>
<html lang="en">
<head>
<title>Get Current Date</title>
</head>
<body>

<h1>Get Current Date</h1>


<div id="output"></div>

<script>
function getCurrentDate() {
let now = new Date();
let date = now.toLocaleDateString();
document.getElementById('output').innerText = `Current date is: ${date}`;
}
getCurrentDate()
</script>

</body>
</html>

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

<!DOCTYPE html>
<html lang="en">
<head>
<title>Triangle Area</title>
</head>
<body>

<h1>Find Triangle Area</h1>


<div id="output"></div>

<script>
function displayTriangleArea() {
let a = 5, b = 6, c = 7;
let s = (a + b + c) / 2;
let area = Math.sqrt(s * (s - a) * (s - b) * (s - c));
document.getElementById('output').innerText = `Area of the triangle: ${area}`;
}
displayTriangleArea()
</script>

</body>
</html>

OUTPUT :
5.Write a JavaScript program to rotate the string 'resource' in
right direction by periodically removing one letter from the end of
the string and attaching it to the front.

<!DOCTYPE html>
<html lang="en">
<head>
<title>Rotate String</title>
</head>
<body>
<h1>Rotate String 'resource'</h1>
<div id="output"></div>

<script>
function rotateString(str) {
let arr = str.split('');
setInterval(function() {
let lastChar = arr.pop();
arr.unshift(lastChar);
document.getElementById('output').innerText = arr.join('');
}, 1000);
}
rotateString('resource')
</script>

</body>
</html>

OUTPUT :
6. Write a JavaScript program to determine whether a given year
is a leap year in the Gregorian calendar.

<!DOCTYPE html>
<html lang="en">
<head>
<title>Check Leap Year</title>
</head>
<body>

<h1>Check Leap Year</h1>


<div id="output"></div>

<script>
function checkLeapYear() {
let year = parseInt(prompt("Enter a year to check if it's a leap year:"));
let isLeap = ((year % 4 === 0 && year % 100 !== 0) || (year % 400 === 0));
document.getElementById('output').innerText = `${year} is a leap year: ${isLeap}`;
}
checkLeapYear();
</script>

</body>
</html>

OUTPUT :
7. Write a JavaScript program to find 1st January is being a
Sunday between 2014 and 2050.

<!DOCTYPE html>
<html lang="en">
<head>
<title>Find Sundays on 1st January</title>
</head>
<body>

<h1>Find Sundays on 1st January (2014-2050)</h1>


<div id="output"></div>

<script>
function findSundayYears() {
let output = '';
for (let year = 2014; year <= 2050; year++) {
let date = new Date(year, 0, 1); // January 1st of the year
if (date.getDay() === 0) {
output += `1st January is a Sunday in the year: ${year}\n`;
}
}
document.getElementById('output').innerText = output;
}
findSundayYears();
</script>

</body>
</html>

OUTPUT :
8. 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 Work"
otherwise display a message "Not matched".

<!DOCTYPE html>
<html lang="en">
<head>
<title>Guessing Game</title>
</head>
<body>

<h1>Guessing Game</h1>
<button onclick="guessingGame()">Play Game</button>

<script>
function guessingGame() {
let randomNumber = Math.floor(Math.random() * 10) + 1;
let userGuess = prompt("Guess a number between 1 and 10:");

if (parseInt(userGuess) === randomNumber) {


alert("Good Work");
} else {
alert("Not matched. The number was " + randomNumber);
}
}
</script>

</body>
</html>
OUTPUT :
9. Write a JavaScript program to calculate days left until next
Christmas.

<!DOCTYPE html>
<html lang="en">
<head>
<title>Days Until Christmas</title>
</head>
<body>

<h1>Days Until Christmas</h1>


<div id="output"></div>

<script>
function daysUntilChristmas() {
let now = new Date();
let year = now.getFullYear();
let christmas = new Date(year, 11, 25);

if (now.getTime() > christmas.getTime()) {


christmas.setFullYear(year + 1);
}

let oneDay = 24 * 60 * 60 * 1000;


let daysLeft = Math.round((christmas.getTime() - now.getTime()) / oneDay);

document.getElementById('output').innerText = `Days until next Christmas: ${daysLeft}`;


}
daysUntilChristmas();
</script>

</body>
</html>
OUTPUT :
10. Write a JavaScript program to calculate multiplication and
division of two numbers (input from user).

<!DOCTYPE html>
<html lang="en">
<head>
<title>Multiplication and Division</title>
</head>
<body>

<h1>Multiplication and Division</h1>


<button onclick="calculateMultiplicationAndDivision()">Calculate</button>
<div id="output"></div>

<script>
function calculateMultiplicationAndDivision() {
let num1 = parseFloat(prompt("Enter the first number:"));
let num2 = parseFloat(prompt("Enter the second number:"));

let multiplication = num1 * num2;


let division = num1 / num2;

document.getElementById('output').innerText = `Multiplication result:


${multiplication}\nDivision result: ${division}`;
}
</script>

</body>
</html>
OUTPUT :

You might also like