Skip to content

Instantly share code, notes, and snippets.

View krohne's full-sized avatar

Gregory Krohne krohne

  • Atlanta, Georgia, USA
View GitHub Profile
@krohne
krohne / evens_no_looping.js
Last active August 29, 2015 14:12
From an array of integers (negative and positive), return the even integers, without looping or using Array API calls
function getIntegers() {
// Go to the source for true random numbers
// Fetch 10 integers from -100 to 100
var xmlHttp = new XMLHttpRequest(),
randomUrl = "http://www.random.org/integer-sets/?sets=1&num=10&min=-100&max=100&commas=on&order=index&format=plain&rnd=new";
xmlHttp.open("GET", randomUrl, false);
xmlHttp.send();
return xmlHttp.responseText.split(", "); // convert to an array
@krohne
krohne / isEven.js
Created December 29, 2014 19:14
Determine if integer is an even number, using last digit.
function isEven(int) {
var intStr = int.toString(),
lastDigit = intStr.charAt(intStr.length - 1);
return ("02468".indexOf(lastDigit) > -1);
}
@krohne
krohne / javaStringToJavaScriptPrimitive.js
Created February 4, 2015 22:51
Convert Java String object to JavaScript String primitive in Rhino
// The good part:
// String(javaString).valueOf()
// Follow the same pattern to convert other Java types to corresponding JavaScript primitives
var javaString = new java.lang.String("test");
print('javaString:', javaString);
print('typeof javaString:', typeof javaString); // Object
print('javaString instanceof String:', (javaString instanceof String)); // true (really a java.lang.String object)
print('String(javaString).valueOf() === "test":', String(javaString).valueOf() === "test"); // true: Converted to JS primitive
/**
* Parses mixed type values into booleans. This is the same function as filter_var in PHP using boolean validation
* @param {Mixed} value
* @param {Boolean} nullOnFailure = false
* @return {Boolean|Null}
*/
var parseBooleanStyle = function(value, nullOnFailure = false){
if (typeof value !== 'string')
return Boolean(value);
@krohne
krohne / 4-minute-chess-timer.md
Last active August 29, 2015 14:24
4 minute chess timer

A 4 minute Chess (or any 2-player game) timer.

Try it out: http://codepen.io/anon/pen/oXqNoO

TODO:

  • start button to start timer for white player
  • timer.add(seconds) function for Fischer or Bronstein clock.
  • timer.delay(seconds) function for Simple Delay clock
  • Responsive design for tablets and phones
  • Add onClick to timers for touchscreens
@krohne
krohne / index.html
Last active September 30, 2015 21:38
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body>
<div class="blue">////////</div>
<script id="jsbin-javascript">
console.log('1. Missing element');
@krohne
krohne / evens.js
Created October 18, 2016 15:27
Given an array of n integers (example: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 100, 112, 113] ), // remove all odd numbers, leaving only the even numbers.
// given an array of n integers (example: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 100, 112, 113] ),
// remove all odd numbers, leaving only the even numbers.
const x = [
-Math.MAX_VALUE, -113, -112, -100, -10, -9, -8, -7, -6, -5, -4, -3, -2, -1, -Math.MIN_VALUE, -0,
0, Math.MIN_VALUE, 1, 2, 2, 3, 4, 5, 6, 7, 8, 9, 10, 100, 112, 113, Math.MAX_VALUE
];
console.log(x.filter( n => { return ( n % 2 === 0); }));
@krohne
krohne / downloadsize.js
Last active October 18, 2016 17:20
Nashorn Javascript CLI that will report the download size of a given URL as a command line parameter
#!env jjs
// jjs filesize.js -- config.txt
// Sample config.txt:
// http://unec.edu.az/application/uploads/2014/12/pdf-sample.pdf
var Files = Java.type('java.nio.file.Files');
var Paths = Java.type('java.nio.file.Paths');
var URL = Java.type('java.net.URL');
function getSize(url) {
@krohne
krohne / yesterday.js
Created January 19, 2017 13:59
Yesterday's date in one line
return ( (new Date( new Date( (new Date()).toISOString().slice(0,10) ) - 1 ) ).toISOString().slice(0,10) );
@krohne
krohne / randomDigits.js
Last active November 7, 2017 20:40
random decimal digits using Node crypto
// Limited to max 16 digits
function randomDigits(digits) {
return parseInt(
crypto
.randomBytes(Math.floor(digits/2))
.toString('hex')
,16)
.toString(10)
.substr(-digits);
}