Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions 1_PM/madlibs.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ const verb1 = ______

Change thses values to generate a different story
*/

const noun1 = 'David'
const verb1 = 'sing'
const story = `
**replace this with your story using a template string**
${noun1} went to school to ${verb1}.
`

console.log(story)
7 changes: 7 additions & 0 deletions 1_PM/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ const assert = require('assert')
*/

const hasFalsyValue = obj => {
for (var key in obj)
{
if ((typeof obj[key] === 'object' && hasFalsyValue(obj[key])) || !obj[key])
return true;

}
return false;
};

const falsyObj = {
Expand Down
36 changes: 33 additions & 3 deletions 1_PM/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,24 +101,54 @@ dont hard code solutions. */

// Implement the function usersByPet to return a list of user objects filtered by cat or dog.
const usersByPet = pet => {
let users = database['users']
let temp = users.filter((x) => {return x['favPet'] === pet;})
return temp
}
console.log(usersByPet('dog'))
console.log(usersByPet('cat'))

// Implement the function collegeLookup to return the name and color of a user's college.
const collegeLookup = user => {
let users = database['users']
let student = users.filter((x) => {return x['firstName'] === user;})[0]
let college = database['college'].filter((x) => {return x['id'] === student['collegeId'] })[0]
return [college['name'], college['color']]
}
console.log(collegeLookup('Charles'))
console.log(collegeLookup('Daniela'))

// define oppositesAttract as a list of friend objects whose favorite pets are different.
const oppositesAttract = _______
let opp = []
let users = database['users']
let friends = database['friends']
friends.forEach(function (pair)
{
var p1 = users.filter((x) => x['id'] === pair['id1'])[0];
var p2 = users.filter((x) => x['id'] === pair['id2'])[0];
if(p1['favPet'] !== p2['favPet'])
opp.push(pair)
});
const oppositesAttract = opp
console.log(oppositesAttract)

// define local as a list of users who live in the same state as they go to school.
const local = _______
let ans = []
users.forEach(function (x) {
var college = database['college'].filter((c) => {return c['id'] === x['collegeId']})[0]
if(x['state'] === college['state'])
ans.push(x)
})
const local = ans
console.log(local)

// define collegeFriends as a list of friend objects that go to the same college
const collegeFriends = _______
let matches = []
friends.forEach(function (pair) {
var p1 = users.filter((x) => x['id'] === pair['id1'])[0];
var p2 = users.filter((x) => x['id'] === pair['id2'])[0];
if(p1['collegeId'] === p2['collegeId'])
matches.push(pair)
});
const collegeFriends = matches;
console.log(collegeFriends)
9 changes: 7 additions & 2 deletions 1_PM/reduce.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@ const assert = require('assert')
* returns the sum of its values. e.g. [0,4,3,6,9].reduce(sum, 0) === 22
*/

const sum = (acc, cur) => {};
const sum = (acc, cur) => {return acc + cur};

Array.prototype.reduce = function(fun, init) {

let result = init;
for(const x of this)
{
result = fun(result, x);
}
return result;
};

const arr1 = [0, 1, 2, 3, 4];
Expand Down
6 changes: 4 additions & 2 deletions 1_PM/scope.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
/**
* Part 1:
* What will print and why?
* It will print 1, 2, and undefined
* What will change if we delete line 15? Why?
* If we delete line 15, it will print 1, 2, 1 because the value of a is defined globally in line 25
*
* Part 2:
* Change to ES6 syntax (use arrow functions and change var to let).
Expand All @@ -12,7 +14,7 @@
*/

function x() {
var a;
var a = 2;
console.log(a);
}

Expand All @@ -22,6 +24,6 @@ function y() {
x();
}

var a = 1;
let a = 1;
console.log(a);
y();
Loading