Skip to content

Instantly share code, notes, and snippets.

View ryasmi's full-sized avatar
🐿️
Checks emails once per day Mon-Fri

Ryan Smith ryasmi

🐿️
Checks emails once per day Mon-Fri
View GitHub Profile
@ryasmi
ryasmi / round.js
Created February 19, 2013 21:05
Rounds an floating point number to an integer.
var round = function (value) {
// {value is some number}
var multiplier = Math.abs(value) / value;
var sValue = (value + (0.5 * multiplier)).toString();
var point = sValue.indexOf(".");
return parseInt(sValue.substring(0, point === -1 ? sValue.length : point), 10);
// {returns value rounded}
};
@ryasmi
ryasmi / divide.js
Created February 24, 2013 17:08
Returns the result of a division where the numerator is divided by the denominator. This highlights how division is actually just multiplication.
var divide = function (numerator, denominator) {
return numerator * (1 / denominator);
};
import java.io.*;
import java.util.*;
public class FilterTextFile {
public static void main(String[] args) throws FileNotFoundException {
Scanner fScan, cScan = new Scanner(System.in);
String inFile, outFile, findStr;
FileInputStream fileInput;
PrintWriter fileOutput;
@ryasmi
ryasmi / ReverseColour.js
Last active December 15, 2015 12:09
Reverses RGB colours.
var reverseNumber = function (maxValue, value) {
return (maxValue - 1) - value;
};
var hex2Dec = function (hexStr) {
return parseInt(hexStr, 16);
};
var dec2Hex = function (decStr, digits) {
var hexStr = decStr.toString(16);
var chars = digits - hexStr.length;
var count;
@ryasmi
ryasmi / bubbleSort.js
Last active December 15, 2015 16:59
Sorting algorithms written in JavaScript for an assignment at Oxford Brookes University for the Discrete Maths module.
var bubbleSort = function (list) {
var i, temp;
var swapped = true;
var iterations = 0;
var n = list.length;
while (swapped) {
swapped = false;
for (i = 1; i < n; i += 1) {
iterations += 1;
@ryasmi
ryasmi / StringToArray.js
Last active December 16, 2015 11:09
Turns a string into an array.
// Using func. https://gist.github.com/ryansmith94/5564105
func("toArray", function (elemDelimeter, attrDelimeter, fn) {
var i, elemArray, len;
elemArray = this.split(elemDelimeter);
len = elemArray.length;
for (i = 0; i < len; i += 1) {
elemArray[i] = elemArray[i].split(attrDelimeter);
if (fn) {
@ryasmi
ryasmi / ReplaceAllInString.js
Last active December 16, 2015 11:09
Replaces all occurrences of a string (x) within a string (this) with another string (y).
// Using func. https://gist.github.com/ryansmith94/5564105
func("replaceAll", function (x, y) {
var self = this;
while (self.indexOf(x) !== -1) {
self = self.replace(x, y);
}
return self;
}, String.prototype);
// Using func. https://gist.github.com/ryansmith94/5564105
func("toObject", function (keys, values) {
var i;
var len = keys.length;
var result = {};
values = values || this;
for (i = 0; i < len; i += 1) {
result[keys[i]] = values[i];
@ryasmi
ryasmi / Function.js
Last active December 16, 2015 17:19
Things learned in JavaScript and they are good. Sourced mainly from Paul Irish (watched his video a while ago but I wanted to get it down in code).
// new Function.
var x = (new Function("return " + data))();
// (1) Better than using eval.
var x = eval("(" + data + ")");
@ryasmi
ryasmi / func.js
Last active December 17, 2015 06:18
A way to extend functionality using prototypes.
// A function to create non-enumerable functions in specific scopes.
var func = (function (defineProp, undefined) {
"use strict";
// In older browsers Object.defineProperty is not available.
// In IE8 Object.defineProperty can only be used on DOM elements.
var initProp = (!defineProp || !defineProp({}, "ie8Test", {value: false})) ?
function () {} :
function (name, scope, write, enumer, config) {
enumer = (enumer === undefined ? false : enumer);