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
8 changes: 6 additions & 2 deletions Sprint-2/debug/address.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Predict and explain first...
//Predict and explain first...

// This code should log out the houseNumber from the address object
// but it isn't working...
Expand All @@ -12,4 +12,8 @@ const address = {
postcode: "XYZ 123",
};

console.log(`My house number is ${address[0]}`);
console.log(`My house number is ${address.houseNumber}`);

/* The code constructed well and the code is an object which is it is consist of key and value pair
in line 15 inside the console it is return in array method which is the code is not array so we have to change that
to object call "object.houseNumber" in this way it will work correctly */
8 changes: 6 additions & 2 deletions Sprint-2/debug/author.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ const author = {
alive: true,
};

for (const value of author) {
console.log(value);
for (const property in author) {
console.log(`${property}:${author[property]}`);
}

/*In this code the problem is that the method for...of is used for array
not for object type instead we use for...in this will work through the object
and list the key and for value We use author[property] */
11 changes: 8 additions & 3 deletions Sprint-2/debug/recipe.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ const recipe = {
serves: 2,
ingredients: ["olive oil", "tomatoes", "salt", "pepper"],
};
console.log(`${recipe.title} serves ${recipe.serves}
ingredients:`);
for (const element of recipe.ingredients) {
console.log(element);
}

console.log(`${recipe.title} serves ${recipe.serves}
ingredients:
${recipe}`);
/* In this code will log the recipe.title and recipe.serves but recipe.ingredient is not
represented well so it can't log the array ,to access and to get all the array index i used
for...of loop to log all the ingredient in the array*/
18 changes: 17 additions & 1 deletion Sprint-2/implement/contains.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
function contains() {}
function contains(targetObject, searchTerm) {
if (
typeof targetObject !== "object" ||
Object.keys(targetObject).length === 0 ||
Array.isArray(targetObject)
) {
return false;
}

for (const property in targetObject) {
if (targetObject[property] === searchTerm || property === searchTerm) {
return true;
}
}
return false;
}
console.log(contains(["a", "b", "c"], "d"));

module.exports = contains;
23 changes: 22 additions & 1 deletion Sprint-2/implement/contains.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,37 @@ 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("The function should return undefined when an empty object is passed to it.", () => {
expect(contains({})).toBe(false);
});

// Given an object with properties
// When passed to contains with an existing property name
// Then it should return true
test("The function should return true when the object contains the targeted property", () => {
expect(contains({ town: "leeds" }, "leeds")).toBe(true);
expect(contains({ a: 1, b: 2, c: 3 }, "a")).toBe(true);
expect(contains({ name: "ahmed", sex: "male", age: 29 }, "male")).toBe(true);
expect(contains({ zone: 2, zone: 3, zone: 4 }, "zone")).toBe(true);
});

// Given an object with properties
// When passed to contains with a non-existent property name
// Then it should return false
test("the function should return false when the targeted property don't exist", () => {
expect(contains({ a: 1, b: 2, c: 3 }, "d")).toBe(false);
expect(contains({ year: 1998, month: 10, Date: 16 }, "age")).toBe(false);
expect(contains({ meal: "pizza", drink: "water", table: 3 }, "starter")).toBe(
false
);
});

// Given invalid parameters like an array
// When passed to contains
// Then it should return false or throw an error
test("the function should return false when invalid parameter like an array is passed", () => {
expect(contains([10, 23, 34, 45], 45)).toBe(false);
expect(contains("hello", "hi")).toBe(false);
expect(contains(["green", "yellow", "rad"], "green")).toBe(false);
expect(contains(((30, 40, 50), 30))).toBe(false);
});
9 changes: 8 additions & 1 deletion Sprint-2/implement/lookup.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
function createLookup() {
function createLookup(arrayOfArrays) {
// implementation here
const lookUpObject = {};
for (const innerArray of arrayOfArrays) {
keyPair = innerArray[0];
valuePair = innerArray[1];
lookUpObject[keyPair] = valuePair;
}
return lookUpObject;
}

module.exports = createLookup;
28 changes: 27 additions & 1 deletion Sprint-2/implement/lookup.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,32 @@
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", () => {
expect(
createLookup([
["US", "USD"],
["CA", "CAD"],
])
).toEqual({
US: "USD",
CA: "CAD",
});
expect(
createLookup([
["BE", "EUR"],
["BR", "BRL"],
["FI", "FIN"],
])
).toEqual({ BE: "EUR", BR: "BRL", FI: "FIN" });
expect(
createLookup([
["CV", "CPV"],
["CN", "CHN"],
["CR", "CRI"],
["DE", "DEU"],
["IN", "IND"],
])
).toEqual({ CV: "CPV", CN: "CHN", CR: "CRI", DE: "DEU", IN: "IND" });
});

/*

Expand Down
9 changes: 7 additions & 2 deletions Sprint-2/implement/querystring.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@ function parseQueryString(queryString) {
const keyValuePairs = queryString.split("&");

for (const pair of keyValuePairs) {
const [key, value] = pair.split("=");
const equalityPosition = pair.indexOf("=");
if (equalityPosition == -1) {
continue;
}
const key = pair.slice(0, equalityPosition);
const value = pair.slice(equalityPosition + 1);
queryParams[key] = value;
}

return queryParams;
}

//console.log(parseQueryString(["amir = male"]));
module.exports = parseQueryString;
18 changes: 15 additions & 3 deletions Sprint-2/implement/querystring.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,22 @@
// Below is one test case for an edge case the implementation doesn't handle well.
// Fix the implementation for this test, and try to think of as many other edge cases as possible - write tests and fix those too.

const parseQueryString = require("./querystring.js")
const parseQueryString = require("./querystring.js");

test("parses querystring values containing =", () => {
test("the function should return object when a string parses querystring values containing = is passed", () => {
expect(parseQueryString("equation=x=y+1")).toEqual({
"equation": "x=y+1",
equation: "x=y+1",
});
expect(parseQueryString("equation")).toEqual({});
expect(parseQueryString("name=ahmed&age=29")).toEqual({
name: "ahmed",
age: "29",
});
expect(parseQueryString("q=sa3")).toEqual({ q: "sa3" });
expect(parseQueryString("code=future+codebetter")).toEqual({
code: "future+codebetter",
});
expect(parseQueryString("location=london=x=+12=2")).toEqual({
location: "london=x=+12=2",
});
});
17 changes: 16 additions & 1 deletion Sprint-2/implement/tally.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
function tally() {}
function tally(myArray) {
if (myArray.length === 0) {
return {};
} else if (typeof myArray === "string") {
throw Error;
}
let tallyObject = {};
for (const singleItems of myArray) {
if (tallyObject[singleItems] === undefined) {
tallyObject[singleItems] = 1;
} else {
tallyObject[singleItems] += 1;
}
}
return tallyObject;
}

module.exports = tally;
23 changes: 21 additions & 2 deletions Sprint-2/implement/tally.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,31 @@ 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("The function should return empty object when an empty array is passed to the function", () => {
expect(tally([])).toEqual({});
});

// Given an array with duplicate items
// When passed to tally
// Then it should return counts for each unique item

test("The function should return an object where each key is a unique item from the array, and each index is how many times that item duplicate", () => {
expect(tally(["a", "a", "a", "b", "b", "b", "c", "c", "c"])).toEqual({
a: 3,
b: 3,
c: 3,
});
expect(
tally(["manchester", "manchester", "london", "london", "leeds"])
).toEqual({ manchester: 2, london: 2, leeds: 1 });
expect(tally([1, 1, 2])).toEqual({ 1: 2, 2: 1 });
expect(tally(["a"])).toEqual({ a: 1 });
expect(tally(["a", "a", "a"])).toEqual({ a: 3 });
});
// Given an invalid input like a string
// When passed to tally
// Then it should throw an error
test("The function should throw an error when invalid input is passed", () => {
expect(tally("hello")).toThrow;
expect(tally(134)).toThrow;
expect(tally({ a: 1, b: 3, d: 2 })).toThrow;
});
16 changes: 16 additions & 0 deletions Sprint-2/interpret/invert.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,29 @@ function invert(obj) {
}

// a) What is the current return value when invert is called with { a : 1 }
// the current return value when invert is called with { a:1 } is { key: 1 }

// b) What is the current return value when invert is called with { a: 1, b: 2 }
// the current return value when invert is called with { a: 1, b: 2 } is { key: 2}

// c) What is the target return value when invert is called with {a : 1, b: 2}
// the target return value when invert is called with {a : 1, b: 2} is { '1':'a','2':'b'}

// c) What does Object.entries return? Why is it needed in this program?
// Object.entries is needed because it will gave as the access of the key and value by separating each key value pair.

// d) Explain why the current return value is different from the target output
/*the current value is different from the targeted value because the for loop already give as access for the key value pairs
but when we declare a new object the function where looking the word key in the loop because it was dot notation and taking the
word key and pairing it withe the last loop value. */

// e) Fix the implementation of invert (and write tests to prove it's fixed!)
/* function invert(obj) {
const invertedObj = {};

for (const [key, value] of Object.entries(obj)) {
invertedObj[value] = key;
}

return invertedObj;
} */
Loading