JSFiddle

  • Lots of Functions

    No-Library (pure JS), HTML, CSS, JavaScript

    // A homework problem I assigned to the freshmen.
    
    function countOfNegatives(a) {
      return a.filter(x => x < 0).length;
    }
    
    function isPrime(n) {
      let LARGEST = 1000000000000;
      if (isNaN(n) || n < 2 || n > LARGEST || n % 1 !== 0 ...
  • Multiple Bouncing Balls #5 is the latest revision

    Starting point of a class exercise where we make incremental improvements.

    var canvas = {
        element: document.getElementById('canvas'),
        width: 600,
        height: 400,
        initialize: function () {
            this.element.style.width = this.width + 'px';
            this ...
  • Baby K'tah

    Massively stripped-down simulation of the famous K'tah

    // This is a rough basis from which to build a more sophisticated game.
    
    const canvas = document.getElementById('canvas');
    const ctx ...
  • Ohm Arithmetic Expression Evaluation - CoffeeScript version

    Simple example of using Ohm/JS to evaluate simple arithmetic expressions

    g = ohm.grammarFromScriptElement()
    
    semantics = g.semantics().addOperation('eval', {
      Exp_plus: (x, op, y) -> x.eval() + y.eval(),
      Exp_minus: (x, op, y ...
  • Ohm Arithmetic Expression Evaluation

    Simple example of using Ohm/JS to evaluate simple arithmetic expressions

    let g = ohm.grammarFromScriptElement();
    
    let semantics = g.semantics().addOperation('eval', {
      Exp_plus: (x, op, y) => x.eval() + y.eval(),
      Exp_minus: (x ...
  • Multiple Bouncing Balls - Starter Version #50 is the latest revision

    Starting point of a class exercise where we make incremental improvements.

    var canvas = {
        element: document.getElementById('canvas'),
        width: 600,
        height: 400,
        initialize: function () {
            this.element.style.width = this.width + 'px';
            this ...
  • Bouncing Ball - Rough Hack #63 is the latest revision

    Starting point of a class exercise where we make incremental improvements.

    var canvas = {
        element: document.getElementById('canvas'),
        width: 600,
        height: 400,
        initialize: function () {
            this.element.style.width = this.width + 'px';
            this ...
  • Datetimes and Locales

    Shows the current instant in a variety of locales

    var now = new Date();
    var message = now.toISOString() + '\n';
    ['en-US','de-DE','zh-CN','ar-EG','hi-IN'].forEach(function (locale) {
        message += now.toLocaleString ...
  • A little BMI Calculator #6 is the latest revision

    A modification of a little experiment started by one of my students.

    var calculateBMI = function() {
        var KILOGRAMS_PER_POUND = 0.453592;
        var METERS_PER_INCH = 0.0254;
        var kg = pounds.value * KILOGRAMS_PER_POUND;
        var m = inches.value ...
  • Finding the nth prime #24 is the latest revision

    A small function to compute the nth prime.

    var prime = function (n) {
        if (n > 100000) {
            throw new Error("Not that many");
        }
        var primes = new Array(n + 1);
        primes ...