Skip to content

Instantly share code, notes, and snippets.

@betterkenly
Created April 21, 2017 03:42
Show Gist options
  • Save betterkenly/cfd512ca05f2f9170ba0ee81b18d5ee6 to your computer and use it in GitHub Desktop.
Save betterkenly/cfd512ca05f2f9170ba0ee81b18d5ee6 to your computer and use it in GitHub Desktop.
Kenly Hui/ assessment3 answer;
// Write a function that takes 3 words and returns a single count of all their letters.
function combinedCharacterCount(word1, word2, word3){
var together = word1 + word2 + word3;
return together.length;
}
// Below is a simple assert function and its invocation. Add this to your code and make sure that the test passes.
// Note: This is a slightly different assert than what you are used to. Take a moment to look at its declaration and invocation in order to identify how it's different to what you've seen in the past.
function assert(expectedBehavior, descriptionOfCorrectBehavior) {
if (!expectedBehavior) {
console.log(descriptionOfCorrectBehavior);
} else {
console.log('test passed');
}
}
assert(combinedCharacterCount('I', 'am', 'here') === 7, "should correctly count the total number of characters");
// For this part, let’s pretend you’re working for a venture capital company interested in analyzing some data we have about startup companies. You’ve been given a small subset of this data to begin working with. Take a moment to look over how the data is structured:
var companies = [
{
"name" : "AdventNet",
"category_code" : "enterprise",
"number_of_employees" : 600,
"founded_year" : 1996,
"total_money_raised" : "$0"
},
{
"name" : "TechnologyGuide",
"category_code" : "web",
"number_of_employees" : 10,
"founded_year" : 2001,
"total_money_raised" : "$0"
},
{
"name" : "Wetpaint",
"category_code" : "web",
"number_of_employees" : 47,
"founded_year" : 2005,
"total_money_raised" : "$39.8M"
},
{
"name" : "Zoho",
"category_code" : "software",
"number_of_employees" : 1600,
"founded_year" : 2005,
"total_money_raised" : "$0"
},
{
"name" : "Omnidrive",
"category_code" : "network_hosting",
"number_of_employees" : null,
"founded_year" : 2005,
"total_money_raised" : "$800k"
},
{
"name" : "Digg",
"category_code" : "news",
"number_of_employees" : 60,
"founded_year" : 2004,
"total_money_raised" : "$45M"
},
{
"name" : "Geni",
"category_code" : "web",
"number_of_employees" : 18,
"founded_year" : 2006,
"total_money_raised" : "$16.5M"
},
{
"name" : "StumbleUpon",
"category_code" : "web",
"number_of_employees" : null,
"founded_year" : 2002,
"total_money_raised" : "$18.5M"
}
];
// Given an array of companies, return an array of all the company names founded last century
var collectCompaniesFoundedLastCentury = function (companies) {
return companies.filter(lastCenturyCompany);
};
function lastCenturyCompany(obj){
return obj["founded_year"] < 2000 ;
}
console.log(collectCompaniesFoundedLastCentury(companies));
// Use the assert function provided in Part 2 to write a single meaningful test for collectCompaniesFoundedLastCentury, using companies as the input.
// Note: You will either need to use JSON.stringify(); or the below areArraysEqual function in order to test array/object equality.
function areArraysEqual(array1, array2) {
var areEqual = true;
for (var i = 0; i < array1.length; i++) {
if (array1[i] !== array2[i]) {
areEqual = false;
}
}
return areEqual && array1.length === array2.length;
}
function assert1(expectedBehavior, descriptionOfCorrectBehavior) {
if (!expectedBehavior) {
console.log(descriptionOfCorrectBehavior);
} else {
console.log('test passed');
}
}
var actual = collectCompaniesFoundedLastCentury(companies).map(function(each){
return JSON.stringify(each);
});
var expectedArr = [
{
"name" : "AdventNet",
"category_code" : "enterprise",
"number_of_employees" : 600,
"founded_year" : 1996,
"total_money_raised" : "$0"
}
].map(function(each){
return JSON.stringify(each);
});
assert1(areArraysEqual(actual, expectedArr) === true,"should return the array of last century companies");
// Given an array of companies, return an array of all the company names that raised 20 million dollars or more. Start this problem by writing a single failing assert function before filling out the logic in the function body.
function failingAssert(actual, expected, testName ){
actual = actual.map(function(each){
return JSON.stringify(each);
});
expected = actual.map(function(each){
return JSON.stringify(each);
});
var areEqual = true;
for (var i = 0; i < actual.length; i++) {
if (actual[i] !== expected[i]) {
areEqual = false;
}
}
return areEqual && actual.length === expected.length;
}
var collectTwentyMillionDollarCompanies = function (companies) {
var theList = companies.filter(function(each){
return each["total_money_raised"].match(/["M"]/gi);
});
var theList2 = theList.filter(function(each){
return each["total_money_raised"].substring(1,3) > 20;
})
return theList2.map(function(each){
return each.name;
});
};
console.log(collectTwentyMillionDollarCompanies(companies));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment