Created
July 4, 2020 14:27
-
-
Save evanrolfe/4ab09eb0e9a56c9fe19f5f9dd3a34b68 to your computer and use it in GitHub Desktop.
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
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' | |
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
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