Skip to content

Instantly share code, notes, and snippets.

/**
* Write a function to determine if a number is an integer
*/
let isInt = function (num) {
return (!isNaN(num) && parseInt(num) === num)
}
console.log(3, isInt(3));
console.log(3.5, isInt(3.5));
console.log(1.00000, isInt(1.00000));
// Using `this` inside an object
// The object has functions created three different ways
// What will be the result of the three log statements?
const circle = {
radius: 10,
circumference: function () {
return (2 * Math.PI * this.radius);
},
diameter() {
//Why do we get this weird result from the map method?
// myarray.map(func);
//We want to convert 3 strings into numbers
let result = ['1', '7', '11'].map(parseInt); //returns [1, NaN, 3]
// solution
['1', '7', '11'].map(item => parseInt(item))
console.log(result);
// Generate Random Hex Colour Values
/*
use Math.random
*/
function colour() {
//var hoisting vs let hoisting
//WHAT will be the output from this code and why?
function f() {
console.log('var', area); // undefined
if(area !== 'undefined) {
console.log('var', area);
}
console.log('let', name); // reference error
try {
//version 1
let a1 = Array.from({
length: 5
}, n => Math.random());
console.log('1', a1);
//version 2
let a2 = new Array(5).fill(1).map(n => Math.random());
console.log('2', a2);
function f1(a) {
let b = 2;
setTimeout(function () {
console.log(a, b)
}, 1000);
}
// has a problem
// use let to scope variable inside for curly braces
function f2() {
for (var i = 0; i < 3; i++) {
/**
* Create an example of a JavaScript Singleton.
* After the first object is created, it will return additional
* references to itself
*/
let obj = (function () {
let objInstance; //private variable
function create() { //private function to create methods and properties
let _isRunning = false;
@henryspivey
henryspivey / gist:6d4a8fad4c1e17514e1eda944e8f5d0a
Created October 23, 2016 21:53
Simple Custom Backbutton in angular js for Ionic
.directive('backButton', ['$ionicHistory',function($ionicHistory){
return {
restrict: 'E',
template: "<button class='button button-clear' id='customBackButton'><i class='button-icon icon ion-ios-arrow-left'></i> Back</button>",
link: function($scope, element, attrs) {
element.bind('click', function(){
$ionicHistory.goBack();
})
}
}