Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Unsubscribed User
17,284 PointsCan anyone solve this? Thanks...
Can someone help me get to the final result of this problem?
/* Dowington, PA had 1000 citizens on the night the blob escaped
its meteorite. At first, the blob could only find and consume
Pennsylvanians at a rate of 1/hour. However, each time it digested
someone, it became faster and stronger: adding to its consumption
rate by 1 person/hour.
persons consumed | rate of consumption
------------------|---------------------
0 | 1/hour
1 | 2/hour
2 | 3/hour
3 | 4/hour
TODO: First, make a constructor function, called Blob, that makes blobs.
It should internally (in the constructor) set this.peoplePerHour to 1 on initialization and have a function
eatTown that takes a population size and returns the number of
hours it takes to eat that town! It should also keep track of the
peoplePerHour increase due to eating the town.
TODO: Next, create an instance of Blob named blob.
.
*/
var Nowhereville = 0;
var Dowington = 1000;
var Smallsville = 5000;
var Portland = 500000;
// Use the eatTown method and console log result:
// 1) How log for four different blobs to each eat one of the towns
console.log( new Blob().eatTown(Nowhereville) );
console.log( new Blob().eatTown(Dowington) );
console.log( new Blob().eatTown(Smallsville) );
console.log( new Blob().eatTown(Portland) );
// 2) How log for the same blob to sequentially eat all four towns!
var blob = new Blob();
console.log( blob.eatTown(Nowhereville) );
console.log( blob.eatTown(Dowington) );
console.log( blob.eatTown(Smallsville) );
console.log( blob.eatTown(Portland) );
3 Answers
Colin Bell
29,679 PointsFirst portion is easy enough. Create a constructor function and set this.peoplePerHour to 1.
function Blob() {
this.peoplePerHour = 1;
}
Then create a function within Blob() called eatTown that takes a population size and returns the number of hours it takes to eat that town!
It might be simpler to first think about how you'd go about creating it as its own function:
var eatTown = function (population) {
var hours=0;
// You've already added this as this.peoplePerHour in the parent function().
// Just adding it for clarity and functionality
var peoplePerHour = 1;
// While loop that checks if there are still people left to eat.
while ( population > 0 ) {
// Population equals itself minus the the people eaten that hour
population = population - peoplePerHour;
// Add an hour
hours ++;
// Add one to the rate of people eaten per hour
peoplePerHour ++;
}
// Return the number of hours taken to eat everyone
return hours;
};
}
Only thing to do after that is include the eatTown function in the Blob constructor function and make sure it references the Blob constructor.
function Blob() {
this.peoplePerHour = 1;
this.eatTown = function (population) {
var hours=0;
while(population > 0) {
population = population - this.peoplePerHour;
hours ++;
this.peoplePerHour ++;
}
return hours;
};
}
There might be 5 people left when the blob is eating 102 people/hour. If it's eating that quickly, it wouldn't take a full hour to eat the last 5 people. To account for that and get a more precise time, you'd just have to add in one more conditional:
function Blob() {
this.peoplePerHour = 1;
this.eatTown = function (population) {
var hours=0;
while(population > 0) {
population = population - this.peoplePerHour;
// If the previous round of eating results in negative people then take the
// amount of people that were left and divide it by the blob's eating rate.
if (population < 0) {
hours = hours + ((population + this.peoplePerHour) / this.peoplePerHour);
return hours;
}
hours ++;
this.peoplePerHour ++;
}
return hours;
};
}
Unsubscribed User
17,284 PointsVery nice, thank you.
Unsubscribed User
17,284 PointsCan look at this? Am I on the right track?
var hello = {
klingon: "nuqneH", // home planet is Qo"noS
romulan: "Jolan\"tru", // home planet is Romulus
"federation standard": "hello" // home planet is Earth
};
// TODO: define a constructor that creates objects to represent
// sentient beings. They have a home planet, a language that they
// speak, and method called sayHello.
function SentientBeing (language, homeplanet) {
this.language = language;
this.homeplanet = homeplanet;
// TODO: specify a home planet and a language
// you'll need to add parameters to this constructor
}
SentientBeing.prototype.sayHello = function(sb){
console.log(hello[this.language]);
// TODO: say hello prints out (console.log's) hello in the
// language of the speaker, but returns it in the language
// of the listener (the sb parameter above).
// use the 'hello' object at the beginning of this exercise
// to do the translating
//TODO: put this on the SentientBeing prototype
}
function human() {
SentientBeing.call(this, 'earth', 'federation standard');
}
Human.prototype = Object.create (SentientBeing.prototype);
Human.prototype.constructor = Human;
var human = new Human;
// TODO: create three subclasses of SentientBeing, one for each
// species above (Klingon, Human, Romulan).
assert((new Human()).sayHello(new Klingon()) === "nuqneH",
"the klingon should hear nuqneH");
// TODO: write five more assertions, to complete all the possible
// greetings between the three types of sentient beings you created above.
/* helper method for assertions */
// We're going to use this special assert method again to
// test our code
function assert(expression, failureMessage) {
if (!expression) {
console.log("assertion failure: ", failureMessage);
}
}
Colin Bell
29,679 PointsYep, just a few semantic things:
- You declared the human function with a lowercase h, but you're creating a new
var humanwith an uppercase H. - You need to add the ending parentheses after creating a new
Human. - The parameters are (language, homeplanet), but you're calling them in this order (homeplanet, language)
// ...
function Human() { // Capitalize here
SentientBeing.call(this, 'federation standard', 'earth'); // Change order of parameters
}
Human.prototype = Object.create (SentientBeing.prototype);
Human.prototype.constructor = Human;
var human = new Human(); // add ending parentheses
// ...