Skip to content
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

Another solution for this exercise using Object #78

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
12 changes: 12 additions & 0 deletions JavaScript/chapter01/p01_is_unique/fvntr.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,15 @@ describe(module.filename, () => {
assert.deepEqual(isUnique([1,1,1,2,2,2,2,3,3,3,3]), [1,2,3]);
});
});

//Solution using Object
const isUnique2=(arr)=>{
let obj = {}
for (let elem of arr){
if(obj.hasOwnProperty(elem)) obj[elem]++
else obj[elem] = 1
}

return Object.keys(obj)
}
//isUnique2([1,1,1,2,2,2,2,3,3,3,3]) //returns[1,2,3]
Comment on lines +17 to +28
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you create a new file (called Ada-11.js)? that way there's no way that multiple solutions conflict with each other.

also, it is considered a best practice (and thus strongly encouraged) to use unit tests to validate solutions: that way you can clearly see that a particular solution is passing (or failing) the tests quickly by executing it. We also have set up CI to automatically run that every time!

you can see how to do that in L10-16 in this file or

describe(module.filename, () => {
it("should return true on an input string with unique characters", () => {
assert.equal(isUnique1("tech"), true);
assert.equal(isUnique2("tech"), true);
});
it("should return false on an input string with non-unique characters", () => {
assert.equal(isUnique1("techqueria"), false);
assert.equal(isUnique2("techqueria"), false);
});
});