-
-
Notifications
You must be signed in to change notification settings - Fork 147
WESTMIDLANDS|MAY-2025|SARA TAHIR|Sprint 2/coursework #565
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
base: main
Are you sure you want to change the base?
Changes from 10 commits
7feb9f9
efe91a5
47ece2c
d9248ca
d169bf8
f105fb6
f91ae9b
0003a16
c81574a
47e39e6
5c4a4e8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,16 @@ | ||
function contains() {} | ||
function contains(object, propertyName) { | ||
// Check if the input is a real object (not null or an array) | ||
const isObject = | ||
typeof object === "object" && object !== null && !Array.isArray(object); | ||
|
||
// If it's not a valid object or property name is missing, return false | ||
if (!isObject || typeof propertyName !== "string") { | ||
return false; | ||
} | ||
|
||
// Return true if the object has the property directly (not inherited) | ||
return object.hasOwnProperty(propertyName); | ||
} | ||
|
||
|
||
module.exports = contains; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,25 @@ | ||
function createLookup() { | ||
// implementation here | ||
function createLookup(pairs) { | ||
const lookup = {}; | ||
for (let i = 0; i < pairs.length; i++) { | ||
const [country, currency] = pairs[i]; | ||
lookup[country] = currency; | ||
} | ||
return lookup; | ||
} | ||
|
||
module.exports = createLookup; | ||
|
||
/*const pair = [['US','USD'],['CA','CAD']] | ||
const firstpair = pair[0] | ||
let country1 = firstpair[0] | ||
let currency1 = firstpair[1] | ||
console.log("First country:", country1); // US | ||
console.log("First currency:", currency1); | ||
|
||
const secondPair= pair[1] | ||
let country2 = secondPair[0] | ||
let currency2 = secondPair[1] | ||
console.log("Second country:", country2); // CA | ||
console.log("Second currency:", currency2); | ||
} | ||
*/ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,49 @@ | ||
|
||
function parseQueryString(queryString) { | ||
const queryParams = {}; | ||
|
||
|
||
if (queryString.startsWith("?")) { // Remove "?" if present at start | ||
queryString = queryString.substring(1); | ||
} | ||
|
||
if (queryString.length === 0) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Given you are checking for empty strings in the later for loop, do you need to also check for an empty string here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Made the change. You are right, The code would have worked just fine as I am checking again for empty strings later. |
||
return queryParams; | ||
} | ||
|
||
const keyValuePairs = queryString.split("&"); | ||
|
||
for (const pair of keyValuePairs) { | ||
const [key, value] = pair.split("="); | ||
queryParams[key] = value; | ||
if (!pair) continue; // Skip empty strings from && or trailing &, | ||
|
||
const idx = pair.indexOf("="); | ||
|
||
let rawKey, rawValue; | ||
|
||
if (idx === -1) { | ||
rawKey = pair; | ||
rawValue = ""; | ||
} else { | ||
rawKey = pair.slice(0, idx);// starts at zero, stop just before the index of = | ||
rawValue = pair.slice(idx + 1); | ||
} | ||
|
||
// Decode without replacing '+' with space | ||
const key = decodeURIComponent(rawKey); | ||
const value = decodeURIComponent(rawValue); | ||
|
||
if (queryParams.hasOwnProperty(key)) { | ||
if (Array.isArray(queryParams[key])) { | ||
queryParams[key].push(value); | ||
} else { | ||
queryParams[key] = [queryParams[key], value]; | ||
} | ||
} else { | ||
queryParams[key] = value; | ||
} | ||
} | ||
|
||
return queryParams; | ||
} | ||
|
||
module.exports = parseQueryString; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,21 @@ | ||
function tally() {} | ||
function tally(items) { | ||
if (!Array.isArray(items)) { | ||
throw new Error("Input should be an array"); | ||
} | ||
|
||
const result = {}; | ||
|
||
for (const item of items) { | ||
if (result[item]) { | ||
result[item]++; | ||
} else { | ||
result[item] = 1; | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
module.exports = tally; | ||
//takes and array of items | ||
//item are keys | ||
// how many times that item is repeated in the array- count of that item(key) is the value of each key |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
const invert = require("./invert"); | ||
|
||
test("given an object of key-value, it should return inverted value-key", () => { | ||
expect(invert({ a: 1, b: 2 })).toEqual({ "1": "a", "2": "b" }); | ||
}); | ||
|
||
test("given an object of single key-value, it should return single value-key", () => { | ||
expect(invert({ a: 1 })).toEqual({ "1": "a" }); | ||
}); | ||
|
||
test("given an empty input it should return an empty object", () => { | ||
expect(invert({})).toEqual({}); | ||
}); | ||
|
||
test("invert values that are strings", () => { | ||
expect(invert({ a: "1", b: "pink" })).toEqual({ "1": "a", "pink": "b" }); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn't a major problem here, but remember in a more complex git project not to commit lots of comments of unused code like this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Noted, I will make sure I dont make it a practice.