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
function countOfNegatives(a) {
return a.filter(x => x < 0).length;
}
function isPrime(n) {
if (n < 1 || !Number.isSafeInteger(n)) {
throw new Error('Only positive safe integers can be tested for primality');
}
// Shortcut: Both 1 and even numbers not equal to 2 are not prime
if (n < 2 || (n % 2 === 0 && n !== 2)) {
return false;
}
// Try all odd divisors starting at 3. Evens already rejected above.
for (let d = 3; d * d < n; d += 2) {
if (n % d === 0) {
return false;
}
}
return true;
}
function randomBetween(low, high) {
return low + (Math.random() * (high - low));
}
function acronym(s) {
return s.toLowerCase().split(/\s+/).map(w => w[0]).join('');
}
function median(x, y, z) {
const array = [x, y, z];
if (array.some(x => typeof x !== 'number' || isNaN(x))) {
throw new Error('All arguments must be numbers other than NaN');
}
array.sort((a, b) => a - b);
return array[1];
}
function occurrences(string, desiredCharacter) {
let count = 0;
for (let character of string) {
if (character === desiredCharacter) {
count += 1;
}
}
return count;
}
function sumOfEvenSquares(a) {
return a.filter(x => x % 2 === 0).map(x => x * x).reduce((x, y) => x + y, 0);
}
function insideSubstring(s1, s2) {
const [shorter, longer] = s1.length < s2.length ? [s1, s2] : [s2, s1];
const pos = longer.indexOf(shorter);
return pos >= 1 && pos < (longer.length - shorter.length);
}
function mostInterestingPerson(a) {
function interestCount(p) {
try {
return new Set(p.interests.map(s => s.toLowerCase())).size;
} catch (e) {
return 0;
}
}
const copy = a.slice(); // don't destroy a
copy.sort((p, q) => interestCount(q) - interestCount(p));
return copy[0];
}
class Point {
constructor(lat, lon) {
if (isNaN(lat) || isNaN(lon) || lat < -90 || lat > 90 || lon < -180 || lon > 180) {
throw new Error('Latitude or longitude out of range');
}
this.lat = lat;
...