Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
function mergeDataStreams(stream1, stream2) {
var results = {};
for (var i=0; i < stream1.length; i++) {
results[stream1[i].id] = stream1[i];
}
for (var key in results) {
var otherData = stream2.find(
function(item) { return item.id === key; });
for (var _key in otherData) {
results[key][_key] = otherData[_key];
}
}
return Object.keys(results).map(function(item) {return results[item]; });
}
// data
var dataSource1 = [
{
id: '0',
firstName: 'Juan',
lastName: 'Doe',
age: 32
},
{
id: '1',
firstName: 'Jane',
lastName: 'Doe',
age: 31
},
{
id: '2',
firstName: 'Janice',
lastName: 'Doe',
age: 30
},
{
id: '3',
firstName: 'Jake',
lastName: 'Doe',
age: 29
},
];
var dataSource2 = [
{
id: '0',
occupation: 'architect',
address: {
street: '123 Main St',
city: 'CityTown',
country: 'USA'
}
},
{
id: '1',
occupation: 'architect',
address: {
street: '234 Main St',
city: 'CityTown',
country: 'USA'
}
},
{
id: '2',
occupation: 'architect',
address: {
street: '345 Main St',
city: 'CityTown',
country: 'USA'
}
},
{
id: '3',
occupation: 'architect',
address: {
street: '456 Main St',
city: 'CityTown',
country: 'USA'
}
},
];
/* From here down, you are not expected to
understand.... for now :)
Nothing to see here!
*/
// tests
function testMergeDataStreams(stream1, stream2) {
var expected = stream1.map(function(item) {
return _.assign(
_.clone(item), stream2.find(function(item2) {return item.id === item2.id;}));
});
var actual = mergeDataStreams(stream1, stream2);
var passing = actual && expected.map(function(item) {
return _.isEqual(
item,
actual.find(function(_item) {return _item.id === item.id; })
);
}).every(function(result) {return result;} );
if (passing) {
console.log('SUCCESS! mergeDataStreams works');
}
else {
console.log('FAILURE! mergeDataStreams not working');
}
}
testMergeDataStreams(dataSource1, dataSource2);
function mostFrequentWord(words) {
var wordFrequencies = {};
for (var i = 0; i <= words.length; i++) {
if (words[i] in wordFrequencies) {
wordFrequencies[words[i]]++;
}
else {
wordFrequencies[words[i]]=1;
}
}
var currentMaxKey = Object.keys(wordFrequencies)[0];
var currentMaxCount = wordFrequencies[currentMaxKey];
for (var word in wordFrequencies) {
if (wordFrequencies[word] > currentMaxCount) {
currentMaxKey = word;
currentMaxCount = wordFrequencies[word];
}
}
return currentMaxKey;
}
/* From here down, you are not expected to
understand.... for now :)
Nothing to see here!
*/
function getTokens(rawString) {
// returns an alphabetically sorted list of words, removing punctuation
// characters
return rawString.toLowerCase().split(/[ ,!.";:-]+/).filter(Boolean).sort();
}
// `getTokens` returns an alphabetically sorted list of words
(function testMostFrequentWord() {
var spaceOddityText = 'Ground Control to Major Tom\n \
Ground Control to Major Tom\n \
Take your protein pills and put your helmet on\n \
Ground Control to Major Tom (ten, nine, eight, seven, six)\n \
Commencing countdown, engines on (five, four, three)\n \
Check ignition and may God\'s love be with you (two, one, liftoff)\n \
\n \
This is Ground Control to Major Tom\n \
You\'ve really made the grade\n \
And the papers want to know whose shirts you wear\n \
Now it\'s time to leave the capsule if you dare\n \
"This is Major Tom to Ground Control\n \
I\'m stepping through the door\n \
And I\'m floating in a most peculiar way\n \
And the stars look very different today\n \
For here\n \
Am I sitting in a tin can\n \
Far above the world\n \
Planet Earth is blue\n \
And there\'s nothing I can do\n \
\n \
Though I\'m past one hundred thousand miles\n \
I\'m feeling very still\n \
And I think my spaceship knows which way to go\n \
Tell my wife I love her very much she knows\n \
Ground Control to Major Tom\n \
Your circuit\'s dead, there\'s something wrong\n \
Can you hear me, Major Tom?\n \
Can you hear me, Major Tom?\n \
Can you hear me, Major Tom?\n \
Can you \"Here am I floating \'round my tin can\n \
Far above the moon\n \
Planet Earth is blue\n \
And there\'s nothing I can do\"';
var expected = 'major';
var actual = mostFrequentWord(getTokens(spaceOddityText));
if (expected === actual) {
console.log('SUCCESS: `mostFrequentWord` is working');
}
else {
console.log('FAILURE: `mostFrequentWord` is not working');
}
})();
function recipeFactory(name, ingredients, steps) {
return {
name: name,
ingredients: ingredients,
steps: steps,
stepsHtml: function() {
return '<ol>' + this.steps.map(
function(step) {return '<li>' + step + '</li>'; }).join('') + '</ol>';
},
ingredientsHtml: function() {
return '<ul>' + this.ingredients.map(
function(ing) {return '<li>' + ing + '</li>'; }).join('') + '</ul>';
}
}
}
// tests
function testRecipeFactory() {
var grilledCheese = recipeFactory(
'grilled cheese',
['2 slices bread', 'butter', '1 slice cheese'],
['Butter bread', 'Put cheese between bread', 'Toast bread on stove']
);
if (grilledCheese) {
// `$` is a shortcut to the jQuery library, which
// you'll learn about in the next unit.
// Here, we're using jQuery to update elements in the HTML
$('.js-recipe-name').text(grilledCheese.name);
$('.js-ingredients').html(grilledCheese.ingredientsHtml());
$('.js-steps').html(grilledCheese.stepsHtml());
}
}
testRecipeFactory()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment