Home > Article > Web Front-end > How to find the maximum value of three numbers in javascript
Javascript method to find the maximum value of three numbers: first create an HTML sample file; then enter three values; finally pass "Math.max(inps[0].value,inps[1].value ,inps[2].value)" just find the maximum value of the three numbers.
The operating environment of this article: windows7 system, javascript version 1.8.5, DELL G3 computer
How to find the maximum of three numbers with javascript value?
How to find the maximum value by inputting three numbers in javascript
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <input type="text"><br> <input type="text"><br> <input type="text"><br> <input type="button" value="确认"> </body> <script> var inps = document.querySelectorAll("input"); inps[3].onclick = function(){ alert(Math.max(inps[0].value,inps[1].value,inps[2].value)); //得到的.value是string型的,需要转换为number var a = Number(inps[0].value); var b = Number(inps[1].value); var c = Number(inps[2].value); if(a>b){ if(a>c){ alert(a); }else{ alert(c); } }else{ if(b>c){ alert(b); }else{ alert(c); } } // 枚举法 列举所有可能 if(a>=b && a>=c){ alert(a); } if(b>=a && b>=c){ alert(b); } if(c>=a && c>=b){ alert(c); } //定义中间变量,定义最大值 var max = 0; if(a >= max){ max = a; } if(b >= max){ max = b; } if(c >= max){ max = c; } alert(max); } </script> </html>
Recommended study: "javascript Advanced Tutorial"
The above is the detailed content of How to find the maximum value of three numbers in javascript. For more information, please follow other related articles on the PHP Chinese website!