Skip to content

LONDON | MAY 2025 | JESUS DEL MORAL | SPRINT 2 | DATA GROUPS #647

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 5 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 @@ -4,6 +4,10 @@
// but it isn't working...
// Fix anything that isn't working

// When we use a Object and we want to extract any of the elements we don't refers them like a array [0] (position)
// We can use the name of the object and follow the dot and the name of the element, with that we can extract the
// value of the element.

const address = {
houseNumber: 42,
street: "Imaginary Road",
Expand All @@ -12,4 +16,4 @@ const address = {
postcode: "XYZ 123",
};

console.log(`My house number is ${address[0]}`);
console.log(`My house number is ${address.houseNumber}`);
10 changes: 7 additions & 3 deletions Sprint-2/debug/author.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
// This program attempts to log out all the property values in the object.
// But it isn't working. Explain why first and then fix the problem

//To get all the values from a object we don't need go through using for loop
//We can use the function Object.values and in parenthesis the name of the Object

const author = {
firstName: "Zadie",
lastName: "Smith",
Expand All @@ -11,6 +14,7 @@ const author = {
alive: true,
};

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

console.log(Object.values(author));


8 changes: 6 additions & 2 deletions Sprint-2/debug/recipe.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
// Predict and explain first...


// This program should log out the title, how many it serves and the ingredients.
// Each ingredient should be logged on a new line
// How can you fix it?

// To display in a new line each Ingredient I use (.join("\n"))

const recipe = {
title: "bruschetta",
serves: 2,
ingredients: ["olive oil", "tomatoes", "salt", "pepper"],
};

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

10 changes: 9 additions & 1 deletion Sprint-2/implement/contains.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
function contains() {}
function contains(input, containsvalues) {
if (!input || !containsvalues) {
return false;
}

return Object.values(input).includes(containsvalues);

}

module.exports = contains;

73 changes: 64 additions & 9 deletions Sprint-2/implement/contains.test.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
const contains = require("./contains.js");

/*
Implement a function called contains that checks an object contains a
particular property
//Implement a function called contains that checks an object contains a
//particular property

E.g. contains({a: 1, b: 2}, 'a') // returns true
as the object contains a key of 'a'
//E.g. contains({a: 1, b: 2}, 'a') // returns true
//as the object contains a key of 'a'

E.g. contains({a: 1, b: 2}, 'c') // returns false
as the object doesn't contains a key of 'c'
*/
//E.g. contains({a: 1, b: 2}, 'c') // returns false
//as the object doesn't contains a key of 'c'
//*/

// Acceptance criteria:

Expand All @@ -20,16 +19,72 @@ 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.todo("contains on empty object returns false");

test("contains on empty object returns false", function () {

const input = "";
const currentOutput = contains(input);
const targetOutput = false;

expect(currentOutput).toEqual(targetOutput);
});


// Given an object with properties
// When passed to contains with an existing property name
// Then it should return true

test("contains on with an existing property name", function () {

const input = {firstName: "Zaida", lastName: "Smith", occupation: "writer",age: 40,alive: true,};
const currentOutput = contains(input, "Zaida");
const targetOutput = true;

expect(currentOutput).toEqual(targetOutput);

});



// Given an object with properties
// When passed to contains with a non-existent property name
// Then it should return false

test("contains returns false for non-existent property", function () {
const input = {
firstName: "Zaida",
lastName: "Smith",
occupation: "writer",
age: 40,
alive: true,
};

const currentOutput = contains(input, "middleName");
const targetOutput = false;

expect(currentOutput).toEqual(targetOutput);

});

// Given invalid parameters like an array
// When passed to contains
// Then it should return false or throw an error


test("contains returns false for invalid parameter", function () {
const input = {
firstName: "Zaida",
lastName: "Smith",
occupation: "writer",
age: 40,
alive: true,
};

const currentOutput = contains(input, "errortest");
const targetOutput = false;

expect(currentOutput).toEqual(targetOutput);

});

17 changes: 15 additions & 2 deletions Sprint-2/implement/lookup.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
function createLookup() {
// implementation here
function createLookup(array) {
if (!array) {
return false;
}

const result = {};

for (let i = 0; i < array.length; i++) {
const [key, value] = array[i];
result [key] = value;
}

return result;

}

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,5 @@
const createLookup = require("./lookup.js");

test.todo("creates a country currency code lookup for multiple codes");

/*

Expand Down Expand Up @@ -33,3 +32,13 @@ It should return:
'CA': 'CAD'
}
*/


test("contains Array of arrays returns a object", function () {

const input = [['US', 'USD'], ['CA', 'CAD']];
const currentOutput = createLookup(input);
const targetOutput = {'US': 'USD', 'CA': 'CAD'};

expect(currentOutput).toEqual(targetOutput);
});
5 changes: 4 additions & 1 deletion Sprint-2/implement/querystring.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
function parseQueryString(queryString) {
const queryParams = {};

if (queryString.length === 0) {
return queryParams;
}

const keyValuePairs = queryString.split("&");

for (const pair of keyValuePairs) {
const [key, value] = pair.split("=");
const [key, ...rest] = pair.split("=");
const value = rest.join("=");
queryParams[key] = value;
}

Expand Down
25 changes: 25 additions & 0 deletions Sprint-2/implement/querystring.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,28 @@ test("parses querystring values containing =", () => {
"equation": "x=y+1",
});
});


test("parses empty string", () => {
expect(parseQueryString("")).toEqual({});
});

test("parses single key=value", () => {
expect(parseQueryString("name=John")).toEqual({ name: "John" });
});

test("parses multiple key=value pairs", () => {
expect(parseQueryString("name=John&age=30")).toEqual({ name: "John", age: "30" });
});

test("handles missing value", () => {
expect(parseQueryString("name=")).toEqual({ name: "" });
});

test("handles missing key", () => {
expect(parseQueryString("=value")).toEqual({ "": "value" });
});

test("handles duplicate keys (last one wins)", () => {
expect(parseQueryString("name=John&name=Jane")).toEqual({ name: "Jane" });
});
26 changes: 25 additions & 1 deletion Sprint-2/implement/tally.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,27 @@
function tally() {}
// It loops over each item in the array.

// If the item is already in the counts object, it increments the count.

// If not, it initializes it with 1.

function tally(items) {
if (!Array.isArray(items)) {
throw new Error("Input must be an array");
}

const counts = {};

for (const item of items) {
counts[item] = (counts[item] || 0) + 1;
}

return counts;
}



module.exports = tally;




17 changes: 16 additions & 1 deletion Sprint-2/implement/tally.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,31 @@ const tally = require("./tally.js");
// Given a function called tally
// When passed an array of items
// Then it should return an object containing the count for each unique item
test("parse an array, return an object containing the count for each unique item", () => {
expect(tally(['a', 'a', 'a'])).toEqual({ a: 3});
});

// 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("Given an empty array, return an empty object", () => {
expect(tally([])).toEqual({});
});



// Given an array with duplicate items
// When passed to tally
// Then it should return counts for each unique item
test("parse an array, return an object containing the count for each unique item", () => {
expect(tally(['a', 'b', 'b', 'a', 'a', 'c'])).toEqual({ a: 3, b: 2, c:1});
});

// Given an invalid input like a string
// When passed to tally
// Then it should throw an error
test("Given an invalid input like a string, it should throw an error", () => {
expect(() => tally("not an array")).toThrow("Input must be an array");
});


25 changes: 24 additions & 1 deletion Sprint-2/interpret/invert.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,43 @@ function invert(obj) {
const invertedObj = {};

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

return invertedObj;
}

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

// Return { key: 1 }

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

// Return { key: 2 }

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

// Target return {"1": "a", "2": "b"}

// c) What does Object.entries return? Why is it needed in this program?

// It returns an array of key–value pairs from the object. Using Object.entries(obj) gives us both the key and value in each iteration:

// d) Explain why the current return value is different from the target output

// The reason is this line (invertedObj.key = value;), dot notation with a literal property name "key", not the variable key.


// 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;
}
*/
17 changes: 17 additions & 0 deletions Sprint-2/stretch/count-words.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,20 @@

3. Order the results to find out which word is the most common in the input
*/

function countWords(querystring) {
const wordCounts = {};

// Remove punctuation and convert to lowercase for consistency
const cleanedStr = str.replace(/[.,!?]/g, "").toLowerCase();

const words = cleanedStr.split(" ");

for (const word of words) {
if (word === "") continue; // skip empty strings from multiple spaces
wordCounts[word] = (wordCounts[word] || 0) + 1;
}

return wordCounts;

}
Loading