├── README.md ├── bundle.js ├── count.js ├── day.js ├── foreach.js ├── functional.js ├── hello.js ├── index.html ├── main.js ├── palindrome.html ├── palindrome.js ├── palindrome_file ├── palindrome_url ├── phrases.txt └── wikp /README.md: -------------------------------------------------------------------------------- 1 | # Learn Enough JavaScript to Be Dangerous 2 | 3 | This is a repo for sample code from [*Learn Enough JavaScript to Be Dangerous*](https://www.learnenough.com/javascript-tutorial) by Michael Hartl. It may or may not be up to date with the source in the tutorial; for definitive versions of the code, see the [tutorial itself](https://www.learnenough.com/javascript-tutorial). 4 | -------------------------------------------------------------------------------- /bundle.js: -------------------------------------------------------------------------------- 1 | (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o c.match(/[a-z]/i)).join(""); 22 | } 23 | 24 | // Returns true if the phrase is a palindrome, false otherwise. 25 | this.palindrome = function palindrome() { 26 | if (this.letters()) { 27 | return this.processedContent() === this.processedContent().reverse(); 28 | } else { 29 | return false; 30 | } 31 | } 32 | } 33 | 34 | 35 | },{}],2:[function(require,module,exports){ 36 | 37 | let Phrase = require("mhartl-palindrome"); 38 | 39 | alert(new Phrase("Madam, I'm Adam.").palindrome()); 40 | 41 | 42 | },{"mhartl-palindrome":1}]},{},[2]); 43 | -------------------------------------------------------------------------------- /count.js: -------------------------------------------------------------------------------- 1 | const sonnet = `Let me not to the marriage of true minds 2 | Admit impediments. Love is not love 3 | Which alters when it alteration finds, 4 | Or bends with the remover to remove. 5 | O no, it is an ever-fixed mark 6 | That looks on tempests and is never shaken; 7 | It is the star to every wand'ring bark, 8 | Whose worth's unknown, although his height be taken. 9 | Love's not time's fool, though rosy lips and cheeks 10 | Within his bending sickle's compass come: 11 | Love alters not with his brief hours and weeks, 12 | But bears it out even to the edge of doom. 13 |   If this be error and upon me proved, 14 |   I never writ, nor no man ever loved.`; 15 | 16 | // let uniques = {}; 17 | let words = sonnet.match(/\w+/g); 18 | 19 | let uniques = words.reduce( (uniques, word) => { 20 | if (uniques[word]) { 21 | uniques[word] += 1; 22 | } else { 23 | uniques[word] = 1; 24 | }; 25 | return uniques; 26 | }, {}); 27 | 28 | 29 | // console.log(uniques); 30 | 31 | // // for (let i = 0; i < words.length; i++) { 32 | // // let word = words[i]; 33 | // // if (uniques[word]) { 34 | // // uniques[word] += 1; 35 | // // } else { 36 | // // uniques[word] = 1; 37 | // // } 38 | // // } 39 | 40 | for (let word in uniques) { 41 | console.log(`"${word}" appears ${uniques[word]} time(s)`); 42 | } 43 | 44 | let sum = [1, 3, 4].reduce( (sum, element) => { 45 | return sum += element; 46 | }); 47 | console.log(sum); 48 | -------------------------------------------------------------------------------- /day.js: -------------------------------------------------------------------------------- 1 | function dayOfTheWeek(date) { 2 | const daysOfTheWeek = ["Sunday", "Monday", "Tuesday", "Wednesday", 3 | "Thursday", "Friday", "Saturday"]; 4 | return daysOfTheWeek[date.getDay()]; 5 | } 6 | -------------------------------------------------------------------------------- /foreach.js: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mhartl/js_tutorial/0aed2068c3d97b559b855a0f3bcc94965ef40f49/foreach.js -------------------------------------------------------------------------------- /functional.js: -------------------------------------------------------------------------------- 1 | let states = ["Kansas", "Nebraska", "North Dakota", "South Dakota"]; 2 | 3 | // Returns a URL-friendly version of a string. 4 | function urlify(string) { 5 | return string.toLowerCase().split(/\s+/).join('-'); 6 | } 7 | 8 | // map: Imperative version 9 | function imperativeMap(states) { 10 | let urlStates = []; 11 | states.forEach(function(state) { 12 | urlStates.push(urlify(state)); 13 | }); 14 | return urlStates; 15 | } 16 | console.log(imperativeMap(states)); 17 | 18 | // map: Functional version 19 | function functionalMap(states) { 20 | return states.map(state => urlify(state)); 21 | } 22 | console.log(functionalMap(states)); 23 | 24 | // filter: Imperative version 25 | function imperativeFilter(states) { 26 | let singleWordStates = []; 27 | states.forEach(function(state) { 28 | if (state.split(/\s+/).length === 1) { 29 | singleWordStates.push(state); 30 | } 31 | }); 32 | return singleWordStates; 33 | } 34 | console.log(imperativeFilter(states)); 35 | 36 | // filter: Functional version 37 | function functionalFilter(states) { 38 | return states.filter(state => state.split(/\s+/).length === 1); 39 | } 40 | console.log(functionalFilter(states)); 41 | 42 | let a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; 43 | 44 | // reduce sum: Iterative solution 45 | function iterativeSum(array) { 46 | let total = 0; 47 | array.forEach(function (n) { 48 | total += n; 49 | }); 50 | return total; 51 | } 52 | console.log(iterativeSum(a)); 53 | 54 | // reduce sum: Functional solution 55 | function functionalSum(array) { 56 | return array.reduce((total, n) => { return total += n; }); 57 | } 58 | console.log(functionalSum(a)); 59 | 60 | // reduce object: Imperative solution 61 | function imperativeLengths(states) { 62 | let lengths = {}; 63 | states.forEach(function(state) { 64 | lengths[state] = state.length; 65 | }); 66 | return lengths; 67 | } 68 | console.log(imperativeLengths(states)); 69 | 70 | // reduce object: Functional solution 71 | function functionalLengths(states) { 72 | return states.reduce((lengths, state) => { 73 | lengths[state] = state.length; 74 | return lengths; 75 | }, {}); 76 | } 77 | console.log(functionalLengths(states)); 78 | -------------------------------------------------------------------------------- /hello.js: -------------------------------------------------------------------------------- 1 | console.log("hello, world!"); 2 | let x = prompt("enter"); 3 | -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Learn Enough JavaScript 5 | 6 | 9 | 10 | 11 |

Hello, world!

12 |

This page includes an alert written in JavaScript.

13 | 14 | 15 | -------------------------------------------------------------------------------- /main.js: -------------------------------------------------------------------------------- 1 | let Phrase = require("mhartl-palindrome"); 2 | 3 | function palindromeTester() { 4 | event.preventDefault(); 5 | 6 | let phrase = new Phrase(event.target.phrase.value); 7 | let palindromeResult = document.querySelector("#palindromeResult"); 8 | 9 | if (phrase.palindrome()) { 10 | palindromeResult.innerHTML = `"${phrase.content}" is a palindrome!`; 11 | } else { 12 | palindromeResult.innerHTML = `"${phrase.content}" is a not a palindrome.`; 13 | } 14 | 15 | return false; 16 | } 17 | 18 | document.addEventListener("DOMContentLoaded", function() { 19 | let tester = document.querySelector("#palindromeTester"); 20 | tester.addEventListener("submit", function() { 21 | palindromeTester(event); 22 | }); 23 | }); 24 | -------------------------------------------------------------------------------- /palindrome.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Palindrome Tester 5 | 6 | 7 | 8 | 9 |

Palindrome Tester

10 | 11 | 12 | -------------------------------------------------------------------------------- /palindrome.js: -------------------------------------------------------------------------------- 1 | // Adds `reverse` to all strings. 2 | String.prototype.reverse = function() { 3 | return Array.from(this).reverse().join(""); 4 | } 5 | 6 | function Phrase(content) { 7 | this.content = content; 8 | 9 | this.processedContent = function processedContent() { 10 | return this.content.toLowerCase(); 11 | } 12 | 13 | this.palindrome = function palindrome() { 14 | return this.processedContent() === this.processedContent().reverse(); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /palindrome_file: -------------------------------------------------------------------------------- 1 | #!/usr/local/bin/node 2 | 3 | let fs = require("fs"); 4 | let Phrase = require("mhartl-palindrome"); 5 | 6 | let text = fs.readFileSync("phrases.txt", "utf8"); 7 | text.split("\n").forEach(function (line) { 8 | let phrase = new Phrase(line); 9 | if (phrase.palindrome()) { 10 | console.log("palindrome detected:", line); 11 | } 12 | }); 13 | -------------------------------------------------------------------------------- /palindrome_url: -------------------------------------------------------------------------------- 1 | #!/usr/local/bin/node 2 | 3 | let request = require("request"); 4 | let Phrase = require("mhartl-palindrome"); 5 | let url = 'https://cdn.learnenough.com/phrases.txt' 6 | 7 | request(url, function(error, response, body) { 8 | let lines = body.split("\n"); 9 | let palindromes = lines.filter(line => new Phrase(line).palindrome()); 10 | 11 | palindromes.forEach(function(palindrome) { 12 | console.log("palindrome detected:", palindrome); 13 | }); 14 | }); 15 | -------------------------------------------------------------------------------- /phrases.txt: -------------------------------------------------------------------------------- 1 | A butt tuba 2 | A bad penny always turns up. 3 | A fool and his money are soon parted. 4 | A man, a plan, a canal—Panama! 5 | A man, a plan, a canoe, pasta, heros, rajahs, a coloratura, maps, snipe, percale, macaroni, a gag, a banana bag, a tan, a tag, a banana bag again (or a camel), a crepe, pins, Spam, a rut, a Rolo, cash, a jar, sore hats, a peon, a canal–Panama! 6 | A Toyota's a Toyota 7 | Able was I ere I saw Elba. 8 | Ah, Satan sees Natasha 9 | deified 10 | Dennis sinned. 11 | Dennis and Edna sinned. 12 | Dennis, Nell, Edna, Leon, Nedra, Anita, Rolf, Nora, Alice, Carol, Leo, Jane, Reed, Dena, Dale, Basil, Rae, Penny, Lana, Dave, Denny, Lena, Ida, Bernadette, Ben, Ray, Lila, Nina, Jo, Ira, Mara, Sara, Mario, Jan, Ina, Lily, Arne, Bette, Dan, Reba, Diane, Lynn, Ed, Eva, Dana, Lynne, Pearl, Isabel, Ada, Ned, Dee, Rena, Joel, Lora, Cecil, Aaron, Flora, Tina, Arden, Noel, and Ellen sinned. 13 | Fools rush in where angels fear to tread. 14 | Gather ye rosebuds while ye may. 15 | Go hang a salami, I'm a lasagna hog. 16 | level 17 | Haste makes waste. 18 | If the shoe fits, wear it. 19 | If wishes were fishes then no man would starve. 20 | Madam, I’m Adam. 21 | No "x" in "Nixon" 22 | No devil lived on 23 | Once bitten, twice shy. 24 | Race fast, safe car 25 | racecar 26 | radar 27 | Those who cannot remember the past are condemned to repeat it. 28 | Was it a bar or a bat I saw? 29 | Was it a car or a cat I saw? 30 | Was it a cat I saw? 31 | When in Rome, do as the Romans do. 32 | Yo, banana boy! 33 | -------------------------------------------------------------------------------- /wikp: -------------------------------------------------------------------------------- 1 | #!/usr/local/bin/node 2 | 3 | // Returns the paragraphs from a Wikipedia link, stripped of reference numbers. 4 | // Especially useful for text-to-speech (both native and foreign). 5 | 6 | let request = require("request"); 7 | let url = process.argv[2]; 8 | 9 | const jsdom = require("jsdom"); 10 | const { JSDOM } = jsdom; 11 | 12 | request(url, function(error, response, body) { 13 | // Simulate a Document Object Model. 14 | const { document } = (new JSDOM(body)).window; 15 | 16 | // Grab all the paragraphs and references. 17 | let paragraphs = document.querySelectorAll("p"); 18 | let references = document.querySelectorAll(".reference") 19 | 20 | // Remove any references. 21 | references.forEach(function(reference) { 22 | reference.remove(); 23 | }) 24 | 25 | // Print out all the paragraphs. 26 | paragraphs.forEach(function(paragraph) { 27 | console.log(paragraph.textContent + "\n"); 28 | }) 29 | }); 30 | --------------------------------------------------------------------------------