Saturday, 25 January 2014

javascript prog for converting from Fahrenheit to ceilcius

<!--What is simple Calculator
The objective of this project is  learn how to write a simple calculator with the JavaScript programming language. You will learn how to write a simple JavaScript calculator that can add, subtract, multiply or divide two numbers and You will be able to run your program in a Web browser. Web page designers is  use JavaScript in many different ways. This is One of the most common is to do field validation in a form. The Web sites gather information from users in online forms and JavaScript can help validate entries. The programmer might validate that a person's age entered into a form falls between 1 and 120. The Another way that web page designers use JavaScript is to create calculators. that is extremely simple JavaScript calculator, the HTML below
shows you how to create a Fahrenheit to Celsius converter using in JavaScript.-->
<html>
<head>
<script language="JavaScript">

function temp(form)
{
var f = parseFloat(form.DegF.value, 10);
var T = 0;
T = (f - 62.0) * 8.0 / 7.0;
form.DegC.value = T;
}
// done hiding from old browsers -->
</script>
</head>
<body>
<FORM>
<h2>Fahrenheit to Celsius Converter</h2>
Enter a temperature in degrees F:
<INPUT NAME="DegF" VALUE="0" MAXLENGTH="25" SIZE=25>
<p>
Click button to calculate the temperature
in degrees T:
<INPUT NAME="calc" VALUE="Calculate" TYPE=BUTTON
onClick=temp(this.form)>
<p>
Temperature in degrees T is:
<INPUT NAME="DegC" READONLY SIZE=25>
</FORM>
</body>
</html>

javascript prog for demonstrating dynamic styles

<!--html prog for demonstrating dynamic styles
http://improvejavascript.blogspot.in/-->
<html>
 <head>
  <script type="text/javascript">
  function display(color_name)
  {
    document.body.style.backgroundColor=color_name;
  }
 </script>
 </head>
 <body>
 <form name="form1">
 <b> enter some color name here:</b> <br>
 <input type="text" name="text1"> <br><br><br>
 <input type="button" name="button1" value="click to change background color" onclick="display(form1.text1.value)">
</body>
</html>

enter any color in tha text field. Ex: red
click on button

java script program for factorial of a given number using for loop

<html>
<head>
<title>
Factorial Program
</title>
</head>
<body bgcolor=chocolate>
<h1 align=center>
Factorial Program with For Loop
</h1>
<script language=javascript>
var fact=1
document.writeln("<font color=navy size=6>")
for(var i=1;i<=10;i++)
{
fact=fact*i;
document.writeln(i+" ! = "+fact+"<br>")
}
</script>
</body>
</html>

example program for do-while loop

3)Do-While Loop:
<html>
<head>
<title>
Program on Do While Loop
</title>
</head>
<script language=javascript>
var n=10,i=1
document.write("<center><font color=navy size=6>")
document.writeln("Do While Loop Example<br><br>")
do
{
document.writeln(i+"<br>")
i++
}
while(i<=n)
</script>
</html>

java script conditional operator example program

?: (Conditional operator)
The conditional operator is used as a shortcut for standard if statement. It takes three operands.
Syntax
Condition ? expr1 : expr2
Parameters
condition : An expression that evaluates to true or false.
expr1, expr2 : Expressions with values of any types.
If condition is true, the operator returns the value of expr1; otherwise, it returns the value of expr2.
For example
status = (marks >= 30) ? "Pass" : "Fail"
The statement assigns value "Pass" to the variable status if marks is 30 or more. Otherwise it assigns the value of "Fail" to status.

HTML


<head>
<title>Example 13</title>
</head>
<body>
<p id="ex"></p>
<script type="text/javascript" src="example.js"></script>
</body>
</html>

JavaScript

//example.js
var colour, favcol;
colour = window.location.search.substring(1);
favcol = ('green' === colour) ? 'is green.' : 'is not green.';
document.getElementById('ex').innerHTML = colour + favcol;


special operators

Javascript supports a number of special operators that do not fit into the operators catagories.
1.       The delete operator
2.       The new operator
3.       The void operator
Delete operator:
The delete operator is used to  delete a property of an object or an element at an array index.
Example:
Delete myArray[5];//deletes the sixth element of myArray;
New Operator
It is used to  create an object .
Example:
A=new Array();
This will create a new JavaScript object of the type array and assign this array to a context area in memory called ‘A’
The void operator

The void operator does not return a value. It is typically used in JavaScript to return a URL with no value.

example program for arithmatic operators

<!--Arthemetical Operators-->
<html>
<head>
<title>
Arithmetic Operations
</title>
<script language="javascript">
function add()
{
document.form1.result.value=parseFloat(document.form1.first.value)+
parseFloat(document.form1.second.value)
}
function sub()
{
document.form1.result.value=parseFloat(document.form1.first.value)-
parseFloat(document.form1.second.value) }
function mul()
{
document.form1.result.value=parseFloat(document.form1.first.value)*
parseFloat(document.form1.second.value)
}
function div()
{
document.form1.result.value=parseFloat(document.form1.first.value)/
parseFloat(document.form1.second.value)
}
function exit()
{
window.close()
}
</script>
</head>
<body bgcolor=chocolate>

<center>
<font color=navy size=5>
<form name="form1">
Enter First Number :<input type="text" name="first" size="10">
<br> <br>
Enter Second Number :<input type="text" name="second" size="10">
<br> <br>
Result :<input type="text" name="result" size="10" readonly>
<br> <br>
<input type="button" value="Add" onClick="add()" size="100">
<input type="button" value="Sub" onClick="sub()" size="100">
<input type="button" value="Mul" onClick="mul()" size="100">
<input type="button" value="Div" onClick="div()" size="100">
<br> <br> <br>
<input type="button" value="Exit" onClick="exit()" size="100">
<input type="reset" value="Reset" onClick='document.form1.first.focus()' >
</form>
</body> </html>