Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Carina Taveras #102

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
193 changes: 164 additions & 29 deletions problems/objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
* @returns {number} The number of apples Eve has.
*/

const eveAppleCount = () => {};
const eveAppleCount = (appleCountByName) => {
return appleCountByName["Eve"];
};

/**
* Takes in an object with peoples names as the keys and
Expand All @@ -22,7 +24,9 @@ const eveAppleCount = () => {};
* bracket notation? Try them both.
*/

const appleCount = () => {};
const appleCount = (appleCountByName, name) => {
return appleCountByName[name];
};

/**
* Takes in an object with peoples names as the keys and
Expand All @@ -36,7 +40,10 @@ const appleCount = () => {};
* @returns {Object} The updated object.
*/

const eveAppleSet = () => {};
const eveAppleSet = (appleCountByName, appleCount) => {
appleCountByName.Eve = appleCount;
return appleCountByName;
};

/**
* Takes in an object with peoples names as the keys and
Expand All @@ -50,7 +57,10 @@ const eveAppleSet = () => {};
*
*/

const appleSet = () => {};
const appleSet = (appleCountByName, name, newAppleCount) => {
appleCountByName[name] = newAppleCount;
return appleCountByName;
};

/**
* Takes in an object with peoples names as the keys and
Expand All @@ -61,7 +71,9 @@ const appleSet = () => {};
*
*/

const adamAndEveApples = () => {};
const adamAndEveApples = (appleCountByName) => {
return appleCountByName["Eve"] + appleCountByName["Adam"];
};

/**
* Takes in an object with peoples names as the keys and
Expand All @@ -72,7 +84,13 @@ const adamAndEveApples = () => {};
*
*/

const appleSum = () => {};
const appleSum = (appleCountByName) => {
let sum = 0;
for (const key in appleCountByName) {
sum += appleCountByName[key];
}
return sum;
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice job!


/**
* Takes in an object with peoples names as the keys and
Expand All @@ -83,7 +101,12 @@ const appleSum = () => {};
*
*/

const appleSetToZero = () => {};
const appleSetToZero = (appleCountByName) => {
for (const key in appleCountByName) {
appleCountByName[key] = 0;
}
return appleCountByName;
};

/**
* Takes in an object of countries and their capitals.
Expand All @@ -93,7 +116,9 @@ const appleSetToZero = () => {};
* @returns {string} Capital of Russia
*/

const russiaCapital = () => {};
const russiaCapital = (capitalByCountry) => {
return capitalByCountry["Russia"];
};

/**
* Takes in an object of countries and their capitals.
Expand All @@ -105,7 +130,9 @@ const russiaCapital = () => {};
* @returns {string} Capital of country
*/

const getCapital = () => {};
const getCapital = (capitalByCountry, country) => {
return capitalByCountry[country];
};

/**
* Takes in an object of countries and their capitals
Expand All @@ -116,7 +143,10 @@ const getCapital = () => {};
* @returns {Object} countriesAndCapitals now with Jamaica
*/

const addsJamaica = () => {};
const addsJamaica = (capitalByCountry) => {
capitalByCountry.Jamaica = "Kingston";
return capitalByCountry;
};

/**
* Takes in an object of countries and their capitals
Expand All @@ -130,7 +160,10 @@ const addsJamaica = () => {};
* @returns {Object} countriesAndCapitals
*/

const addsCountry = () => {};
const addsCountry = (capitalByCountry, country, capital) => {
capitalByCountry[country] = capital;
return capitalByCountry;
};

/**
* Takes an array of arrays. First element of inner array is authorName, second element
Expand All @@ -141,7 +174,13 @@ const addsCountry = () => {};
* @returns {Object} {"Mark Twain": 8.9, "Nathaniel Hawthorne": 5.1}
*/

const authorScores = () => {};
const authorScores = (authors) => {
let newObj = {}
for(let el of authors){
newObj[el[0]] = el[1]
}
return newObj
}

/**
* You are given an array of objects.
Expand All @@ -151,15 +190,33 @@ const authorScores = () => {};
* @returns {string} The full name of person with best score.
*/

const bestScore = () => {};
const bestScore = (submissions) => {
let highScore = {score: -Infinity}
for(let key of submissions){
if(key.score > highScore.score){
highScore = key
}
}
return `${highScore.firstName} ${highScore.lastName}`
};

/**
* Returns an object where the keys are numbers 1 through 20,
* and their respective values is key cubed (num * num * num).
* @returns {Object} {1: 1, 2: 8, 3: 27...}
*/

const cubeObj = () => {};
const cubeObj = () => {
let num = 1
let newObj = {}
let i = 1
while(i <= 20){
newObj[i] = num * num * num
num++
i++
}
return newObj
};

/**
* Takes in a string and returns an object with
Expand All @@ -168,7 +225,20 @@ const cubeObj = () => {};
* @returns {Object} Counts of e and a. {a: 2, e: 1}
*/

const countAandE = () => {};
const countAandE = (string) => {
let str = string.toLowerCase()
let count = {
"a" : 0,
"e" : 0
};
for (let i = 0; i < str.length; i++) {
const el = i
if (str[el] === "a" || str[el] === "e") {
count[str[el]] += 1
}
}
return count;
};

/**
* Takes in a string and returns an object with
Expand All @@ -177,7 +247,18 @@ const countAandE = () => {};
* @returns {Object} Counts of all characters: {a: 2, g: 1, o: 2, d:1, " ": 2, s: 1, n:1, k:1, e: 1}
*/

const countOccurance = () => {};
const countOccurance = (str) => {
let count = {};
for (let i = 0; i < str.length; i++) {
const el = str[i].toLocaleLowerCase();
if (count[el] !== undefined) {
count[el] += 1;
} else {
count[el] = 1;
}
}
return count;
};

/**
* Takes in a string and returns an object with
Expand All @@ -187,15 +268,50 @@ const countOccurance = () => {};
* @returns {Object} Counts all characters except spaces {a: 2, g: 1, o: 2, d:1, s: 1, n:a, k:1, e: 1}
*/

const countOccuranceNoSpaces = () => {};
const countOccuranceNoSpaces = (str) => {
let newStr = str.split(" ").join("").toLowerCase()
let count = { }
for(let i = 0; i < newStr.length; i++){
const el = newStr[i]
if(count[el] !== undefined){
count[el] += 1
} else {
count[el] = 1
}
}
return count
};

/**
* Takes in an array and returns the most common element.
* @param {Array} array - array of elements
* @returns {(number|string)} Most common element
*/

const mostCommonElement = () => {};
const mostCommonElement = (array) => {
let obj = {};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Think about splitting this part of your code into a helper function so that each function only does one thing.

for (let i = 0; i < array.length; i++) {
let element = array[i];
if (obj[element]) {
obj[element] += 1;
} else {
obj[element] = 1;
}
}
let mostCommon = -Infinity;
let commonElement;
for (let key in obj) {
if (obj[key] > mostCommon) {
mostCommon = obj[key];
commonElement = key;
}
}
if (parseInt(commonElement)) {
return parseInt(commonElement);
} else {
return commonElement;
}
};

/**
* Takes in an object and an array.
Expand All @@ -215,8 +331,19 @@ const mostCommonElement = () => {};
* @returns {string[]} Elements or their pair values.
*/

const updateList = () => {};

const updateList = (pairs, arr) => {
let newArr = [];
for (let i = 0; i < arr.length; i++) {
let artists = arr[i];

if (pairs[artists]) {
newArr.push(pairs[artists]);
} else {
newArr.push(artists);
}
}
return newArr;
};
/**
* Takes in an object and a key.
* It should delete the key value pair from the object and
Expand All @@ -226,16 +353,24 @@ const updateList = () => {};
* @returns {Object} The Object without the key.
*/

const deleteKey = () => {};

const deleteKey = (obj, key) => {
delete obj[key];
return obj;
};

/**
* Takes in an object and returns the number of
* properties it has.
* @param {Object} obj
* @returns {number} Number of properties.
*/
const propertyCount = () => {};
/**
* Takes in an object and returns the number of
* properties it has.
* @param {Object} obj
* @returns {number} Number of properties.
*/
const propertyCount = (obj) => {
let props = 0;
for (key in obj) {
++props;
}
return props;
};

module.exports = {
eveAppleCount,
Expand Down