feat: split JS challenge into two (#39044)
* feat: split JS challenge into two * fix/change-to-not-use-untaught-assignments Co-authored-by: moT01 <20648924+moT01@users.noreply.github.com>
This commit is contained in:
@ -20,6 +20,10 @@
|
|||||||
"56533eb9ac21ba0edf2244a8",
|
"56533eb9ac21ba0edf2244a8",
|
||||||
"Storing Values with the Assignment Operator"
|
"Storing Values with the Assignment Operator"
|
||||||
],
|
],
|
||||||
|
[
|
||||||
|
"5ee127a03c3b35dd45426493",
|
||||||
|
"Assigning the Value of One Variable to Another"
|
||||||
|
],
|
||||||
[
|
[
|
||||||
"56533eb9ac21ba0edf2244a9",
|
"56533eb9ac21ba0edf2244a9",
|
||||||
"Initializing Variables with the Assignment Operator"
|
"Initializing Variables with the Assignment Operator"
|
||||||
|
@ -0,0 +1,105 @@
|
|||||||
|
---
|
||||||
|
id: 5ee127a03c3b35dd45426493
|
||||||
|
title: Assigning the Value of One Variable to Another
|
||||||
|
challengeType: 1
|
||||||
|
isHidden: false
|
||||||
|
videoUrl: ''
|
||||||
|
forumTopicId: 418265
|
||||||
|
---
|
||||||
|
|
||||||
|
## Description
|
||||||
|
|
||||||
|
<section id='description'>
|
||||||
|
After a value is assigned to a variable using the <dfn>assignment</dfn> operator, you can assign the value of that variable to another variable using the <dfn>assignment</dfn> operator.
|
||||||
|
|
||||||
|
```js
|
||||||
|
var myVar;
|
||||||
|
myVar = 5;
|
||||||
|
var myNum;
|
||||||
|
myNum = myVar;
|
||||||
|
```
|
||||||
|
|
||||||
|
The above declares a `myVar` variable with no value, then assigns it the value `5`. Next, a variable named `myNum` is declared with no value. Then, the contents of `myVar` (which is `5`) is assigned to the variable `myNum`. Now, `myNum` also has the value of `5`.
|
||||||
|
|
||||||
|
</section>
|
||||||
|
|
||||||
|
## Instructions
|
||||||
|
|
||||||
|
<section id='instructions'>
|
||||||
|
Assign the contents of <code>a</code> to variable <code>b</code>.
|
||||||
|
</section>
|
||||||
|
|
||||||
|
## Tests
|
||||||
|
|
||||||
|
<section id='tests'>
|
||||||
|
|
||||||
|
```yml
|
||||||
|
tests:
|
||||||
|
- text: You should not change code above the specified comment.
|
||||||
|
testString: assert(/var a;/.test(code) && /a = 7;/.test(code) && /var b;/.test(code));
|
||||||
|
- text: <code>b</code> should have a value of 7.
|
||||||
|
testString: assert(typeof b === 'number' && b === 7);
|
||||||
|
- text: <code>a</code> should be assigned to <code>b</code> with <code>=</code>.
|
||||||
|
testString: assert(/b\s*=\s*a\s*/g.test(code));
|
||||||
|
```
|
||||||
|
|
||||||
|
</section>
|
||||||
|
|
||||||
|
## Challenge Seed
|
||||||
|
|
||||||
|
<section id='challengeSeed'>
|
||||||
|
|
||||||
|
<div id='js-seed'>
|
||||||
|
|
||||||
|
```js
|
||||||
|
// Setup
|
||||||
|
var a;
|
||||||
|
a = 7;
|
||||||
|
var b;
|
||||||
|
|
||||||
|
// Only change code below this line
|
||||||
|
```
|
||||||
|
|
||||||
|
</div>
|
||||||
|
|
||||||
|
### Before Test
|
||||||
|
|
||||||
|
<div id='js-setup'>
|
||||||
|
|
||||||
|
```js
|
||||||
|
if (typeof a != 'undefined') {
|
||||||
|
a = undefined;
|
||||||
|
}
|
||||||
|
if (typeof b != 'undefined') {
|
||||||
|
b = undefined;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
</div>
|
||||||
|
|
||||||
|
### After Test
|
||||||
|
|
||||||
|
<div id='js-teardown'>
|
||||||
|
|
||||||
|
```js
|
||||||
|
(function(a, b) {
|
||||||
|
return 'a = ' + a + ', b = ' + b;
|
||||||
|
})(a, b);
|
||||||
|
```
|
||||||
|
|
||||||
|
</div>
|
||||||
|
|
||||||
|
</section>
|
||||||
|
|
||||||
|
## Solution
|
||||||
|
|
||||||
|
<section id='solution'>
|
||||||
|
|
||||||
|
```js
|
||||||
|
var a;
|
||||||
|
a = 7;
|
||||||
|
var b;
|
||||||
|
b = a;
|
||||||
|
```
|
||||||
|
|
||||||
|
</section>
|
@ -9,23 +9,22 @@ forumTopicId: 18310
|
|||||||
|
|
||||||
## Description
|
## Description
|
||||||
<section id='description'>
|
<section id='description'>
|
||||||
In JavaScript, you can store a value in a variable with the <dfn>assignment</dfn> operator.
|
In JavaScript, you can store a value in a variable with the <dfn>assignment</dfn> operator (<code>=</code>).
|
||||||
<code>myVariable = 5;</code>
|
<code>myVariable = 5;</code>
|
||||||
This assigns the <code>Number</code> value <code>5</code> to <code>myVariable</code>.
|
This assigns the <code>Number</code> value <code>5</code> to <code>myVariable</code>.
|
||||||
Assignment always goes from right to left. Everything to the right of the <code>=</code> operator is resolved before the value is assigned to the variable to the left of the operator.
|
If there are any calculations to the right of the <code>=</code> operator, those are performed before the value is assigned to the variable on the left of the operator.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
|
var myVar;
|
||||||
myVar = 5;
|
myVar = 5;
|
||||||
myNum = myVar;
|
|
||||||
```
|
```
|
||||||
|
|
||||||
This assigns <code>5</code> to <code>myVar</code> and then resolves <code>myVar</code> to <code>5</code> again and assigns it to <code>myNum</code>.
|
First, this code creates a variable named <code>myVar</code>. Then, the code assigns <code>5</code> to <code>myVar</code>. Now, if <code>myVar</code> appears again in the code, the program will treat it as if it is <code>5</code>.
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Instructions
|
## Instructions
|
||||||
<section id='instructions'>
|
<section id='instructions'>
|
||||||
Assign the value <code>7</code> to variable <code>a</code>.
|
Assign the value <code>7</code> to variable <code>a</code>.
|
||||||
Assign the contents of <code>a</code> to variable <code>b</code>.
|
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
## Tests
|
## Tests
|
||||||
@ -34,13 +33,9 @@ Assign the contents of <code>a</code> to variable <code>b</code>.
|
|||||||
```yml
|
```yml
|
||||||
tests:
|
tests:
|
||||||
- text: You should not change code above the specified comment.
|
- text: You should not change code above the specified comment.
|
||||||
testString: assert(/var a;/.test(code) && /var b = 2;/.test(code));
|
testString: assert(/var a;/.test(code));
|
||||||
- text: <code>a</code> should have a value of 7.
|
- text: <code>a</code> should have a value of 7.
|
||||||
testString: assert(typeof a === 'number' && a === 7);
|
testString: assert(typeof a === 'number' && a === 7);
|
||||||
- text: <code>b</code> should have a value of 7.
|
|
||||||
testString: assert(typeof b === 'number' && b === 7);
|
|
||||||
- text: <code>a</code> should be assigned to <code>b</code> with <code>=</code>.
|
|
||||||
testString: assert(/b\s*=\s*a\s*;/g.test(code));
|
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -54,7 +49,6 @@ tests:
|
|||||||
```js
|
```js
|
||||||
// Setup
|
// Setup
|
||||||
var a;
|
var a;
|
||||||
var b = 2;
|
|
||||||
|
|
||||||
// Only change code below this line
|
// Only change code below this line
|
||||||
|
|
||||||
@ -69,9 +63,6 @@ var b = 2;
|
|||||||
if (typeof a != 'undefined') {
|
if (typeof a != 'undefined') {
|
||||||
a = undefined;
|
a = undefined;
|
||||||
}
|
}
|
||||||
if (typeof b != 'undefined') {
|
|
||||||
b = undefined;
|
|
||||||
}
|
|
||||||
```
|
```
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
@ -80,7 +71,7 @@ if (typeof b != 'undefined') {
|
|||||||
<div id='js-teardown'>
|
<div id='js-teardown'>
|
||||||
|
|
||||||
```js
|
```js
|
||||||
(function(a,b){return "a = " + a + ", b = " + b;})(a,b);
|
(function(a){return "a = " + a;})(a);
|
||||||
```
|
```
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
@ -93,9 +84,7 @@ if (typeof b != 'undefined') {
|
|||||||
|
|
||||||
```js
|
```js
|
||||||
var a;
|
var a;
|
||||||
var b = 2;
|
|
||||||
a = 7;
|
a = 7;
|
||||||
b = a;
|
|
||||||
```
|
```
|
||||||
|
|
||||||
</section>
|
</section>
|
||||||
|
Reference in New Issue
Block a user