2018-10-10 22:03:03 +00:00
|
|
|
|
---
|
|
|
|
|
id: 8d1323c8c441eddfaeb5bdef
|
2020-12-16 07:37:30 +00:00
|
|
|
|
title: 创建一个Set类
|
2018-10-10 22:03:03 +00:00
|
|
|
|
challengeType: 1
|
|
|
|
|
videoUrl: ''
|
2021-01-13 02:31:00 +00:00
|
|
|
|
dashedName: create-a-set-class
|
2018-10-10 22:03:03 +00:00
|
|
|
|
---
|
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
# --description--
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
在接下来的几个练习中,我们将创建一个函数来模拟一个名为“Set”的数据结构。 Set类似于数组,但不能包含重复值。 Set的典型用途是简单地检查项目是否存在。这可以用对象实现,例如:
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
> var set = new Object();
|
|
|
|
|
> set.foo = true;
|
|
|
|
|
> //看看我们的集合中是否存在foo:
|
|
|
|
|
> console.log(set.foo)// true
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
在接下来的几个练习中,我们将从头开始构建一个全功能的Set。对于本练习,只要该值中尚不存在该值,就创建一个将值添加到set集合的函数。例如:
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
> this.add = function(element){
|
|
|
|
|
> //一些代码来为集合添加值
|
|
|
|
|
> }
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
如果成功添加该值,则该函数应返回`true`否则返回`false` 。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
# --hints--
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
您的`Set`类应该有一个`add`方法。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
|
|
|
|
```js
|
2020-12-16 07:37:30 +00:00
|
|
|
|
assert(
|
|
|
|
|
(function () {
|
|
|
|
|
var test = new Set();
|
|
|
|
|
return typeof test.add === 'function';
|
|
|
|
|
})(),
|
|
|
|
|
'Your <code>Set</code> class should have an <code>add</code> method.'
|
|
|
|
|
);
|
2018-10-10 22:03:03 +00:00
|
|
|
|
```
|
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
您的`add`方法不应添加重复值。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
```js
|
|
|
|
|
assert(
|
|
|
|
|
(function () {
|
|
|
|
|
var test = new Set();
|
|
|
|
|
test.add('a');
|
|
|
|
|
test.add('b');
|
|
|
|
|
test.add('a');
|
|
|
|
|
var vals = test.values();
|
|
|
|
|
return vals[0] === 'a' && vals[1] === 'b' && vals.length === 2;
|
|
|
|
|
})(),
|
|
|
|
|
'Your <code>add</code> method should not add duplicate values.'
|
|
|
|
|
);
|
|
|
|
|
```
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
成功添加值后, `add`方法应返回`true` 。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
```js
|
|
|
|
|
assert(
|
|
|
|
|
(function () {
|
|
|
|
|
var test = new Set();
|
|
|
|
|
var result = test.add('a');
|
|
|
|
|
return result != undefined && result === true;
|
|
|
|
|
})(),
|
|
|
|
|
'Your <code>add</code> method should return <code>true</code> when a value has been successfully added.'
|
|
|
|
|
);
|
|
|
|
|
```
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
`add`重复值时, `add`方法应返回`false` 。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
|
|
|
|
```js
|
2020-12-16 07:37:30 +00:00
|
|
|
|
assert(
|
|
|
|
|
(function () {
|
|
|
|
|
var test = new Set();
|
|
|
|
|
test.add('a');
|
|
|
|
|
var result = test.add('a');
|
|
|
|
|
return result != undefined && result === false;
|
|
|
|
|
})(),
|
|
|
|
|
'Your <code>add</code> method should return <code>false</code> when a duplicate value is added.'
|
|
|
|
|
);
|
2018-10-10 22:03:03 +00:00
|
|
|
|
```
|
2020-08-13 15:24:35 +00:00
|
|
|
|
|
2021-01-13 02:31:00 +00:00
|
|
|
|
# --seed--
|
|
|
|
|
|
|
|
|
|
## --seed-contents--
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
class Set {
|
|
|
|
|
constructor() {
|
|
|
|
|
// Dictionary will hold the items of our set
|
|
|
|
|
this.dictionary = {};
|
|
|
|
|
this.length = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// This method will check for the presence of an element and return true or false
|
|
|
|
|
has(element) {
|
|
|
|
|
return this.dictionary[element] !== undefined;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// This method will return all the values in the set
|
|
|
|
|
values() {
|
|
|
|
|
return Object.values(this.dictionary);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Only change code below this line
|
|
|
|
|
|
|
|
|
|
// Only change code above this line
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
# --solutions--
|
|
|
|
|
|
2021-01-13 02:31:00 +00:00
|
|
|
|
```js
|
|
|
|
|
class Set {
|
|
|
|
|
constructor() {
|
|
|
|
|
this.dictionary = {};
|
|
|
|
|
this.length = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
has(element) {
|
|
|
|
|
return this.dictionary[element] !== undefined;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
values() {
|
|
|
|
|
return Object.values(this.dictionary);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
add(element) {
|
|
|
|
|
if (!this.has(element)) {
|
|
|
|
|
this.dictionary[element] = element;
|
|
|
|
|
this.length++;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
remove(element) {
|
|
|
|
|
if (this.has(element)) {
|
|
|
|
|
delete this.dictionary[element];
|
|
|
|
|
this.length--;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size() {
|
|
|
|
|
return this.length;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|