Skip to content

Instantly share code, notes, and snippets.

View SHoar's full-sized avatar

Sean Hoar SHoar

View GitHub Profile
@SHoar
SHoar / convertToC.js
Created May 6, 2017 14:12
convert fahrenheit to celsius function
function convertToC(fahrenheit) {
var celsius;
celsius = parseFloat((fahrenheit - 32) * 5/9).toFixed(2);
return celsius;
}
@SHoar
SHoar / convertToF.js
Created May 6, 2017 13:13
Convert a celsius temperature to fahrenheit
function convertToF(celsius) {
var fahrenheit;
fahrenheit = parseInt(celsius*9/5 + 32);
return fahrenheit;
}
@SHoar
SHoar / 0_reuse_code.js
Created November 3, 2016 23:44
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
@SHoar
SHoar / RecursiveStringSearch.js
Created September 30, 2016 13:06
Search the "node" for a particular "string"... True if the string exists, False if not.
function talksAbout(node, string) {
if (node.nodeType == document.ELEMENT_NODE) {
for (var i = 0; i < node.childNodes.length; i++) {
if (talksAbout(node.childNodes[i], string))
return true;
}
return false;
} else if (node.nodeType == document.TEXT_NODE) {
return node.nodeValue.indexOf(string) > -1;
}
@SHoar
SHoar / chessboard.js
Created September 18, 2016 18:22
Create a string chessboard with "#" and " " blocks
var board= ""
function chessboard(size){
for (var i = 0; i < size; i++) { // 8 rows
if (i%2==0) {
for (var j = 0; j < size; j++) { // 8 columns
if (j%2 ==0) {
board+=" "
} else {
board+="#"
@SHoar
SHoar / listAllProperties.js
Created September 18, 2016 15:41
How to reveal and list "hidden" object properties
// This can be useful to reveal "hidden" properties (properties in the prototype chain which are not accessible through the object, because another property has the same name earlier in the prototype chain). Listing accessible properties only can easily be done by removing duplicates in the array.
function listAllProperties(o) {
var objectToInspect;
var result = [];
for(objectToInspect = o; objectToInspect !== null; objectToInspect = Object.getPrototypeOf(objectToInspect)){
result = result.concat(Object.getOwnPropertyNames(objectToInspect));
}
@SHoar
SHoar / cashRegister.js
Created July 14, 2016 11:48
build a cash register that scans & totals, but also applies staff discounts.
function StaffMember(employee,discountPercent){
this.employee = employee;
this.discountPercent = discountPercent;
}
var sally = new StaffMember("Sally",5);
var bob = new StaffMember("Bob",10);
// Create yourself again as 'me' with a staff discount of 20%
var me = new StaffMember("Sean",20);
var user = prompt("Pick a number between 1 and 3").toUpperCase();
switch(user){
case '1':
if(user === '1' && !isNaN(user)){
console.log("don't you want more?");
break;
} else {
user = prompt("Try again");
}
var slaying= true;
var youHit = Math.floor(Math.random() * 2);
var damageThisRound = Math.floor(Math.random() * 5 + 1);
var totalDamage = 0;
while (slaying) {
totalDamage += damageThisRound;
if (youHit == 1) {
slaying=false;
if (totalDamage >= 4) {
pre-vetted ES6 polyfills {
ES5-Shim
ES6-Shim // available from https://github.com/es-shims/
Transpiling == Tranforming + compiling; // converts new code into older code equivalents for syntax compatibility.
// ES6
function foo(a=2){ // default parameter value == 2
console.log( a );
}