JavaScript DHTML/Data Type/Number
Содержание
- 1 Add different type variables together
- 2 Calculate circle area/Peri/Calculate sphere volume
- 3 Calculate the average for integers
- 4 Check if it is a number and calculate the cubic power
- 5 Compare the value for integer numbers
- 6 Convert a Number to a Exponential format
- 7 Convert a number to a precision
- 8 Convert a number to string by append empty string to it
- 9 Convert Fahrenheit to Celsius
- 10 Convert from Celsius to Fahrenheit
- 11 Declare float point number, boolean value and null value
- 12 Fix a number
- 13 Hexadecimal Numbers
- 14 Integer math calculation
- 15 Read a number and compare its value
- 16 Using Binary Flags
Add different type variables together
<html>
<head>
<title></title>
</head>
<body>
<script type="text/javascript">
var num1 = 1;
var num2 = 2;
var num3 = 3;
var fourthvar = "4";
var name1 = "a";
var name2 = "b";
alert(num1 + num2);
alert(num3 + fourthvar);
alert(name1 + name2);
</script>
</body>
</html>
Calculate circle area/Peri/Calculate sphere volume
<html>
<head>
<title>A Simple Page</title>
<script language="JavaScript">
function calcArea(){
inpRadius = 3;
alert(Math.PI * ((inpRadius)*(inpRadius)));
}
function calcPeri()
{
inpRadius = 3;
alert(2 * Math.PI * (inpRadius));
}
function calcVol()
{
inpRadius = 3;
alert(Math.PI * ((inpRadius)*(inpRadius)*(inpRadius)) * (4/3));
}
</script>
</head>
<body>
<input type="button" value="Calculate circle area" onclick="calcArea();">
<input type="button" value="Calculate circle Peri" onclick="calcPeri();">
<input type="button" value="Calculate sphere volume" onclick="calcVol();">
</body>
</html>
Calculate the average for integers
<html>
<head>
<title>A Simple Page</title>
<script language="JavaScript">
function calcAvg(){
var inpNum1 = 1;
var inpNum2 = 2;
var inpNum3 = 3;
var inpNum4 = 4;
numAvg = doCalcAvg(inpNum1, inpNum2, inpNum3, inpNum4);
alert("The average is: " + numAvg);
}
function doCalcAvg(inpNum1, inpNum2, inpNum3, inpNum4)
{
var ans;
ans = (Number(inpNum1) + Number(inpNum2) + Number(inpNum3) + Number(inpNum4)) / 4;
return (ans);
}
</script>
</head>
<body onLoad="calcAvg();">
</body>
</html>
Check if it is a number and calculate the cubic power
<html>
<head>
<script type = "text/javascript">
function cubeme(aNum) {
if (aNum == 1) {
return 1;
} else {
return Math.pow(aNum,3);
}
}
</script>
</head>
<body>
<script type = "text/javascript">
var theNum = 2;
var finalNum = cubeme(theNum);
if (isNaN(finalNum)) {
alert("Not a number.");
} else {
alert(finalNum);
}
</script>
</body>
</html>
Compare the value for integer numbers
<HTML>
<BODY>
<SCRIPT language="JavaScript">
var num1 = 0;
var num2 = 0;
if(num1 == num2) {
window.alert("True");
}
else {
window.alert("False");
}
</SCRIPT>
</BODY>
</HTML>
Convert a Number to a Exponential format
<html>
<body>
<button onclick="var myNum = new Number(100); alert(myNum.toExponential(1));">
Number toExponential
</button>
</body>
</html>
Convert a number to a precision
<html>
<body>
<button onclick="var myNum = new Number(100); alert(myNum.toPrecision(1));">
Number toPrecision
</button>
</body>
</html>
Convert a number to string by append empty string to it
<html>
<head>
</head>
<body>
<script type="text/javascript">
function numToString(number) {
number += "";
return number;
}
var number = 100;
alert(typeof number);
number = numToString(number);
alert(typeof number);
</script>
</body>
</html>
Convert Fahrenheit to Celsius
<html>
<head>
<title>A Simple Page</title>
<script language="JavaScript">
function inputFah(){
var fah = 100;
ansCel = doCelCalc(fah);
alert(fah + " Degrees Fahrenheit is " + ansCel + " Degrees Celsius");
}
function doCelCalc(fah){
var ans = ((Number(fah) - 32) / 1.8);
return (ans);
}
</script>
</head>
<body>
<input type="button" value="Convert Fahrenheit to Celsius" onClick="inputFah();">
</body>
</html>
Convert from Celsius to Fahrenheit
<html>
<head>
<title>A Simple Page</title>
<script language="JavaScript">
function inputCels(){
var cels = 100;
ansFah = doFahCalc(cels);
alert(cels + " Degrees Celsius is " + ansFah + " Degrees Fahrenheit");
}
function doFahCalc(cels){
var ans = ((1.8 * Number(cels)) + 32);
return (ans);
}
</script>
</head>
<body>
<input type="button" value="Convert Celsius to Fahrenheit" onClick="inputCels();">
</body>
</html>
Declare float point number, boolean value and null value
<HTML>
<BODY>
<SCRIPT language="JavaScript">
<!--
var c=2.59;
var i=false;
var n=null;
document.write("\"This is fun!\"");
//-->
</SCRIPT>
</BODY>
</HTML>
Fix a number
<html>
<body>
<button onclick="var myNum = new Number(100.22); alert(myNum.toFixed(1));">
Number toFixed
</button>
</body>
</html>
Hexadecimal Numbers
<html>
<head>
<script type = "text/javascript">
var h = 0xe;
var i = 0x2;
var j = h * i;
document.write(j);
</script>
</head>
</html>
Integer math calculation
<HTML>
<BODY>
<SCRIPT language="JavaScript">
var intValue=200;
document.write(intValue+"<BR>");
intValue += 200;
document.write(intValue+"<BR>");
intValue = intValue-50;
document.write(intValue+"<BR>");
intValue = intValue*0;
document.write(intValue+"<BR>");
intValue = intValue + 500;
document.write(intValue+"<BR>");
intValue -= 80;
document.write(intValue+"<BR>");
</SCRIPT>
</BODY>
</HTML>
Read a number and compare its value
<html>
<head>
<title>An If Example</title>
</head>
<body>
<script type = "text/javascript" >
var inputNum = prompt("Please enter a number below 100:");
if (inputNum > 99) {
document.write("That number, " + inputNum + ", is not below 100.");
}
</script>
</body>
</html>
Using Binary Flags
<html>
<head>
<title>Using Binary Flags</title>
<script type="text/javascript">
var FIELD_A = 0x1; // 00001
var FIELD_B = 0x2; // 00010
var FIELD_C = 0x4; // 00100
var FIELD_D = 0x8; // 01000
var FIELD_E = 0x10; // 10000
var fieldsSet = FIELD_A | FIELD_C | FIELD_E;
if ((fieldsSet & FIELD_A) && (fieldsSet & FIELD_C)) {
document.write("Fields A and C are set");
}
</script>
</head>
</body>
</html>