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.
Before starting, take the time to install Eslint Sublime Linter in Sublime Text:
-
Open Sublime Text
stt
-
In Sublime Text, open Package Control's menu:
# macOS cmd shift p # Ubuntu ctrl shift p
-
Type
install package
and selectPackage Control: Install Package
-
Type
SublimeLinter-eslint
and select it -
Restart Sublime Text
Click on the extension page and click on Install. You will get an alert asking you to open Visual Studio Code. The editor will open to the extension page.
Click on Install on WSL:Ubuntu. Click on Reload required.
It will highlight instantly your syntax errors / style offenses in Sublime Text / Visual Studio Code. Picking up the JavaScript syntax after Ruby may be tricky, so this should help you a lot.
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.)
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 🙏
Open the Rakefile
in Sublime Text. You will find two tasks:
eslint
, a JavaScript linter, the equivalent of Rubocop in the Ruby world.mocha
, a JavaScript testing framework, the equivalent of Rspec in the Ruby world.
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!).