This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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() { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Generate Random Hex Colour Values | |
/* | |
use Math.random | |
*/ | |
function colour() { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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 { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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++) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.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(); | |
}) | |
} | |
} |