Background & Objectives

Today is your first day of JavaScript. The goal of this first day is for you to realize that it's a programming language, like Ruby. It has variables, functions, conditions, loops, etc.

Today we won't need the browser. Instead, we'll use Node.js to execute some JavaScript directly in our terminal.

Make sure that the following command returns a version greater than 10:

node -v

If not, Node might not be installed on your system. Please have a look at the Setup section in the lecture slides.

Install Eslint Sublime Linter

Before starting, take the time to install Eslint Sublime Linter in Sublime Text:

  1. Open Sublime Text
stt
  1. In Sublime Text, open Package Control's menu:
# macOS
cmd shift p

# Ubuntu
ctrl shift p
  1. Type install package and select Package Control: Install Package
  2. Type SublimeLinter-eslint and select it
  3. Restart Sublime Text

It will highlight instantly your syntax errors / style offenses in Sublime Text. Picking up the JavaScript syntax after Ruby may be tricky, so this should help you a lot.

Specs

Let's start with a very simple algorithm. Open the lib/even_or_odd.js file. Implement the evenOrOdd function which takes one parameter number (of type Number) and returns a String:

  • "even" if the number is even (0, 2, 4, etc.)
  • "odd" if the number is odd (1, 3, 5, etc.)

⚠️ Warning: In JavaScript, you need to explicitly write the return keyword, otherwise the function will return undefined! The only exception to this rule is when you use a one-liner arrow function with implicit return.

Hint: remember the Ruby modulo operator? It also exists in JavaScript and might be useful!

Run rake to check your style and the correctness of your function!

Once the first exercise is all green (style + tests), please commit and push 🙏

About the testing setup

Open the Rakefile in Sublime Text. You will find two tasks:

These two commands are run from the node_modules folder. It was created by running yarn install in the 04-FrontEnd folder (cd ../..), reading the package.json file (open it!).