LaunchCode's Marketing Team needs your help! To make the website more maintainable, they thought an object called
launchcode that contains important facts and functions they need would be helpful.
This way if they need to make a change to one of the facts, they just have to change the object in one place as opposed to going through pages of code to make the change in every place.
Here is what they need the
launchcode object to contain:
organizationwith a value of
executiveDirectorwith a value of
percentageCoolEmployeeswith a value of
programsOfferedwith a value of
["LC101", "LaunchCode Women+", "CodeCamp"].
launchOutput(). This method will return a string.
Let's use Test-Driven Development to write this code! Rather than complete the code and then test it, TDD flips the process:
Open this repl and note the files:
index.jsholds the object we want to design.
launchcode.spec.jsholds the testing script.
The files are mostly empty. Only a framework has been provided for you.
Let's start our work with the properties we need to add to the object.
launchcode.spec.js, write a test that will check that the value of
"nonprofit". Run your test.
launchcode.jsand add the
"Jeff". Run your test.
percentageCoolEmployees. Run your test.
programsOffered. You should have four
expectstatements within your test. Three of them should check that the array contains the appropriate values and the final one should check that the array is the appropriate size. Before moving on, take a moment either individually or with a classmate, to reflect on why you would need these four
expectstatements. Run your test.
You now have the properties set up for the
launchcode object. Time to move on to creating the
launchOutput() needs to meet the following conditions:
'Rutabagas! That doesn't work.'
To make sure that you meet all of these conditions, you need to take it one test at a time.
launchcode.spec.js, complete the
describe function by adding a
test for condition #1:
When passed a number that is ONLY divisible by 2,
Run the test. It should fail because there is no code inside
launchCodeRocks.js, use an
if statement inside the
function to check if the parameter is evenly divisible by 2, and then return an
output. (Hint: modulus).
Run the test script again to see if your code passes. If not, modify
launchOutput() until it does.
launchCodeRocks.spec.js, add tests for the conditions:
Run the tests. The two new ones should fail, but the first
should still pass. Modify the
it statements as needed if you see a
Add more code inside
launchOutput() to check if the parameter is evenly
divisible by 2, 3, or 5, and then return an output based on the result.
Run the test script again to see if your code passes all three tests. If not,
launchOutput() until it does.
launchCodeRocks.spec.js, add a test for the condition:
When passed a number that is divisible by 2 AND 3,
Run the tests. Only the new one should fail.
launchOutput() until the function passes all four of the tests.
Continue adding ONE test at a time for the remaining conditions. After you add EACH new test, run the script to make sure it FAILS, while the previous tests still pass.
launchOutput() until the function passes the new test and all of the
Presto! By starting with the testing script, you constructed
one segment at a time. The result is complete, valid code that has already
been checked for accuracy.
Now that your function passes all 8 tests, let's change one of the conditions.
For the case where a number is divisible by both 2 and 5, instead of returning
'Launch Rocks!', we want the function to return
Modify the testing and function code to deal with this new condition.