Skip to content

Instantly share code, notes, and snippets.

@evanrolfe
Created July 4, 2020 14:27
Show Gist options
  • Save evanrolfe/4ab09eb0e9a56c9fe19f5f9dd3a34b68 to your computer and use it in GitHub Desktop.
Save evanrolfe/4ab09eb0e9a56c9fe19f5f9dd3a34b68 to your computer and use it in GitHub Desktop.
class AddressBook {
constructor() {
this.names = [];
this.emails = [];
}
store(name, email) {
this.names.push(name);
this.emails.push(email);
}
lookup(name) {
const index = this.names.indexOf(name);
if (index >= 0) {
return this.emails[index];
} else {
return 'address not found';
}
}
}
const myAddressBook = new AddressBook();
myAddressBook.store("bart", "[email protected]");
myAddressBook.store("maggie", "[email protected]");
console.log(myAddressBook.lookup("bart"));
// '[email protected]'
console.log(myAddressBook.lookup("homer"));
// 'address not found'
class RunningTotal {
constructor() {
this.currentTotal = 0;
}
add(n) {
this.currentTotal += n;
}
getTotal() {
return this.currentTotal;
}
}
const runningTotal = new RunningTotal();
runningTotal.add(3);
runningTotal.add(7);
runningTotal.add(7);
console.log(runningTotal.getTotal());
// 17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment