Coffeescript-math-max

提供:Dev Guides
移動先:案内検索

CoffeeScript Math-max()

説明

  • max()*メソッドは一連の数値を受け入れ、指定された数値の最大値を返します。 引数を渡さずにこのメソッドを呼び出すと、-Infinityが返されます。

構文

以下に、JavaScriptの* max()*メソッドの構文を示します。 CoffeeScriptコードで同じメソッドを使用できます。

Math.max ( x )

次の例は、CoffeeScriptでの* max()メソッドの使用方法を示しています。 このコードを *math_max.coffee という名前のファイルに保存します。

value = Math.max 10, 20, -1, 100
console.log "The max value among (10, 20, -1, 100) is : " + value

value = Math.max -1, -3, -40
console.log "The max value among (-1, -3, -40) is : " + value

value = Math.max 0, -1
console.log "The max value among (0, -1) is : " + value
  • コマンドプロンプト*を開き、以下に示すように.coffeeファイルをコンパイルします。
c:\> coffee -c math_max.coffee

コンパイル時に、次のJavaScriptが提供されます。

//Generated by CoffeeScript 1.10.0
(function() {
  var value;

  value = Math.max(10, 20, -1, 100);

  console.log("The max value among (10, 20, -1, 100) is : " + value);

  value = Math.max(-1, -3, -40);

  console.log("The max value among (-1, -3, -40) is : " + value);

  value = Math.max(0, -1);

  console.log("The max value among (0, -1) is : " + value);

}).call(this);

次に、*コマンドプロンプト*を再度開き、以下に示すようにCoffeeScriptファイルを実行します。

c:\> coffee math_max.coffee

CoffeeScriptファイルを実行すると、次の出力が生成されます。

The max value among (10, 20, -1, 100) is : 100
The max value among (-1, -3, -40) is : -1
The max value among (0, -1) is : 0