Pages

Showing posts with label JavaScript. Show all posts
Showing posts with label JavaScript. Show all posts

Sunday, November 13, 2016

Logical and Bitwise Operators


a: ← Operand (number, true or false)
b: ← Operand (number, true or false)

← Operand examples


Exercise

  • For each example pair of operands, study the result of "passing" the operands to the corresponding logical or bitwise operators.
  • Try other pairs of operands.
  • Attempt to empirically determine the rules associated with each logical and bitwise operator.
  • For each operator, after you have formed a hypothesis about how the operator works, compare your hypothesis with documentation (print or online) for that operator. For example:
  • Formulate a question about something you don't understand related to a logical or bitwise operator, and find an answer to your question. How much confidence do you have in the answer? Why?
  • Optional: Compare the logical and bitwise operators for JavaScript with those for Java.
    • Does Java have the same logical and bitwise operators that JavaScript does?
    • Are there any differences between the way the JavaScript logical and bitwise operators work compared to the corresponding Java ones?

Thursday, October 27, 2016

Quadrilateral Tests

function QuadrilateralTests(epsilon) {

  var methods = [];
  methods.push({name: "area", args: [""]});
  methods.push({name: "circumference", args: [""]});
  methods.push({name: "side", args: [0,1,2,3]});
  methods.push({name: "angle", args: [0,1,2,3]});

  var difference;

  function closeEnough(a, b) {
    difference = Math.abs(a-b);
    return difference < epsilon;
  }

  function test(f) {
    var q = f();
    for (var i=0; i < methods.length; i++) {
      var name = methods[i].name;
      var args = methods[i].args;
      for (var j=0; j < args.length; j++) {
        var arg = args[j];
        if (!closeEnough(q[name](arg), f[name+arg])) {
          var errorMessage = "TEST FAILED: " + f.name + "." + name;
          console.log(errorMessage);
          console.log("Expected: " + f[name+arg]);
          console.log("Actual: " + q[name](arg));
          console.log("Difference: " + difference);
          console.log("Epsilon: " + epsilon);
          throw new Error(errorMessage);
        }
      }
    }
  }

  function isoscelesTrapezoid() {
    return new Quadrilateral(1, Math.sqrt(2), 3, 135, 45);
  }
  isoscelesTrapezoid.area = 2;
  isoscelesTrapezoid.circumference = 6.82842712474619;
  isoscelesTrapezoid.side0 = 1;
  isoscelesTrapezoid.side1 = Math.sqrt(2);
  isoscelesTrapezoid.side2 = 3;
  isoscelesTrapezoid.side3 = Math.sqrt(2);
  isoscelesTrapezoid.angle0 = 135;
  isoscelesTrapezoid.angle1 = 135;
  isoscelesTrapezoid.angle2 = 45;
  isoscelesTrapezoid.angle3 = 45;

  function unitSquare() {
    return new Quadrilateral(1, 1, 1, 90, 90);
  }
  unitSquare.area = 1;
  unitSquare.circumference = 4;
  unitSquare.side0 = 1;
  unitSquare.side1 = 1;
  unitSquare.side2 = 1;
  unitSquare.side3 = 1;
  unitSquare.angle0 = 90;
  unitSquare.angle1 = 90;
  unitSquare.angle2 = 90;
  unitSquare.angle3 = 90;

  test(unitSquare);
  test(isoscelesTrapezoid);
}

QuadrilateralTests(1e-13);

Polygon

See Also


Possible Scores

  • Quadrilateral score: 45
  • Parallelgram score: 55
  • Rhomboid score: 55
  • Rhombus score: 55
  • Rectangle score: 55
  • Square score: 55
  • Total score: 320


Monday, October 24, 2016

Two-Button Stopwatch

Sunday, October 23, 2016

JavaScript: inheritsFrom (and superConstructor?)

Object.prototype.inheritsFrom = function(Super) {
  function newPrototype(prototype) {
    function Prototype() {
    }
    Prototype.prototype = prototype;
    return new Prototype();
  }
  this.prototype = newPrototype(Super.prototype);  
  this.prototype.constructor = this;
};

/* THIS IS FUBAR!
Object.prototype.superConstructor = function() {
  this.__proto__.__proto__.constructor.apply(this, arguments);
};
*/

Example: Foo ← Bar

function Foo(bar) { this.foo = function() { return bar; }; }
Foo.prototype.bar = function() { return this.foo(); };
Foo.BAR = function() { return "FOOBAR"; };

function Bar() { Foo.call(this, "BAR"); }
Bar.inheritsFrom(Foo);

function fooBar() {
  console.log("Foo.BAR(): " + Foo.BAR());
  var foo = new Foo("FOO");
  console.log(foo);
  console.log("foo.bar(): " + foo.bar());
  var bar = new Bar();
  console.log(bar);
  console.log("bar.bar(): " + bar.bar());
}
See Also

Saturday, October 22, 2016

Ponder This (October, 2016)

The October 2016 Challenge



play again?



10,000 digits of:

Sources

Tuesday, October 18, 2016

Pin the tail on the donkey



Image source: https://openclipart.org/detail/217463/jackass

Monday, October 17, 2016

Brave New World