供參考。。
<!DOCTYPE? HTML>
<html >
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>函數</title>
<script type="text/javascript">
//定義函數
function add2(x,y){
? ?if(x>y){
? ? ? ?return x;
? ? ?}
? ?else if(x<y){
? ? ? ?return y;
? ? ? ?
? ?}
? ?else if(x==y){
? ? ? ?return x;
? ? ? ?
? ?}
}
//函數體,判斷兩個整數比較的三種情況
?
//調用函數,實現下面兩組數中,返回較大值。
? document.write(" 5 和 4 的較大值是:"+add2(5,4)+"<br>");
? document.write(" 6 和 3 的較大值是:"+add2(6,3)+"<br>");?
? document.write(" 8 和 8 的較大值是:"+add2(8,8) );
</script>
</head>
<body>
</body>
</html>
2025-02-01
函數部分使用以下代碼應該要簡潔些。
function add2(x,y){
? ? if (x>=y){
? ? return x;
? ?}else{
? ? ? return y;
? ? ? }
}