This repo contains coding examples to be copied by first-time learners of programming, on the hypothesis that: "Typing code helps you learn, so rather than giving you text to copy and paste the code is an image that you must read, hold in your mind, type in to your editor and then manually check for accuracy."
- The
code-examples/directory contains the source files that must be transformed into images. They are divided into multiple stages and extras. - The
dev/directory contains all the scripts that are used to transform the source files into images. - The
docs/pages/directory contains HTML files that show the examples and its css. When examples are updated, these may need updating too. - The
docs/img/directory contains images of the examples.
For the project to work correctly, you must install all the relevant modules:
npm install
To execute the main script, type from the command line at the root of the project:
npm run build
If you want to add an example you must add its source code inside code-examples/stage-* or code-examples/extras-*.
Then you must create a .md file inside docs/md/stage-*/ with the same name as your source file.
Example: You have added 12-hungry.js inside code-examples/stage-1/, you must create 12-hungry.js.md inside docs/md/stage-*/.
# Example title
any description goes here, including **formatting**
<img alt="What you want for the image" >
any further stuff goes here
## Documentation links
* [the modulo operator `%`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Remainder)
## Metadata
* badge: for-loop
* badge: for-loop
* category: foobar
* difficulty: easyEven if you don't have documentation links, you need to add the three titles.
You can create a new code-examples/stage-?/ or code-examples/extras-?/ folder and put your source code inside.
If you create a new code-examples/stage-?/ or code-examples/extras-?/ folder you should create the corresponding page: docs/stage-?.html or docs/extras-?.html.
Languages currently supported to convert :
- javascript
- html
- xml
If you want to make the application support another language go inside dev/create-img-from-source.ts
Import your language from highlight.js/lib/languages
Add hljs.registerLanguage(languageName, languageDefinition) near the top of the file.
Documentation for highlightjs.registerLanguage() :
https://highlightjs.readthedocs.io/en/latest/api.html#registerlanguage-languagename-languagedefinition
These examples contain just if, variables, strings and numbers, console.log, and a sync promp.
The advanced variations touch on arrays, objects, and do-while loops.
- given a number, log whether it is odd or even
- … or fractional
- … or not a number at all
- given two params, return their concatenation
- given two numbers, return their sum
- how long is this word? given a string, log its length
- rock - paper - scissors - one game
- what number am I thinking? given a number between 1 and 10, get random number, output if you guessed right
- … sarcastic, passive-aggresive version of this, as error checking
- maybe this should still use command-line argument?
- … more conversational, nice at first but increasingly unhappy and annoyed
- … with colors
Advanced:
- rock-paper-scissors with array and object so it's shorter
- rock-paper-scissors in a loop until the user enters "stop"
These examples deal with arrays, objects, loops.
The advanced variations touch on functions?
- multiply by adding
- count words by counting spaces
- … ignoring consecutive spaces
- … count sentences by counting ". "
- take a file name, remove filename extension
- generate random text of N words
- generate random text of N words, but with sentences
- in-memory nickbook (like a phonebook)
Advanced:
- remove filename extension with lastIndexOf and substring
- generate sentences with functions to remove repetition
- function: isPrime
- function: reverseArray
- function isPalindrome, using reverseArray
- function to roll a dice
- compute percentages of numbers
- show histogram of N dice rolls
- challenge: show histogram of N rolls of 2 dice each
- draw a tower
- … with colors https://npmjs.com/package/colors
Advanced:
- draw several towers next to one another
- play hanoi towers without recursion
- play hanoi towers with recursion
change the above programs to use process.argv rather than prompt
-
replicate
seq first last -
replicate
seq [first [incr]] last -
reverse a text file
-
reverse each word in a text file
-
rot13
-
count the words in a text file
-
print out top-N most frequent words (challenge: omit counting words shorter than argument
n) (challenge: pass a stop list as a paramteter) (challenge: modify so it works with words or characters)
Advanced:
- substitution cipher or caesar cipher or some such (Matt)
- rock - paper - scissors - just with if and Math.random(), log one of them
- rock - paper - scissors judge: takes two words, logs who won
- … try to add spock and lizard
- …
node judge `node rps` `node rps` - … then try write rock.js which always just logs the word "rock", and let rps.js play against rock.js
- Milliways: an example where we took a bad repetitive program for ordering meals at the Restaurant at the End of the Universe, and rewrote it in an online session to use functions and loops in the right way.
maybe our programs can contain a function that does readline with a promise, which they simply copy, and then we can have a conversational interface, so we can avoid npm i in the first examples
- replicate the PI two-digit distribution check from https://www.youtube.com/watch?v=etx0k1nLn78 with plotting in SVG or on Canvas
also continue adding: on your own, try to write this instead: …
- ELIZA