-
-
Notifications
You must be signed in to change notification settings - Fork 212
NW | 25-ITP-Sep | Ahmad Hmedan |Sprint 2 | Sprint 2 exercises #897
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
Changes from 14 commits
6e3bb1f
85d22f2
019e2bd
d532bac
edca834
b815ce5
21ea627
4d5eef2
6e4dabe
7af05ac
a6be38f
6bd3e07
8875e1c
8dfca89
d751e49
5f39667
8d9f673
2e676bb
7e68427
30029f6
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,29 @@ | ||
| function contains() {} | ||
| function isObject(value) { | ||
| return typeof value === "object" && value !== null && !Array.isArray(value); | ||
| } | ||
|
|
||
| function contains(obj, prop) { | ||
| if(!isObject(obj)) throw new Error ("Invalid input"); | ||
|
|
||
| // return Object.keys(obj).includes(prop); | ||
| return Object.hasOwn(obj, prop); | ||
| } | ||
|
|
||
| console.log( contains({ a: 1, b: 2 }, 'toString')); | ||
| module.exports = contains; | ||
|
|
||
|
|
||
| //Does the following function call returns what you expect? | ||
|
|
||
| // contains({a: 1, b: 2}, 'toString'); | ||
| // returns false for inherited properties | ||
|
|
||
|
|
||
| //How to determine if an object's property is its own property or an inherited property in JavaScript? | ||
| // by using hasown method. | ||
|
|
||
| //Does your test correctly check whether the function can detect the given object is an array? | ||
| //Yes, the test correctly checks that the function can detect when the input is an array. | ||
| //it("throws an error when the first parameter is not an object",()=>{ | ||
| // expect(()=> contains([],"a")).toThrow("Invalid input") | ||
| // }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,10 @@ | ||
| function createLookup() { | ||
| // implementation here | ||
| function createLookup(myArray) { | ||
| if (!Array.isArray(myArray)) return "Invalid input"; | ||
| const myObject = {}; | ||
| for (let i = 0; i < myArray.length; i++) { | ||
| myObject[myArray[i][0]] = myArray[i][1]; | ||
| } | ||
|
||
| return myObject; | ||
| } | ||
|
|
||
| module.exports = createLookup; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,12 @@ | ||
| function tally() {} | ||
| function tally(myArray) { | ||
| if (!Array.isArray(myArray)) throw new Error("Invalid input"); | ||
| if (myArray.length === 0) return {}; | ||
| const myObject =Object.create(null); | ||
| for (const item of myArray) { | ||
| myObject[item] = (myObject[item] || 0) + 1; | ||
| } | ||
| return myObject; | ||
| } | ||
|
|
||
| module.exports = tally; | ||
| console.log(tally(["toString", "toString"])); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| const invert = require("./invert.js"); | ||
|
|
||
| test("inverts a two-key object", () => { | ||
| expect(invert({ a: 1, b: 2 })).toEqual({ 1: "a", 2: "b" }); | ||
| }); | ||
|
|
||
| test("inverts a single-key object", () => { | ||
| expect(invert({ a: 1 })).toEqual({ 1: "a" }); | ||
| }); |
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.
Can also consider using
Array.prototype.join().