Finding the maximum and minimum number : min « Math « JavaScript Tutorial

Home
JavaScript Tutorial
1.Language Basics
2.Operators
3.Statement
4.Development
5.Number Data Type
6.String
7.Function
8.Global
9.Math
10.Form
11.Array
12.Date
13.Dialogs
14.Document
15.Event
16.Location
17.Navigator
18.Screen
19.Window
20.History
21.HTML Tags
22.Style
23.DOM Node
24.Drag Drop
25.Object Oriented
26.Regular Expressions
27.XML
28.GUI Components
29.Dojo toolkit
30.jQuery
31.Animation
32.MS JScript
JavaScript Tutorial » Math » min 
9.18.3.Finding the maximum and minimum number
<html>
<head>
<title>Finding the maximum and minimum number</title>
<script type="text/javascript" language="javascript">
<!-- //
function SetFocus(){
    document.SimpleForm.FirstInput.focus();
}

function FindMaxAndMin(){
    var num1 = document.SimpleForm.FirstInput.value;
    var num2 = document.SimpleForm.SecondInput.value;
    var num3 = document.SimpleForm.ThirdInput.value;

    if (isNaN(num1|| isNaN(num2|| isNaN(num3)){
        alert("You made an invalid entry. Please start again.");
        document.SimpleForm.reset();
        SetFocus();
    else {
        var MaxNum = Math.max(num1,num2,num3);
        var MinNum = Math.min(num1,num2,num3);
        var alertString = "You entered " + num1 + ", " + num2 + " and " +num3;
        alertString += "\nThe largest number is " + MaxNum;
        alertString += "\nThe smallest number is " + MinNum;
        document.write(alertString);
        document.SimpleForm.reset();
        SetFocus();
    }
}

// -->
</script>
</head>
<body onload="SetFocus()">
<form name="SimpleForm">
<table>
<tr>
 <td width="25%" align="right">Enter first number:</td>
 <td><input name="FirstInput" type="text"></td>
</tr>
<tr>
 <td width="25%" align="right">Enter second number:</td>
 <td><input name="SecondInput" type="text"></td>
</tr>
<tr>
 <td width="25%" align="right">Enter third number:</td>
 <td><input name="ThirdInput" type="text"></td>
</tr>
<tr>
 <td width="25%" align="right">&nbsp;</td>
 <td><button type="Button" onclick="FindMaxAndMin()">
Click to calculate</button></td>
</tr>
</table>
</form>
</body>
</html>
9.18.min
9.18.1.Math.min()
9.18.2.Find the minimum number by using the Math.min
9.18.3.Finding the maximum and minimum number
9.18.4.Use Math.min the get the minimum value among three
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.