Skip to content

Instantly share code, notes, and snippets.

View whal-e3's full-sized avatar

Eric Whale whal-e3

View GitHub Profile
@whal-e3
whal-e3 / loanCalc.js
Created November 4, 2020 05:43
JS Loan Calculator (isFinite(), setTimeout())
// Listen for submit
document.getElementById('loan-form').addEventListener('submit', function (e) {
// Hide results
document.getElementById('results').style.display = 'none';
// Show loader
document.getElementById('loading').style.display = 'block';
setTimeout(calculateResults, 2000);
e.preventDefault();
@whal-e3
whal-e3 / iterNext.py
Last active November 4, 2020 06:48
Python OOP User defined iterator ( how to use __iter__ and __next__ )
class EvenNum:
def __init__(self, size) -> None:
self.size = size
print(self, end=" : ")
def __iter__(self):
self.count = 0
return self
def __next__(self):
@whal-e3
whal-e3 / howToImport.py
Created November 4, 2020 06:47
Python import + if __name__ == "__main__":
# sand box 1 file -------------------------------------------------------------------
import SandBox2
if __name__ == "__main__":
print("Executed when invoked directly from SANDBOX 1")
else:
print("Executed when imported from SANDBOX 1")
@whal-e3
whal-e3 / numGuess.js
Created November 4, 2020 14:45
JS Number guessing game - window.location.reload, a === b ? K : P, isNaN(),
/*
GAME FUNCTION:
- Player must guess a number between a min and max
- Player gets a certain amount of guesses
- Notify player of guesses remaining
- Notify the player of the correct answer if loose
- Let player choose to play again
*/
// Game values
@whal-e3
whal-e3 / prototype.js
Last active November 7, 2020 00:53
JS constructor without class and with class - prototype
// Object prototype
// |
// Person prototype
// |
// Person instance ex) john
// Constructor
function Person(firstName, lastName, dob) {
this.firstName = firstName;
this.lastName = lastName;
@whal-e3
whal-e3 / class.js
Created November 7, 2020 10:49
JS constructor inheritance, Object.create(), class
function Person(firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
Person.prototype.greeting = function () {
return `Hello there ${this.firstName} ${this.lastName}`;
};
const person1 = new Person('John', 'Doe');
@whal-e3
whal-e3 / es5es6.js
Last active November 9, 2020 10:49
JS es5 vs es6 difference
// Book Constructor
function Book(title, author, isbn) {
this.title = title;
this.author = author;
this.isbn = this.isbn;
}
// UI Constructor
function UI() {}
UI.prototype.addBookToList = function (book) {
@whal-e3
whal-e3 / asnyc.js
Last active November 13, 2020 09:26
JS Asynchronous - XHR ~ fetch API
// XHR (xmlHTTPrequest) - JSON ---------------------------------------------------------------------
document.getElementById('button2').addEventListener('click', loadCustomers);
function loadCustomers(e) {
const xhr = new XMLHttpRequest();
xhr.open('GET', 'customers.json', true);
xhr.onload = function () {
if (this.status === 200) {
@whal-e3
whal-e3 / httpLibraryExample.js
Created November 14, 2020 07:32
XHR http library example (GET, POST, PUT DELETE)
function easyHTTP() {
this.http = new XMLHttpRequest();
}
// Make an HTTP GET Request
easyHTTP.prototype.get = function (url, callback) {
this.http.open('GET', url, true);
let self = this;
this.http.onload = function () {
@whal-e3
whal-e3 / promise.js
Created November 14, 2020 10:06
JS es6 Promises
const posts = [
{ title: 'Post one', body: 'this is post one' },
{ title: 'Post two', body: 'this is post two' }
];
function createPost(post) {
return new Promise(function (resolve, reject) {
setTimeout(function () {
posts.push(post);
const error = false;