Giter Club home page Giter Club logo

node-workshop's Introduction

NodeJS basic workshop

Base instructions

  • Fork this repository and clone it in a new Cloud9 workspace.
  • As you open the project in Cloud9, open notice there is a file called .gitignore. Open this file to check its contents.
  • Notice that the .gitignore file contains one line that says "node_modules". What this is doing is telling Git that it should ignore the contents of the node_modules directory. Since the code in there comes from the outside world, we don't need to save it in our repo.
  • All the code of this workshop will go in master. After your first commit, push and create a pull-request. Then, create a pull-request after each exercise.

First program

  • Create a file called hello-world.js . In it, write a simple node program that outputs "Hello World!" to the console.
  • Add an instruction to your program that will output "Hello World Again!!" 10 seconds after the program was run.
  • Save, commit and push.

A wild interval has appeared!

If you never tried setInterval, give it a try first. It works the same way as setTimeout, takes a callback function and a time in milliseconds. But instead of calling your callback once, it calls it once every x milliseconds. In this exercise, you'll have to mimic what setInterval is doing but only with setTimeout!

Your exercise: Write a piece of code using setTimeout that prints "Hello World!" every 10 seconds forever.

One idea that may come to you is to do something like this:

setTimeout(function() {
  console.log('Hello World!');
  setTimeout(function() {
    console.log('Hello World!');
    // when does this stop??
  }, 1000);
}, 1000);

This won't work, because you'd have to write an infinite amount of code.

Another idea you might have is to do the following:

while (true) {
  setTimeout(function() {
    console.log('Hello World!');
  }, 1000);
}

Up to you to figure out why this does not work ;) Ask us if you can't figure it out. Then, find a real solution.

Two BIG hints: the solution involves recursion. You will have to write a function.

Getting some data

  • Create a file called iss.js. In it, write a simple node program that will output the latitude and longitude of the International Space Station.
  • Practice your google-fu by searching for "iss api" and figuring out the correct URL to use. Hint: there are many options and they are all good :)
  • Notice that the values provided by the API are very precise. Round off the values to two decimal digits for a nicer display. Hint: toFixed
  • Save/commit/push

Augmenting our application

  • Create a file called iss-augmented.js. It will be similar to iss.js but more difficult!
  • Augment your ISS application to tell the user how "far" the ISS is from them. Here is how you will do it:
  • Using the prompt module, ask the user to enter their location (e.g. "montreal")
  • Using Google's Geolocation API, find out the latitude and longitude of the provided location. Here is how:
  • When you are comfortable with finding the location based on an input address, you can then calculate the distance between the ISS and the user:
    • Look at this URL: http://www.movable-type.co.uk/scripts/latlong.html
    • It specifies a formula for calculating the distance. Scroll the page to the JavaScript portion, and create a function that uses the provided code. You don't need to understand what is going on in there, it is very mathy!
    • NOTE: In order for this code to work, you'll need to add the following code at the beginning of your program:
  Number.prototype.toRadians = function() {
    return this * Math.PI / 180;
  }
  • Finally, display a message to the user telling them what their "distance" to the ISS is.
  • Save/commit/push

Number guessing game!

  • Create a file called number-guessing-game.js.
  • In this file, re-write your number guessing game (from the basic javascript workshop) for the command line!
  • Instead of using prompt and alert, you will have to use capabilities from NodeJS and any external module. HINT: there is an npm library called prompt that can help you with that :)
  • Save/commit/push

Challenge: Hangman!

  • Create a file called hangman.js.
  • In this file, write a program that will let the user play hangman. The program should work as follows:
    • Choose a random word from a list of words.
    • In a loop, do the following:
      • Ask the user to guess a letter
      • If the user guessed a wrong letter, then add one step to the hangman "drawing"
      • Display the current completion of the word next to a hangman ASCII "drawing". You can get some inspiration from either here or here
      • Keep looping until either the word is found or the hangman is hanged!
    • Display a message to the user letting them know what happened
  • Save/commit/push

node-workshop's People

Contributors

klingat avatar ziad-saab avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.