Skip to content

Session Notes 2016.05.10

Austin Guest edited this page May 11, 2016 · 1 revision

TLC 5.10.2015 Session: Command Line, Node Programs, and Arrays!

Command Line

Useful commands:

pwd
cd
cd ../
cp
mv
mkdir
ls
ls -al

REPL

  • Stands for Read-Evaluate-Print Loop
  • There's one in your browser! (Developer Tools / Console)
  • Or use node from the command line:
    • Install the package manager homebrew:

      /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
    • Install node:

      brew isntall node
    • Run node:

      node
      > console.log("hello world!")
      

Arrays

Useful Resources

Covered:

  • printing stuff to the REPL:

    console.log("hello world!");
  • Building an array:

    var fruits = ["Apple", "Orange", "Banana"]
  • finding head and tail with slice:

    fruits[0] // => "Apple"
    fruits.slice(1) // => ["Orange", "Banana"]
    // shortcut for:
    fruits.slice(1, fruits.length) // who wants to type that all the time!?
  • adding stuff to an array:

    fruits.concat("Kiwi", "Strawberry") // => ["Apple", "Orange", "Banana", "Kiwi", "Strawberry"]
  • Array.pop(), and Array.push() work, but are evil!

    • Use Array[-1] and Array.concat() instead!

    • Pop quiz: why are they evil?

    • Hint: what happens if you try to substitute the value of x for every occurrence of x below:

      var fruit = ["Apple", "Orange", "Banana"];
      var x = fruit.push("Kiwi");
      var y = x.push("Strawberry");
      y // => ["Apple", "Orange", "Banana", "Kiwi", "Strawberry"]

      Ie: if x is equivalent to fruit.push("Kiwi""), then I should be able to substitute fruit.push("Wiki") everywhere I see x and get the same result for y, but do I? Try it and see:

      var fruit = ["Apple", "Orange", "Banana"];
      var x = fruit.push("Kiwi");
      var y = fruit.push("Kiwi").push("Strawberry");
      y // => ???

Running programs with node

Given the file fruits.js stored at ~/myProjects/fruit.js:

var fruit = ["Apple", "Orange", "Banana"];
console.log(fruit);

Running this from the command line should print the contents of the fruits array to the REPL:

$ cd ~/myProjects
$ node fruits.js

Testing

It's sort of annoying to run everything from the CLI and console-log every time we want to check something. Wouldn't it be nice to have tests like we did in TLC.js? We didn't get there yet... but we will!

Sneak preview: mocha is great!

Example Problems for next couple of weeks:

  • Given as input the array [1,2,3,4,5,6,7,8]:

    1. Write a function sum that adds up every number in the array
    2. Write a function countEvens that counts the number of even numbers in the array
  • Given as input the array:

    [
      "How", "much", "wood", "would", "a",
      "wood", "chuck", "chuck", "if", "a",
      "wood", "chuck", "could", "chuck", "wood", "?"
    ]
    1. Write a function countChucks that counts the number of times the word chuck appears in the array
    2. Write a function countAll that returns an object whose keys are each word in the array, and whose values are the number of times each word appears in the array
  • HARD: Given as input the first section of Gravity's Rainbow: