-
-
Notifications
You must be signed in to change notification settings - Fork 278
Manchester| 26-ITP-Jan | fonime Edak | Sprint 2 | Data-groups #1110
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 6 commits
0348ecc
db1fde3
9ea5edc
2dcd8e8
dfa626a
c1aa1de
df93e29
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,15 +1,17 @@ | ||
| // Predict and explain first... | ||
|
|
||
| // The program cant log the ingredient because the object literal was not reference inside the console.log() | ||
| // 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? | ||
|
|
||
| const recipe = { | ||
| title: "bruschetta", | ||
| serves: 2, | ||
| ingredients: ["olive oil", "tomatoes", "salt", "pepper"], | ||
| }; | ||
|
|
||
| console.log(`${recipe.title} serves ${recipe.serves} | ||
| ingredients: | ||
| ${recipe}`); | ||
| console.log(`${recipe.title} serves ${recipe.serves}`); | ||
|
|
||
| for (const part of recipe.ingredients) { | ||
| console.log(part); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,13 @@ | ||
| function contains() {} | ||
| function contains(elements, itemKey) { | ||
| if (!elements) return false; | ||
| if (typeof elements !== "object" || Array.isArray(elements)) { | ||
| return false; | ||
| } | ||
| if (Object.hasOwn(elements, itemKey)) { | ||
| return true; | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| module.exports = contains; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,14 @@ | ||
| function createLookup() { | ||
| // implementation here | ||
| function createLookup(elements) { | ||
| if (!Array.isArray(elements) || !elements.every(Array.isArray)) { | ||
| return "Invalid input"; | ||
| } | ||
| if (elements.length === 0) return "Invalid input"; | ||
| const lookup = {}; | ||
|
|
||
| for (const value of elements) { | ||
| lookup[value[0]] = value[1]; | ||
| } | ||
| return lookup; | ||
| } | ||
|
|
||
| module.exports = createLookup; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,13 +3,18 @@ function parseQueryString(queryString) { | |
| if (queryString.length === 0) { | ||
| return queryParams; | ||
| } | ||
| if (typeof queryString !== "string" || !queryString.includes("=")) | ||
|
||
| return "invalid input"; | ||
| const keyValuePairs = queryString.split("&"); | ||
|
|
||
| for (const pair of keyValuePairs) { | ||
| const [key, value] = pair.split("="); | ||
| queryParams[key] = value; | ||
| } | ||
| const index = pair.indexOf("="); | ||
|
|
||
| if (index !== -1) { | ||
| const key = pair.slice(0, index); | ||
| const value = pair.slice(index + 1); | ||
| queryParams[key] = value; | ||
| } | ||
| } | ||
| return queryParams; | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,20 @@ | ||
| function tally() {} | ||
| function tally(elements) { | ||
| if (!Array.isArray(elements)) { | ||
| throw new Error("Invalid input "); | ||
| } | ||
| if (elements.length === 0) { | ||
| return {}; | ||
| } | ||
| const frequency = Object.create(null); | ||
|
|
||
| for (let i = 0; i < elements.length; i++) { | ||
| const item = elements[i]; | ||
| if (frequency[item] === undefined) { | ||
| frequency[item] = 1; | ||
| } else { | ||
| frequency[item]++; | ||
| } | ||
| } | ||
| return frequency; | ||
cjyuan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| module.exports = tally; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
|
|
||
| const invert=require('./invert') | ||
|
|
||
| test("should swap the keys and values in the object", () => { | ||
| expect(invert({ x: 10, y: 20 })).toEqual({ 10: "x", 20: "y" }); | ||
| }); | ||
|
|
||
| test("should swap the keys and values in the object", () => { | ||
| expect(invert({ a: 1 })).toEqual({ 1: "a" }); | ||
| }); | ||
|
|
||
| test("should swap the keys and values in the object", () => { | ||
| expect(invert({ x: 10, y: 20 })).toEqual({ 10: "x", 20: "y" }); | ||
| }); |
Uh oh!
There was an error while loading. Please reload this page.