JS-Basic Code PDF
JS-Basic Code PDF
JS-Basic Code PDF
<html>
<head>
<script>
function check_leapyear(){
//define variables
var year;
<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>
<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);
}
</script>
</head>
<body>
<input id="first" type="text"><br>
<input type="button" value="Calculate" onclick="titleCase()">
</body>
</html>