-
-
Notifications
You must be signed in to change notification settings - Fork 195
West Midlands | ITP-Sept-25 | Mustaf Asani | Sprint 2 | Data groups coursework #934
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 all commits
5007c4b
f21c96e
b058970
b63c6de
e17bf30
a1c9c1e
87ccd7f
43ae752
dd2c422
f90d792
c63b70f
b7dddb5
180ac40
1d92cf8
b9111f1
dcb2cbb
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,26 @@ | ||
| // Predict and explain first... | ||
| //it will display the title and number of people it serves on one line then the whole recipe on the next line | ||
|
|
||
| // 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? | ||
| //by using the string literal to ensure multiple lines, then having the title on the first line, the number served on the next line and ingredients on the third line. using the dot notation to access the properties needed. | ||
|
|
||
| const recipe = { | ||
| title: "bruschetta", | ||
| serves: 2, | ||
| ingredients: ["olive oil", "tomatoes", "salt", "pepper"], | ||
| }; | ||
|
|
||
| console.log(`${recipe.title} serves ${recipe.serves} | ||
| ingredients: | ||
| ${recipe}`); | ||
| //changed console to display for any number of ingredients | ||
| console.log(`${recipe.title} serves ${recipe.serves} | ||
| ingredients: ${recipe.ingredients[0]} | ||
| ${recipe.ingredients[1]} | ||
| ${recipe.ingredients[2]} | ||
| ${recipe.ingredients[3]}`); | ||
|
|
||
| console.log(`${recipe.title} serves ${recipe.serves} | ||
| ingredients:`); | ||
| for (let ingredient of recipe.ingredients) { | ||
| console.log(` ${ingredient}`); | ||
| } | ||
cjyuan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,17 @@ | ||
| function contains() {} | ||
| function contains(checkObj, checkProp) { | ||
| if ( | ||
| typeof checkObj === "object" && | ||
| checkObj !== null && | ||
| !Array.isArray(checkObj) | ||
| ) { | ||
| if (Object.hasOwn(checkObj, checkProp)) { | ||
| return true; | ||
| } else { | ||
| return false; | ||
| } | ||
| } else { | ||
| throw new Error("error invalid parameter please provide an object"); | ||
| } | ||
| } | ||
|
|
||
| module.exports = contains; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,20 @@ | ||
| function createLookup() { | ||
| function createLookup(inputArray) { | ||
| // implementation here | ||
| const returnObject = {}; | ||
|
|
||
| if (!Array.isArray(inputArray)) { | ||
| throw new Error("error incorrect parameter passed please provide an array"); | ||
| } | ||
|
|
||
| if (inputArray.length === 0) { | ||
| return returnObject; | ||
| } | ||
|
|
||
| for (let item of inputArray) { | ||
| returnObject[item[0]] = item[1]; | ||
| } | ||
|
|
||
| return returnObject; | ||
| } | ||
|
|
||
| module.exports = createLookup; |
cjyuan marked this conversation as resolved.
Show resolved
Hide resolved
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,37 @@ | ||
| function tally() {} | ||
| function tally(inputArray) { | ||
| if (Array.isArray(inputArray)) { | ||
| if (inputArray.length === 0) { | ||
| return {}; | ||
| } | ||
|
|
||
| let itemCount = 0; | ||
| const tallyObject = {}; | ||
| let n = 0; | ||
|
|
||
| while (inputArray.length > 0) { | ||
| itemCount = 0; | ||
|
Contributor
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.
|
||
| const tempArray = []; | ||
|
Contributor
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. Can your code work without using |
||
| let i = 0; | ||
| let currentArrayItem = inputArray[0]; | ||
|
|
||
| while (i < inputArray.length) { | ||
| if (currentArrayItem === inputArray[i]) { | ||
| itemCount++; | ||
| tempArray.push(inputArray.splice(i, 1)); | ||
| i--; | ||
| } | ||
|
|
||
| if (itemCount > 0) { | ||
|
Contributor
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. Is this check necessary? Under what circumstances will |
||
| tallyObject[currentArrayItem] = itemCount; | ||
| } | ||
|
|
||
| i++; | ||
| } | ||
| } | ||
| return tallyObject; | ||
| } else { | ||
| throw new Error("error invalid input passed, please provide an array"); | ||
| } | ||
| } | ||
|
|
||
| module.exports = tally; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| const invert = require("./invert.js"); | ||
|
|
||
| describe("tests to see if invert function swaps around the keys and values in a given object", () => { | ||
| test("if invert is passed a value which is not an object, it should throw an error", () => { | ||
| expect(() => | ||
| invert([ | ||
| ["x", 10], | ||
| ["y", 20], | ||
| ]) | ||
| ).toThrow("error invalid input entered, expecting an object"); | ||
| }); | ||
|
|
||
| test("if we are passed an empty object we should return an empty object", () => { | ||
| expect(invert({})).toEqual({}); | ||
| }); | ||
|
|
||
| test("given an object with key value pairs, it should swap the keys and values in the object", () => { | ||
| expect(invert({ x: 10, y: 20 })).toEqual({ 10: "x", 20: "y" }); | ||
| expect( | ||
| invert({ city: "Birmingham", population: "345908", boroughs: "20" }) | ||
| ).toEqual({ Birmingham: "city", 345908: "population", 20: "boroughs" }); | ||
| }); | ||
| }); | ||
| test("if invert is passed an object which has an array or object as one of its values, it should throw an error", () => { | ||
| expect(() => invert({ cars: { toyota: 2, bmw: 1, benz: 4 } })).toThrow( | ||
| "error invalid input entered, expecting an object to have only strings as values" | ||
| ); | ||
| }); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.