Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

1) Write a JavaScript program to determine whether a given year is a leap year

<html>
<head>
<script>
function check_leapyear(){
//define variables
var year;

//get the entered year from text box


year = document.getElementById("year").value;

//three conditions to find out the leap year


if( (0 == year % 4) && (0 != year % 100) || (0 == year % 400) )
{
alert(year+" is a leap year");
}
else
{
alert(year+" is not a leap year");
}
}
</script>
</head>
<body>
<h3>Javascript Program to find leap year</h3>
<input type="text" id="year"></input>
<button onclick="check_leapyear()">Check</button>
</body>
</html>
2) 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".

<html>
<head>
<script>

function check()
{
var x = Math.floor((Math.random() * 10) + 1);
var i = document.getElementById('inputField').value;
var feedback = document.getElementById('feedback');
if (i == x)
{
feedback.innerHTML = 'Good Work';
}
else
{
feedback.innerHTML = 'Not matched';
}
}

</script>
</head>
<body>
<input id="inputField" type=text />
<input type="button" onclick="check()" value="check" />
<p id="random"></p>
<div id="feedback"></div>
</body>
</html>
3) Write a JavaScript program to calculate multiplication and division of two numbers

<html>
<head>
<script>
function calculate() {
var x = document.getElementById("first").value;
var y = document.getElementById("second").value;
var d = document.getElementById("decision").value;
if (d=="*")
result = x*y;
else if(d=="/")
result = x/y;
alert(result)}
</script>
<title></title>
</head>
<body>
<h1></h1>
<select id="decision">
<option value="*">Multiply</option>
<option value="/">Divide</option>
</select><br>
<input id="first" type="text">
<input id="second" type="text"><br>
<input type="button" value="Calculate" onclick="calculate()">
</body>
</html>
4) Write a JavaScript program to convert temperatures to and from Celsius, Fahrenheit.

<html>
<head>
<script>

function findCelsius(){
var fahrenheit = document.getElementById("fahrenheit").value;
var celsius;
if(fahrenheit != '’)
{
celsius = (fahrenheit - 32) * 5/9;
document.getElementById("output").innerHTML = celsius;
}
else
{
document.getElementById("output").innerHTML = "Please enter a value!";
}
}

</script>
</head>
<body>
<h2>Enter Fahrenheit</h2>
<input type="text" id="fahrenheit" />
<button onclick="findCelsius()" >Find Celsius</button>

<h4>Output: <span id="output"></span></h4>


</body>
</html>
5) Write a JavaScript program to compute the sum of the two given integers. If the two
values are same, then returns triple their sum.

<html>
<head>
<script>
function sumTriple () {
var x = document.getElementById("first").value;
var y = document.getElementById("second").value;

if (x == y) {
var z= 3 * (x + y);
alert(z);
}
else
{
Var z= (x + y);
alert(z);
}
}
</script>
</head>
<body>
<input id="first" type="text">
<input id="second" type="text"><br>
<input type="button" value="Calculate" onclick="sumTriple()">
</body>
</html>
6) Write a JavaScript program to reverse a given string.

<html>
<head>
<script>

//usage
function doReverse(){
var str = document.getElementById( 'mystring' ).value;
alert( "" + reverseString( str ) );
}

//reverse function
function reverseString( str ){
str = str.split(''); //split the entered string
str = str.reverse(); //reverse the order
str = str.join(''); //then join the reverse order array values
return str;
}

</script>
</head>
<body>
<input type="text" id="mystring" value="spider" />
<button id="btn" onClick="doReverse()" >Reverse</button>
</body>
</html>
7) Write a JavaScript program to capitalize the first letter of each word of a given string.

<html>
<head>
<script>
function titleCase() {
var str = document.getElementById("first").value;
var splitStr = str.toLowerCase().split(' ');
for (var i = 0; i < splitStr.length; i++)
{
splitStr[i] = splitStr[i].charAt(0).toUpperCase() + splitStr[i].substring(1);
}

return splitStr.join(' ');


}

</script>
</head>
<body>
<input id="first" type="text"><br>
<input type="button" value="Calculate" onclick="titleCase()">
</body>
</html>

You might also like