Skip to content

Westmidlands/Millena-Mesfin/Module Data Groups Sprint#2/Week 10 #485

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
6 changes: 5 additions & 1 deletion Sprint-2/debug/address.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,8 @@ const address = {
postcode: "XYZ 123",
};

console.log(`My house number is ${address[0]}`);
//console.log(`My house number is ${address[0]}`);
// Objects in javascript are key,value types that is why the code is not working,
// We are trying to access house number using the key "0"

console.log(`My house number is ${address.houseNumber}`);
8 changes: 5 additions & 3 deletions Sprint-2/debug/author.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ const author = {
alive: true,
};

for (const value of author) {
console.log(value);
}

for (const key in author) {
console.log(key);
}

Comment on lines +14 to +18
Copy link

Choose a reason for hiding this comment

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

Can you modify this to output the property values instead of property names?

16 changes: 13 additions & 3 deletions Sprint-2/debug/recipe.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,19 @@
const recipe = {
title: "bruschetta",
serves: 2,
ingredients: ["olive oil", "tomatoes", "salt", "pepper"],
ingredients: ["olive oil", "tomatoes", "salt", "pepper"]
};

console.log(`${recipe.title} serves ${recipe.serves}
ingredients:
${recipe}`);
// ingredients:
// ${recipe.ingredients.join("\n")}`);
//

//OR
//recipe.ingredients.forEach(ingredient => console.log(ingredient));


//.join ==> converts array in to a string.

//("\n") ==> this is called new line character which is used to make sure the ingredients are
// listed in a new line instead of separated by commas.
4 changes: 3 additions & 1 deletion Sprint-2/implement/contains.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
function contains() {}
function contains(obj,key) {
return (key in obj);
}
Comment on lines +1 to +3
Copy link

Choose a reason for hiding this comment

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

Consider the following two approaches for determining if an object contains a property:

  let obj = {}, propertyName = "toString";
  console.log( propertyName in obj );                // true
  console.log( Object.hasOwn(obj, propertyName) );   // false

Which of these approaches suits your needs better?
For more info, you can look up JS "in" operator vs Object.hasOwn.

Copy link
Author

Choose a reason for hiding this comment

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

Thank you cj, I got to differentiate the (propertyName in obj) suits to my function.


module.exports = contains;
21 changes: 20 additions & 1 deletion Sprint-2/implement/contains.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,35 @@ as the object doesn't contains a key of 'c'
// Given an empty object
// When passed to contains
// Then it should return false
test.todo("contains on empty object returns false");
test("contains an empty object returns false", () => {
const obj = {};
expect(contains(obj,"m")).toBe(false);
});

// Given an object with properties
// When passed to contains with an existing property name
// Then it should return true
test("contains an object with an existing property should return true", () => {
const obj = {name:"Milli",surname:"Mesfin",city:"Birmingham"};
expect(contains(obj,'surname')).toBe(true);
expect(contains(obj,'city')).toBe(true);
});

// Given an object with properties
// When passed to contains with a non-existent property name
// Then it should return false
test("contains an object with non-existent property name should return false", () => {

const obj = {name:"Milli",surname:"Mesfin", city:"Birmingham"}
expect(contains(obj,'age')).toBe(false);
expect(contains(obj,'address')).toBe(false);
});

// Given invalid parameters like an array
// When passed to contains
// Then it should return false or throw an error
test("contains an invalid parameter like array should return false" ,() => {
const obj = ["code","your","future"];
expect(contains(obj,'age')).toBe(false);
expect(contains(obj,'address')).toBe(false);
});
Comment on lines +50 to +54
Copy link

Choose a reason for hiding this comment

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

An array is a kind of object in JS.
The indexes serve as the keys (or property names) in an array. So contains(["code","your","future"], '0') would return true.
If the function needs to return false when the parameter is an array, the function will need to check if the parameter is an array or not.

9 changes: 6 additions & 3 deletions Sprint-2/implement/lookup.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
function createLookup() {
// implementation here
}
function createLookup(countryCurrencyPairs) {

const obj = Object.fromEntries(countryCurrencyPairs);

return obj ;
};

module.exports = createLookup;
11 changes: 10 additions & 1 deletion Sprint-2/implement/lookup.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
const createLookup = require("./lookup.js");

test.todo("creates a country currency code lookup for multiple codes");
test("creates a country currency code lookup for multiple codes", () => {
const countryCurrencyPairs = [['US','USD'], ['CA', 'CAD']]
expect(createLookup(countryCurrencyPairs)).toEqual({

'US':'USD',
'CA':'CAD'

});

});

/*

Expand Down
15 changes: 14 additions & 1 deletion Sprint-2/implement/tally.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
function tally() {}
function tally(arr) {
if (!Array.isArray(arr)) {
//this line checks if arr is not an array, if it's not an array it will throw error.
throw new Error("not an array");
}

const count = {};
for(const item of arr){
count[item]=(count[item]||0)+1; // this line is where we update the count of each item in the count object.
//count[item]=> is checking if already exists as a key in the count object.
// ||0 => this changes the undefined count to zero if the item have not seen in the count object and then we add 1 .
}
return count;
}

module.exports = tally;
19 changes: 18 additions & 1 deletion Sprint-2/implement/tally.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,29 @@ const tally = require("./tally.js");
// Given an empty array
// When passed to tally
// Then it should return an empty object
test.todo("tally on an empty array returns an empty object");
test("tally on an empty array returns an empty object", () => {
const count = [];
expect(tally([])).toEqual({})
});

// Given an array with duplicate items
// When passed to tally
// Then it should return counts for each unique item
test("An array with duplicate items should return counts for each unique item", () => {
const count = ["h", "e", "l", "l", "o",];
const expectedOutput = {h: 1, e: 1, l: 2, o: 1 };

expect(tally(count)).toEqual(expectedOutput);
});

// Given an invalid input like a string
// When passed to tally
// Then it should throw an error
test("An array with invalid input should throw an error", () => {
const count = ["hello", "hey", "hey", "hello", "hello", "hi"];
const expectedOutput = {hello: 3, hey: 2, hi: 1 };

expect(tally(count)).toEqual(expectedOutput);
});
Comment on lines +44 to +49
Copy link

Choose a reason for hiding this comment

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

The description and the code does not match.