Skip to content

Instantly share code, notes, and snippets.

@BaReinhard
Last active April 2, 2018 14:32
Show Gist options
  • Save BaReinhard/40e2d38ddd2bb5230b9413df71317db3 to your computer and use it in GitHub Desktop.
Save BaReinhard/40e2d38ddd2bb5230b9413df71317db3 to your computer and use it in GitHub Desktop.

Setting up NodeJS

  1. Install Node, Download
  2. Run through the installer
  3. JavaScript runtime is setup on your local machine.

Writing your first node application

  1. Open up your CLI of choice (terminal works just fine, press command+space type in terminal and press enter)
  2. In terminal create a development directory mkdir ~/dev
  3. Enter the dev directory cd ~/dev
  4. Make your first project directory mkdir helloWorld
  5. Enter directory cd helloWorld
  6. Lets start by making our first program touch helloWorld.js
  7. Open helloWorld.js in your favorite IDE/Text Editor
  8. Lets add some code. Inside your helloWorld.js place the following inside (be sure to save it):
/**
 * Hello World Function, prints Hello World to the console
 * @returns {undefined}
*/
function helloWorld(){
  console.log('Hello World');
}
// Run the function
helloWorld();
  1. Return back to your terminal instance and run the script node helloWorld.js
  2. You should now see Hello World printed to your console.
  3. Now try making your own function inside a new file, lets call it IP.js
  4. Inside this function lets try to solve the issue of an incorrectly formatted IP address. Place the following code inside the file:
/* 
 * Here we have the following problem, all our IP addresses inside a service are formatted in a space delimited format
 * The incorrectly formatted IP address is stored inside the variable badIP below:
*/
let badIP = '10 65 22 33';

/**
 * This functions takes a poorly formatted IP address, which is space delimited and returns a string with the correct format of the ip address (XX.XX.XX.XX)
 * @param {String} ip 
 * @returns {String} a properly formatted string (XX.XX.XX.XX)
*/
function fixIP(ip){
  let goodIP;
    // Your Code Goes here
    // Hint: You will need the following functions to solve this issue (others will work, but these should make it much easier)
    // Look at what the functions do and decide how to use them to solve the issue
    // String.prototype.split : Documentation here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split
    // Array.prototype.map : Documentation here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
    // Array.prototype.join : Documentation here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/join
    // Be sure to look at what each of these functions is called on and what type they return.
    // You will notice that these functions should be called in this order, you may need to declare more variables as needed.
    // Feel free to google around as needed (this is how development works)
    
  return goodIP;
}

// This should output '10.65.22.33' to your console.
console.log(fixIP(badIP));

// Bonus Points:
// Create a function that properly formats a MAC Address to a colon delimited string
// i.e. AABBCCDDEEFF to AA:BB:CC:DD:EE:FF

/**
 * Properly displays a MAC address from AABBCCDDEEFF to AA:BB:CC:DD:EE:FF
 * @params {String} mac
 * @returns {String}
*/
function displayMAC(mac){
  let macDisplay;
  
  return macDisplay;
}
console.log(displayMAC("AABBCCDDEEFF"));

  1. Lastly, run the new script and see that it matches (of course after you fill in the function to make the output.) node IP.js
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment