Skip to content

Instantly share code, notes, and snippets.

@juan-m-medina
Created February 3, 2016 03:08
Show Gist options
  • Save juan-m-medina/ff93158a60456a7027c5 to your computer and use it in GitHub Desktop.
Save juan-m-medina/ff93158a60456a7027c5 to your computer and use it in GitHub Desktop.
ES6 Generators
"use strict";
var coolPeople = {};
coolPeople[Symbol.iterator] = function* () {
yield "Supriya";
yield "Andy (Basu)";
yield "Sachin";
yield "Tom";
};
function* boringPeople() {
yield* ["Juan", "Jackies", "Ritesh", "Mark"];
};
class Amortization {
constructor(principal, annualRate, months) {
this.principal = principal;
this.annualRate = annualRate;
this.months = months;
this.monthlyRate = this.annualRate/(12*100);
}
getMonthlyPayment() {
let totalInterest = Math.pow(1 + this.monthlyRate, this.months);
return (this.monthlyRate * this.principal * totalInterest)/(totalInterest - 1);
}
* getTable() {
let currentMonth = 1;
let currentPrincipal = this.principal;
let monthlyPayment = this.getMonthlyPayment();
while(currentMonth <= this.months) {
let currentMonthInterest = currentPrincipal * this.monthlyRate;
let currentMonthPrincipal = monthlyPayment - currentMonthInterest;
yield { currentMonth, monthlyPayment, currentMonthPrincipal, currentMonthInterest };
currentPrincipal -= currentMonthPrincipal;
currentMonth++;
}
}
}
console.log(typeof coolPeople);
console.log(typeof boringPeople);
for(let coolPerson of coolPeople) {
console.log(`${coolPerson} is cool.`);
}
for(let boringPerson of boringPeople()) {
console.log(`${boringPerson} is boring.`)
}
var amortizationInstance = new Amortization(100000, 10, 12, 0, 0);
console.log(amortizationInstance.getMonthlyPayment());
for (let amortization of amortizationInstance.getTable()) {
console.log(`Month: ${amortization.currentMonth} - Payment: ${amortization.monthlyPayment} - Interest: ${amortization.currentMonthInterest} - Principal: ${amortization.currentMonthPrincipal}`);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment