From c3155c4f5b1add1036b5d74edd792c0ca3e24818 Mon Sep 17 00:00:00 2001 From: Aditya Date: Sun, 25 Nov 2018 23:26:33 +0530 Subject: [PATCH] [Guide] Data Structures: Create a Set Class (#34434) * feat: added solution * fix: solution * fix: changed expression to be be more readable * fix: formatting --- .../create-a-set-class.english.md | 13 ++++++++- .../create-a-set-class/index.md | 28 +++++++++++++++++-- 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/curriculum/challenges/english/08-coding-interview-prep/data-structures/create-a-set-class.english.md b/curriculum/challenges/english/08-coding-interview-prep/data-structures/create-a-set-class.english.md index 4a1bf3324d..6651a4e719 100644 --- a/curriculum/challenges/english/08-coding-interview-prep/data-structures/create-a-set-class.english.md +++ b/curriculum/challenges/english/08-coding-interview-prep/data-structures/create-a-set-class.english.md @@ -70,7 +70,18 @@ function Set() { ```js -function Set() {var collection = []; this.has = function(e){return(collection.indexOf(e) !== -1);};this.values = function() {return collection;};this.add = function(element) {if (!this.has(element)) {collection.push(element);return true;} else {return false;}};} +function Set() { + var collection = []; + this.has = function(element) { + return (collection.indexOf(element) !== -1); + }; + this.values = function() { + return collection; + }; + this.add = function(el) { + return this.has(el) ? false : Boolean(collection.push(el)); + } +} ``` diff --git a/guide/english/certifications/coding-interview-prep/data-structures/create-a-set-class/index.md b/guide/english/certifications/coding-interview-prep/data-structures/create-a-set-class/index.md index 64fbd8646d..6d2da73dd2 100644 --- a/guide/english/certifications/coding-interview-prep/data-structures/create-a-set-class/index.md +++ b/guide/english/certifications/coding-interview-prep/data-structures/create-a-set-class/index.md @@ -3,8 +3,30 @@ title: Create a Set Class --- ## Create a Set Class -This is a stub. Help our community expand it. +### Method: +- A Set is an abstract data structure. +- It can store unique value and the collection is unordered. +- In this challenge, we have to implement `.add()` method. This method should only add unique values to `collection`. + - The method should return `true`, if the value is sucessfully added to the collection, otherwise it should return `false`. -This quick style guide will help ensure your pull request gets accepted. +### Solution: +```js +function Set() { + // the var collection will hold our set + var collection = []; + // this method will check for the presence of an element and return true or false + this.has = function(element) { + return (collection.indexOf(element) !== -1); + }; + // this method will return all the values in the set + this.values = function() { + return collection; + }; + this.add = function(el) { + return this.has(el) ? false : Boolean(collection.push(el)); + } +} +``` - +### Resources: +- [Wikipedia](https://en.wikipedia.org/wiki/Set_(abstract_data_type))