chore(i8n,learn): processed translations
This commit is contained in:
committed by
Mrugesh Mohapatra
parent
15047f2d90
commit
e5c44a3ae5
@ -1,43 +1,47 @@
|
||||
---
|
||||
id: 8d1323c8c441eddfaeb5bdef
|
||||
title: 创建一个Set类
|
||||
title: Create a Set Class
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
forumTopicId: 301632
|
||||
dashedName: create-a-set-class
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
在接下来的几个练习中,我们将创建一个函数来模拟一个名为“Set”的数据结构。 Set类似于数组,但不能包含重复值。 Set的典型用途是简单地检查项目是否存在。这可以用对象实现,例如:
|
||||
In this exercise we are going to create a class named `Set` to emulate an abstract data structure called "set". A set is like an array, but it cannot contain duplicate values. The typical use for a set is to simply check for the presence of an item. We can see how the ES6 `Set` object works in the example below:
|
||||
|
||||
> var set = new Object();
|
||||
> set.foo = true;
|
||||
> //看看我们的集合中是否存在foo:
|
||||
> console.log(set.foo)// true
|
||||
```js
|
||||
const set1 = new Set([1, 2, 3, 5, 5, 2, 0]);
|
||||
console.log(set1);
|
||||
// output: {1, 2, 3, 5, 0}
|
||||
console.log(set1.has(1));
|
||||
// output: true
|
||||
console.log(set1.has(6));
|
||||
// output: false
|
||||
```
|
||||
|
||||
在接下来的几个练习中,我们将从头开始构建一个全功能的Set。对于本练习,只要该值中尚不存在该值,就创建一个将值添加到set集合的函数。例如:
|
||||
First, we will create an add method that adds a value to our set collection as long as the value does not already exist in the set. Then we will create a remove method that removes a value from the set collection if it already exists. And finally, we will create a size method that returns the number of elements inside the set collection.
|
||||
|
||||
> this.add = function(element){
|
||||
> //一些代码来为集合添加值
|
||||
> }
|
||||
# --instructions--
|
||||
|
||||
如果成功添加该值,则该函数应返回`true`否则返回`false` 。
|
||||
Create an `add` method that adds a unique value to the set collection and returns `true` if the value was successfully added and `false` otherwise.
|
||||
|
||||
Create a `remove` method that accepts a value and checks if it exists in the set. If it does, then this method should remove it from the set collection, and return `true`. Otherwise, it should return `false`. Create a `size` method that returns the size of the set collection.
|
||||
|
||||
# --hints--
|
||||
|
||||
您的`Set`类应该有一个`add`方法。
|
||||
Your `Set` class should have an `add` method.
|
||||
|
||||
```js
|
||||
assert(
|
||||
(function () {
|
||||
var test = new Set();
|
||||
return typeof test.add === 'function';
|
||||
})(),
|
||||
'Your <code>Set</code> class should have an <code>add</code> method.'
|
||||
})()
|
||||
);
|
||||
```
|
||||
|
||||
您的`add`方法不应添加重复值。
|
||||
Your `add` method should not add duplicate values.
|
||||
|
||||
```js
|
||||
assert(
|
||||
@ -48,12 +52,11 @@ assert(
|
||||
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.'
|
||||
})()
|
||||
);
|
||||
```
|
||||
|
||||
成功添加值后, `add`方法应返回`true` 。
|
||||
Your `add` method should return `true` when a value has been successfully added.
|
||||
|
||||
```js
|
||||
assert(
|
||||
@ -61,12 +64,11 @@ assert(
|
||||
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.'
|
||||
})()
|
||||
);
|
||||
```
|
||||
|
||||
`add`重复值时, `add`方法应返回`false` 。
|
||||
Your `add` method should return `false` when a duplicate value is added.
|
||||
|
||||
```js
|
||||
assert(
|
||||
@ -75,8 +77,73 @@ assert(
|
||||
test.add('a');
|
||||
var result = test.add('a');
|
||||
return result != undefined && result === false;
|
||||
})()
|
||||
);
|
||||
```
|
||||
|
||||
Your `Set` class should have a `remove` method.
|
||||
|
||||
```js
|
||||
assert(
|
||||
(function () {
|
||||
var test = new Set();
|
||||
return typeof test.remove === 'function';
|
||||
})()
|
||||
);
|
||||
```
|
||||
|
||||
Your `remove` method should only remove items that are present in the set.
|
||||
|
||||
```js
|
||||
assert.deepEqual(
|
||||
(function () {
|
||||
var test = new Set();
|
||||
test.add('a');
|
||||
test.add('b');
|
||||
test.remove('c');
|
||||
return test.values();
|
||||
})(),
|
||||
'Your <code>add</code> method should return <code>false</code> when a duplicate value is added.'
|
||||
['a', 'b']
|
||||
);
|
||||
```
|
||||
|
||||
Your `remove` method should remove the given item from the set.
|
||||
|
||||
```js
|
||||
assert(
|
||||
(function () {
|
||||
var test = new Set();
|
||||
test.add('a');
|
||||
test.add('b');
|
||||
test.remove('a');
|
||||
var vals = test.values();
|
||||
return vals[0] === 'b' && vals.length === 1;
|
||||
})()
|
||||
);
|
||||
```
|
||||
|
||||
Your `Set` class should have a `size` method.
|
||||
|
||||
```js
|
||||
assert(
|
||||
(function () {
|
||||
var test = new Set();
|
||||
return typeof test.size === 'function';
|
||||
})()
|
||||
);
|
||||
```
|
||||
|
||||
The `size` method should return the number of elements in the collection.
|
||||
|
||||
```js
|
||||
assert(
|
||||
(function () {
|
||||
var test = new Set();
|
||||
test.add('a');
|
||||
test.add('b');
|
||||
test.remove('a');
|
||||
return test.size() === 1;
|
||||
})()
|
||||
);
|
||||
```
|
||||
|
||||
@ -103,7 +170,7 @@ class Set {
|
||||
}
|
||||
|
||||
// Only change code below this line
|
||||
|
||||
|
||||
// Only change code above this line
|
||||
}
|
||||
```
|
||||
|
Reference in New Issue
Block a user