-
Notifications
You must be signed in to change notification settings - Fork 0
Session Notes 2016.05.10
pwd
cd
cd ../
cp
mv
mkdir
ls
ls -al
- 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!")
-
-
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()
, andArray.push()
work, but are evil!-
Use
Array[-1]
andArray.concat()
instead! -
Pop quiz: why are they evil?
-
Hint: what happens if you try to substitute the value of
x
for every occurrence ofx
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 tofruit.push("Kiwi"")
, then I should be able to substitutefruit.push("Wiki")
everywhere I seex
and get the same result fory
, 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 // => ???
-
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
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!
-
Given as input the array
[1,2,3,4,5,6,7,8]
:- Write a function
sum
that adds up every number in the array - Write a function
countEvens
that counts the number of even numbers in the array
- Write a function
-
Given as input the array:
[ "How", "much", "wood", "would", "a", "wood", "chuck", "chuck", "if", "a", "wood", "chuck", "could", "chuck", "wood", "?" ]
- Write a function
countChucks
that counts the number of times the wordchuck
appears in the array - 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
- Write a function
-
HARD: Given as input the first section of Gravity's Rainbow:
- Generate a word frequency concordance of the section
- Hints: you will likely find String#split and Object#assign