Title photo
frugal technology, simple living and guerrilla large-appliance repair
Sun, 18 Jun 2017

Eloquent Javascript, Chapter 3 (Functions) -- what the hell?

I read Chapter 3 of Eloquent Javascript some time ago, and it's a difficult one. It introduces the concept of functions. Quickly introduced are: Parameters and Scopes, Nested Scopes, Closure and Recursion.

It is too much, too fast with too few examples. I was able to do the first exercise, Minimum, but got lost in the second, Recursion.

Here is my solution for Minimum:

#!/usr/bin/env node
/* Eloquent Javascript, Chapter 3, Page 56, Exercises 
Create a function to find the minimum of two arguments

By Steven Rosenberg, 6/17/2017 */

function smallest(first_number, second_number) {
    if (first_number < second_number)
        return first_number;
    else if (second_number < first_number)
        return second_number;
    else
        console.log("They are equal")
}

// Output will be the smallest of these two numbers
console.log(smallest(100, 2));

Expressing this as a function doesn't really do much. The program could just as easily have been written in a straight "procedural" format. But it's a function, and it works.

The second problem on recursion stumped me. I'm pretty sure I can figure it out, but I need more time to think (and look up more on recursion).

Tue, 13 Jun 2017

Sitepoint: How I Designed & Built a Fullstack JavaScript Trello Clone

Sitepoint: How I Designed & Built a Fullstack JavaScript Trello Clone by Moustapha Diouf.

This article and accompanying repo show how Moustapha Diouf built this React app with Express and Mongo.

Tue, 30 May 2017

To run Node in Debian and Ubuntu, install nodejs and nodejs-legacy

Installing node.js in Fedora is no problem. You just run sudo dnf install node, and you're off to the JavaScript-in-the-console races. But it's slightly more complicated in Debian and Ubuntu.

Since there's an old amateur radio package called node for communicating on packet radio nodes, Debian and Ubuntu use the package name (and shell command) nodejs. So you would run nodejs when you would normally run node.

But you don't have to do this. And you don't have to resort to any Linux/Unix tomfoolery either.

Both Debian and Ubuntu have a package called nodejs-legacy that makes the symlink for you. Then you can run node by typing node in the console.

Since it looks like there is no node for amateur radio in Debian Sid or Experimental, I'm thinking that the node-vs-node.js problem will go away at some point in the near future -- when Debian declares its next release stable, and in turn when Ubuntu bases its future releases on versions of Debian that have "re-resolved" the issue. (Since I'm running Ubuntu 16.04 in the Windows Subsystem for Linux, this hasn't happened yet.)

Until then:

$ sudo apt install nodejs nodejs-legacy
Tue, 09 May 2017

Javascript digital clock

Since this blog's time is displayed in UTC, I wanted to put a digital clock on the site that told the current Universal Time so a casual reader (like me) could have some idea of how long ago (or what time of "day") a given post was published.

I started with some JavaScript code from Cory/uniqname on CodePen and simplified it greatly and because I wanted more output rather than less, counterintuitively as well.

It uses the Date() object and the toUTCString() method to create the text for display of the time/day/date/year and setInterval to "update" the text every second:

function clock() {
  var local_time = new Date(),
  utc_time = local_time.toUTCString();

document.querySelectorAll('.clock')[0].innerHTML = utc_time;

}
  setInterval(clock, 1000);

I stashed this bit of code in a file called clock.js, and called it into the site with script tags and a div:

<script type="text/javascript" src="/path/to/clock.js">
</script>
<div class="clock"></div>

(Note that /path/to/clock.js means the actual path on the server to where you happen to have created the JavaScript file.)

The toUTCString() method outputs the time in 24-hour format. If I want it it in 12-hour format (with AM/PM), the script would have to get a lot more complicated. I'm not saying I won't do that, but for now the easy wins over the perfect.

Wed, 12 Apr 2017

The Node in the Windows Subsystem for Linux is so old, I installed Node for Windows

I want to run Node, so I figured that I would install the package from the Ubuntu LTS in the Windows Subsystem for Linux and just use it from the Ubuntu commmand line in Windows 10.

But I soon learned that the nodejs in the WSL is v0.10.25. That is hella old. Early 2014 old. No ES6 old.

I don't want to mess with the WSL environment too much, and I have no idea what kinds of binaries from outside the WSL will even work (if any of them will). But I wanted a newer -- a much newer -- Node.

So I installed the Windows version of Node -- the Current version -- which is v7.9.0.

That is a lot newer.

I'm not building major web applications with Node. I'm mostly using it to learn Javascript and even do some traditional scripting that I might otherwise do in Ruby or Bash.

Now I'll be doing that in the Windows command line and not the Windows Subsystem for Linux (until I can no longer hold out without a full, "modern" Linux distribution like Fedora on this laptop).

Update: Node v.0.10.25 in the Ubuntu Trusty LTS is super, super old. For comparison's sake:

Ubuntu Trusty: Node v0.10.25
Ubuntu Xenial (newer LTS): Node v4.2.6
Ubuntu Zesty: Node v.4.7.2
Debian Jessie: Node v0.10.29
Debian Stretch: Node v4.7.2
Debian Sid: Node v4.8.2
Fedora 25 and 26: Node v6.10.2

Even Debian Jessie has a slightly newer nodejs than the Ubuntu LTS in the Windows 10 WSL. There is a way to update the Ubuntu in the WSL from 14.04 to 16.04. Might be worth a look for me.

Update: After a Windows 10 upgrade hosed the laptop, I restored Windows and reinstalled the Windows Subsystem for Linux after that. My user files were preserved, but I lost all of the files I created in the WSL.

Moral of this story: Back up your Linux files. You can back them up in your Windows user files. I would recommend making a habit of using the WSL/Ubuntu command line not in the WSL's traditional /home directory but in your Windows user area. However, things that are complicated (and particularly which involve setting Unix-style permissions) cannot be done successfully on the Windows side. Among these "complicated" things are the use of Unison to sync two filesystems on different computers. The Ubuntu/WSL version of Unison works great in the WSL but throws errors aplenty when used on the Windows side. (One solution is to use the Windows version of Unison, but I'm a whole lot of hacking away from getting ssh working on the Windows command line in a way that Windows Unison finds acceptable; It's not as easy as subbing PuTTY's plink command-line tool.)

My new WSL turned out to be 16.04, not 14.04: This "solved" my Node problem, as I got 4.2.6 instead of 0.10.25, but I also got a newer version of Unison (and had to download, install and "hold" the 14.04 version).

The newer Node in Windows: So I could make better use of Node in Windows, I installed the Windows version of Vim, making sure the console version was included and .bat files were created so I could use Vim to edit files for Node from the Windows command line, which is somewhat of a mystery to me as I've barely used it (and many years ago at that).

Tue, 04 Apr 2017

Make a website with Ember - great introduction to a framework

I really like the idea behind Less than *ambitious* websites with Ember.js, in which author sheriffderek goes through the steps required to create a simple web site with the Ember JavaScript framework.

Easing into a framework -- that's the way I want to do it.

Thu, 30 Mar 2017

React-Redux links by Mark Erikson

React-Redux links by Mark Erikson https://github.com/markerikson/react-redux-links -- an excellent list that also covers learning JavaScript

Wed, 29 Mar 2017

Eloquent JavaScript - Chapter 2 exercises - Fizz Buzz

I'm not saying I will make it through all 22 chapters of "Eloquent JavaScript," by Marijn Haverbeke, but enough people I respect have recommended the book that I'm doing my best to absorb what I can from it.

To that end, I am doing the exercises in the back of each chapter, and I plan on presenting my solutions here.

This entry also serves as a test of the Highlight.js JavaScript library, which I just added to this Ode site for syntax highlighting of code. I'm using the zenburn CSS.

Back to "Eloquent JavaScript." If you don't want any hints, don't go past the blog index. I will only start showing my code after the "read more" portion of each entry.

Before maybe a year ago, I'd never heard of Fizz Buzz, where you write a program that outputs the words Fizz, Buzz or Fizz Buzz depending on whether a number is divisible by 3, by 5 (and not 3) or by 5 and 3.

Fizz Buzz is supposedly used as a programming test in hiring. I was surprised when it was given as the second exercise in Chapter 2

Read the rest of this post

Sun, 01 Jan 2017

JavaScript books for 2017

"Eloquent JavaScript: A Modern Introduction to Programming" by Marijn Haverbeke

"Learning JavaScript: JavaScript Essentials for Modern Application Development" by Ethan Brown

"Speaking JavaScript: An In-Depth Guide for Programmers" by Dr. Axel Rauschmayer

"Learning JavaScript Data Structures and Algorithms" by Loiane Groner

"You Don't Know JS" (series) by Kyle Simpson

"Programming JavaScript Applications: Robust Web Architecure With Node, HTML5 and Modern JS Libraries" by Eric Elliott

Wed, 28 Dec 2016

Eric Elliott on JavaScript

I am linking to these Eric Elliott articles on JavaScript programming because I don't want to forget about them.

Eric Elliott: 12 Books Every JavaScript Developer Should Read

Eric Elliott: The Software Developer’s Library: A Treasure Trove of Books for People Who Love Code

Eric Elliott: Learn to Code: 13 Tips that Could Save You Years of Effort

Eric Elliott: Native Apps are Doomed

Eric Elliott: Why Native Apps Really are Doomed: Native Apps are Doomed pt 2