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

JavaScript

Could someone help me with this question? Blob Eats Town

Would love it if someone could help me with this question!! I could use some help with someone coaching me through it.

'use strict';

// LAB 2: SORTING AND CAMPY SCI-FI

// Welcome to Lab 2 =)

// Be sure to read all the comments!

// All of the instructions are inline with the assignment below.
// Look for the word TODO in comments.  Each TODO will have a
// description of what is required.

// To run this file (in the terminal) use: node lab2.js

//*********************************************************
// SETUP
//*********************************************************

// 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);
  }
}

//*********************************************************
// PROBLEM 1: The Blob. 20 points
//*********************************************************

/* 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.

    for the...      | starting rate of | persons consumed |
                    |  consumption     |    that hour     |
--------------------|------------------|------------------|
    first hour      |    1/hour        |        1         |
    second hour     |    2/hour        |        2         |
    third hour      |    3/hour        |        3         |
    fourth hour     |    4/hour        |        4         |

 TODO: First, make a constructor function, called Blob, that makes blobs.
*/


// TODO: Next, create an instance of Blob named blob.


//TODO: Then, use a loop to calculate how long it took the blob to finish
// with Dowington.

while ()


var hoursSpentInDowington; // TODO: assign me the value of the
                           // above calculation (how long it took
                           // the blob to eat Dowington)


// Now, write a method that takes a population for an arbitrary
// town, and the starting consumption rate, and returns the number
// of hours the blob needs to ooze its way through that town.

function hoursToOoze(population, peoplePerHour) {
  // TODO: implement me based on the instructions above.
  // Be sure to then assign me to the Blob's prototype.
}

assert(blob.hoursToOoze(0, 1) === 0, 'no people means no time needed.');
assert(blob.hoursToOoze(1000, 1) === hoursSpentInDowington,
  'hoursSpentInDowington should match hoursToOoze\'s result for 1000');

// TODO: write three more assertions like the two above, testing out
// the hoursToOoze method.

1 Answer

The sum of an arithmetic progression is called an arithmetic series and a formula for it is below:

Where n is the final number and a is the first (in your case referring to the ending and starting consumption rate):

(n + 1 - a) / 2 * (n - a)

You'd then need to loop through each integer as n, starting from a, to see if the result is greater than 1,000, or whatever the target population is.

Hope that helps!

Thanks for the feedback! So this may be going a little different direction but how does this look?

var blob = new Blob();

blob.hours = 0;
blob.population = 1000;
blob.peoplePerHour = 1;

while ( blob.population > 0 ) {
 blob.population = blob.population - blob.peoplePerHour;
 blob.hours ++;
 blob.peoplePerHour++;
}

Ahhh yes. That would be way simpler I guess. :)

Better to let the JS just keep looping to do the math for us than to try and teach it a formula!

Note that you'd want `while ( blob.population >= 0 ), for the cases where it manages to have the eating capacity exactly matching the remaining population (and in case the function is passed a population of zero).