Fix/remove property references and update tests (#35109)
* fix(challenges): Update challenge text and assertion test * fix(challenges): update assert test regex * fix(challenges): update regex, fix inline comment in code example * fix: corrected regex
This commit is contained in:
parent
e8b7b2eb6f
commit
9341aadb5b
@ -6,12 +6,17 @@ challengeType: 1
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
|
||||
In the previous challenge, <code>bird</code> had a public property <code>name</code>. It is considered public because it can be accessed and changed outside of <code>bird</code>'s definition.
|
||||
|
||||
<blockquote>bird.name = "Duffy";</blockquote>
|
||||
|
||||
Therefore, any part of your code can easily change the name of <code>bird</code> to any value. Think about things like passwords and bank accounts being easily changeable by any part of your codebase. That could cause a lot of issues.
|
||||
The simplest way to make properties private is by creating a variable within the constructor function. This changes the scope of that variable to be within the constructor function versus available globally. This way, the property can only be accessed and changed by methods also within the constructor function.
|
||||
<blockquote>function Bird() {<br> let hatchedEgg = 10; // private property<br><br> this.getHatchedEggCount = function() { // publicly available method that a bird object can use<br> return hatchedEgg;<br> };<br>}<br>let ducky = new Bird();<br>ducky.getHatchedEggCount(); // returns 10</blockquote>
|
||||
Here <code>getHatchedEggCount</code> is a privileged method, because it has access to the private variable <code>hatchedEgg</code>. This is possible because <code>hatchedEgg</code> is declared in the same context as <code>getHatchedEggCount</code>. In JavaScript, a function always has access to the context in which it was created. This is called <code>closure</code>.
|
||||
|
||||
The simplest way to make this public property private is by creating a variable within the constructor function. This changes the scope of that variable to be within the constructor function versus available globally. This way, the variable can only be accessed and changed by methods also within the constructor function.
|
||||
<blockquote>function Bird() {<br> let hatchedEgg = 10; // private variable<br><br> /* publicly available method that a bird object can use */<br> this.getHatchedEggCount = function() { <br> return hatchedEgg;<br> };<br>}<br>let ducky = new Bird();<br>ducky.getHatchedEggCount(); // returns 10</blockquote>
|
||||
Here <code>getHachedEggCount</code> is a privileged method, because it has access to the private variable <code>hatchedEgg</code>. This is possible because <code>hatchedEgg</code> is declared in the same context as <code>getHachedEggCount</code>. In JavaScript, a function always has access to the context in which it was created. This is called <code>closure</code>.
|
||||
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
@ -24,10 +29,12 @@ Change how <code>weight</code> is declared in the <code>Bird</code> function so
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: The <code>weight</code> property should be a private variable.
|
||||
testString: assert(!code.match(/this\.weight/g), 'The <code>weight</code> property should be a private variable.');
|
||||
- text: Your code should create a method in <code>Bird</code> called <code>getWeight</code> that returns the <code>weight</code>.
|
||||
testString: assert((new Bird()).getWeight() === 15, 'Your code should create a method in <code>Bird</code> called <code>getWeight</code> that returns the <code>weight</code>.');
|
||||
- text: The <code>weight</code> property should be a private variable and should be assigned the value of <code>15</code>.
|
||||
testString: assert(code.match(/(var|let|const)\s+weight\s*\=\s*15\;?/g), 'The <code>weight</code> property should be a private variable and should be assigned the value of <code>15</code>.');
|
||||
- text: Your code should create a method in <code>Bird</code> called <code>getWeight</code> that returns the value of the private variable <code>weight</code>.
|
||||
testString: assert((new Bird()).getWeight() === 15, 'Your code should create a method in <code>Bird</code> called <code>getWeight</code> that returns the value of the private variable <code>weight</code>.');
|
||||
- text: Your <code>getWeight</code> function should return the private variable <code>weight</code>.
|
||||
testString: assert(code.match(/((return\s+)|(\(\s*\)\s*\=\>\s*))weight\;?/g), 'Your <code>getWeight</code> function should return the private variable <code>weight</code>.');
|
||||
|
||||
```
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user