JSFiddle - React, Tailwind, and code Playground
by Ray Toal
HTML
<link rel="stylesheet" href="https://code.jquery.com/qunit/qunit-git.css">
<script src="https://code.jquery.com/qunit/qunit-git.js"></script>
<div id="qunit"></div>
<div id="qunit-fixture"></div>
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) {
throw new Error('Cannot test this for primality');
}
if (n % 2 === 0 && n !== 2) {
return false;
}
for (var d = 3; d * d < n; d += 2) {
if (n % d === 0) {
return false;
}
}
return true;
}
function randomBetween(x, y) {
return x + Math.random() * (y - x);
}
function acronym(s) {
return s.toLowerCase().split(/\s+/).map(s => s[0]).join('');
}
function median(a, b, c) {
return a + b + c - Math.min(a, b, c) - Math.max(a, b, c);
}
function occurrences(s, c) {
let count = 0;
for (let ch of s) {
if (ch === c) {
count += 1;
}
}
return count;
}
function sumOfEvenSquares(a) {
const even = (x) => x % 2 === 0;
const square = (x) => x * x;
const plus = (x, y) => x + y;
return a.filter(even).map(square).reduce(plus, 0);
}
function prefix(s1, s2) {
return s1 === '' || s2 === '' || s1.indexOf(s2) === 0 || s2.indexOf(s1) === 0;
}
function Point(latitude, longitude) {
if (isNaN(latitude) || Math.abs(latitude) > 90) {
throw "Illegal latitude: " + latitude;
}
if (isNaN(longitude) || Math.abs(longitude) > 180) {
throw "Illegal longitude: " + longitude;
}
this.latitude = latitude;
this.longitude = longitude;
}
Point.prototype.inArcticCircle = function () {
return this.latitude >= 66.5628;
};
Point.prototype.inAntarcticCircle = function () {
return this.latitude <= -66.5628;
};
Point.prototype.inTropics = function () {
return Math.abs(this.latitude) < 23.4372;
};
Point.prototype.antipode = function () {
return new Point(-this.latitude,
(this.longitude < 0) ? 180 + this.longitude : this.longitude - 180);
};
QUnit.test("My negative counter function works", test => {
test.equal(countOfNegatives([]), 0);
...