turingschool-examples / intermission-assignments

33 stars 218 forks source link

Reading Option 1: JavaScript Allongé #5

Closed stevekinney closed 9 years ago

stevekinney commented 9 years ago

Read JavaScript Allongé by Reginald Braithwaite

Discuss questions and thoughts here. Feel free to talk about the book as you're reading it. I'll also answer any questions you have.

Some things to think about:

VikiAnn commented 9 years ago

I'm really, really not good at reading tech books, apparently.

VikiAnn commented 9 years ago

Also, are the examples in this supposed to actually run on their own in Node? I tried typing one out and the console complained at me immediately about an unexpected token (

stevekinney commented 9 years ago

Code sample?

On Fri, Feb 6, 2015 at 9:45 PM, Viki Harrod notifications@github.com wrote:

Also, are the examples in this supposed to actually run on their own in Node? I tried typing one out and the console complained at me immediately about an unexpected token (

Reply to this email directly or view it on GitHub: https://github.com/turingschool/intermission-assignments/issues/5#issuecomment-73349767

VikiAnn commented 9 years ago

function (d) { var calc = function (diameter) { var Pi = 3.14159265;

return diameter * Pi };

return "The circumference is " + calc(d) }

VikiAnn commented 9 years ago

Thing yelled at me before I could get past line 1 though...

stevekinney commented 9 years ago

Are you typing this in the REPL or running a file?

On Fri, Feb 6, 2015 at 9:55 PM, Viki Harrod notifications@github.com wrote:

Thing yelled at me before I could get past line 1 though...

Reply to this email directly or view it on GitHub: https://github.com/turingschool/intermission-assignments/issues/5#issuecomment-73350119

VikiAnn commented 9 years ago

I tried both.

VikiAnn commented 9 years ago

Also, ignore that that sample is from maaaaybe 25% through the book >_<

stevekinney commented 9 years ago

It looks like the issue is just that code is out of context of a runnable script. The JavaScript engine doesn't like that there is a lonely anonymous function just hanging out in the world. So, it sounds the alarms.

If you were to do something like this, it would work:

var calculateDiameter = function (d) {
  var calc = function (diameter) {
    var Pi = 3.14159265;

    return diameter * Pi
  };

  return "The circumference is " + calc(d)
}

The interpreter then sees you want to store it in a variable and is like—oh yea, totally legit.

Alternatively, you can use it as an IIFE:

(function (d) {
  var calc = function (diameter) {
    var Pi = 3.14159265;

    return diameter * Pi
  };

  return "The circumference is " + calc(d)
})(20)
VikiAnn commented 9 years ago

Huh. Weird. I guess that makes sense. I guess I was just figuring if it's in the book, it's runnable as-is.

Semi off-topic, how do you read a tech book? Do you try to just... read it? Or do you try typing out the examples even though the result's given in the book? Or something else? And does your strategy vary depending on whether it's a paper book or an e-book? I'm just finding it very difficult to stay focused on it.

stevekinney commented 9 years ago

I tend to—in my first pass—just read through the book and get a sense of the big picture. I liberally highlight and bookmark pages that I don't fully understand and return to them after I've gotten to the end of the book.