Functions In CoffeeScript

I'm trying to convert a function from Javascript to CoffeeScript. This is the code:

function convert(num1, num2, num3) {
    return num1 + num2 * num3;
}

But how I can do that in CoffeeScript?


I'm trying to run the function from an HTML source like this:

<script type="text/javascript" src="../coffee/convert.js"></script>

<script type="text/javascript">
    convert(6, 3, 10);
</script>

But it won't work and I get an error saying: ReferenceError: Can't find variable: convert

How to correct this?


You need to export the convert function to the global scope.
See How can Coffescript access functions from other assets?

window.convert = (num1, num2, num3) ->
  num1 + num2 * num3

@lawnsea answer is great.

I just want to add some thoughts.

Instead of polluting the global namespace, I prefer to add just one variable to the window object.

window.App = {}

Then, you can have access to App globally and add all your stuff there. the function convert can now be expressed this way:

App.convert = convert = (a, b, c) -> a + b * c

Then, to call the function within the local scope

convert 1,2,3

And now globally

App.convert 1,2,3