Write concise source expressions and understand their JavaScript compilation.

Before starting: Basic JavaScript values, functions and Node.js.

Practice project: Extend the examples into a small CoffeeScript study demo. Explain the setup, one normal case and one failure or boundary case.

Start here: concepts and guided practice

  1. CoffeeScript: a small function

    CoffeeScript compiles into JavaScript. The thin arrow defines a function; its final expression supplies the result. Read the generated JavaScript when debugging.

    Node.js and CoffeeScript 2 installed in a practice project; save as study.coffee and use the installed coffee command.

    double = (value) -> value * 2
    console.log double(6)
    Check the output
    12

    Change the input to -2 and predict -4.

  2. CoffeeScript: transform a collection

    A comprehension visits collection entries and builds a result array. This example keeps the source array and creates a new array of doubled scores.

    Node.js and CoffeeScript 2 installed in a practice project; save as study.coffee and use the installed coffee command.

    scores = [3, 5, 8]
    doubled = (score * 2 for score in scores)
    console.log doubled.join(", ")
    Check the output
    6, 10, 16

    Use an empty array and explain the empty printed line.

  3. CoffeeScript: make a condition return a value

    In CoffeeScript, an if expression can supply a value. Use an explicit boundary and keep the two result labels easy to understand.

    Node.js and CoffeeScript 2 installed in a practice project; save as study.coffee and use the installed coffee command.

    score = 60
    label = if score >= 60 then "Pass" else "Practice"
    console.log label
    Check the output
    Pass

    Check scores 59 and 60.

Open the CoffeeScript learning path on DevStudyHub · Play coding games on this blog

logoblog

No comments:

Post a Comment

Please do not enter any spam link in the comment box.