Feat: add new Markdown parser (#39800)
and change all the challenges to new `md` format.
This commit is contained in:
committed by
GitHub
parent
a07f84c8ec
commit
0bd52f8bd1
@@ -6,12 +6,15 @@ videoUrl: 'https://scrimba.com/c/cBZQbTz'
|
||||
forumTopicId: 16158
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
# --description--
|
||||
|
||||
We can access the data inside arrays using <dfn>indexes</dfn>.
|
||||
Array indexes are written in the same bracket notation that strings use, except that instead of specifying a character, they are specifying an entry in the array. Like strings, arrays use <dfn>zero-based</dfn> indexing, so the first element in an array has an index of <code>0</code>.
|
||||
<br />
|
||||
<strong>Example</strong>
|
||||
|
||||
Array indexes are written in the same bracket notation that strings use, except that instead of specifying a character, they are specifying an entry in the array. Like strings, arrays use <dfn>zero-based</dfn> indexing, so the first element in an array has an index of `0`.
|
||||
|
||||
<br>
|
||||
|
||||
**Example**
|
||||
|
||||
```js
|
||||
var array = [50,60,70];
|
||||
@@ -19,62 +22,67 @@ array[0]; // equals 50
|
||||
var data = array[1]; // equals 60
|
||||
```
|
||||
|
||||
<strong>Note</strong><br>There shouldn't be any spaces between the array name and the square brackets, like <code>array [0]</code>. Although JavaScript is able to process this correctly, this may confuse other programmers reading your code.
|
||||
</section>
|
||||
**Note**
|
||||
There shouldn't be any spaces between the array name and the square brackets, like `array [0]`. Although JavaScript is able to process this correctly, this may confuse other programmers reading your code.
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Create a variable called <code>myData</code> and set it to equal the first value of <code>myArray</code> using bracket notation.
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
Create a variable called `myData` and set it to equal the first value of `myArray` using bracket notation.
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: The variable <code>myData</code> should equal the first value of <code>myArray</code>.
|
||||
testString: assert((function(){if(typeof myArray !== 'undefined' && typeof myData !== 'undefined' && myArray[0] === myData){return true;}else{return false;}})());
|
||||
- text: The data in variable <code>myArray</code> should be accessed using bracket notation.
|
||||
testString: assert((function(){if(code.match(/\s*=\s*myArray\[0\]/g)){return true;}else{return false;}})());
|
||||
# --hints--
|
||||
|
||||
The variable `myData` should equal the first value of `myArray`.
|
||||
|
||||
```js
|
||||
assert(
|
||||
(function () {
|
||||
if (
|
||||
typeof myArray !== 'undefined' &&
|
||||
typeof myData !== 'undefined' &&
|
||||
myArray[0] === myData
|
||||
) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
})()
|
||||
);
|
||||
```
|
||||
|
||||
</section>
|
||||
The data in variable `myArray` should be accessed using bracket notation.
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
```js
|
||||
assert(
|
||||
(function () {
|
||||
if (code.match(/\s*=\s*myArray\[0\]/g)) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
})()
|
||||
);
|
||||
```
|
||||
|
||||
<div id='js-seed'>
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
if(typeof myArray !== "undefined" && typeof myData !== "undefined"){(function(y,z){return 'myArray = ' + JSON.stringify(y) + ', myData = ' + JSON.stringify(z);})(myArray, myData);}
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
// Setup
|
||||
var myArray = [50,60,70];
|
||||
|
||||
// Only change code below this line
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
if(typeof myArray !== "undefined" && typeof myData !== "undefined"){(function(y,z){return 'myArray = ' + JSON.stringify(y) + ', myData = ' + JSON.stringify(z);})(myArray, myData);}
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
var myArray = [50,60,70];
|
||||
var myData = myArray[0];
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@@ -6,10 +6,11 @@ videoUrl: 'https://scrimba.com/c/ckND4Cq'
|
||||
forumTopicId: 16159
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
One way to think of a <dfn>multi-dimensional</dfn> array, is as an <em>array of arrays</em>. When you use brackets to access your array, the first set of brackets refers to the entries in the outer-most (the first level) array, and each additional pair of brackets refers to the next level of entries inside.
|
||||
<strong>Example</strong>
|
||||
# --description--
|
||||
|
||||
One way to think of a <dfn>multi-dimensional</dfn> array, is as an *array of arrays*. When you use brackets to access your array, the first set of brackets refers to the entries in the outer-most (the first level) array, and each additional pair of brackets refers to the next level of entries inside.
|
||||
|
||||
**Example**
|
||||
|
||||
```js
|
||||
var arr = [
|
||||
@@ -23,32 +24,36 @@ arr[3][0]; // equals [10,11,12]
|
||||
arr[3][0][1]; // equals 11
|
||||
```
|
||||
|
||||
<strong>Note</strong><br>There shouldn't be any spaces between the array name and the square brackets, like `array [0][0]` and even this `array [0] [0]` is not allowed. Although JavaScript is able to process this correctly, this may confuse other programmers reading your code.
|
||||
</section>
|
||||
**Note**
|
||||
There shouldn't be any spaces between the array name and the square brackets, like `array [0][0]` and even this `array [0] [0]` is not allowed. Although JavaScript is able to process this correctly, this may confuse other programmers reading your code.
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Using bracket notation select an element from <code>myArray</code> such that <code>myData</code> is equal to <code>8</code>.
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
Using bracket notation select an element from `myArray` such that `myData` is equal to `8`.
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>myData</code> should be equal to <code>8</code>.
|
||||
testString: assert(myData === 8);
|
||||
- text: You should be using bracket notation to read the correct value from <code>myArray</code>.
|
||||
testString: assert(/myData=myArray\[2\]\[1\]/.test(__helpers.removeWhiteSpace(code)));
|
||||
# --hints--
|
||||
|
||||
`myData` should be equal to `8`.
|
||||
|
||||
```js
|
||||
assert(myData === 8);
|
||||
```
|
||||
|
||||
</section>
|
||||
You should be using bracket notation to read the correct value from `myArray`.
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
```js
|
||||
assert(/myData=myArray\[2\]\[1\]/.test(__helpers.removeWhiteSpace(code)));
|
||||
```
|
||||
|
||||
<div id='js-seed'>
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
if(typeof myArray !== "undefined"){(function(){return "myData: " + myData + " myArray: " + JSON.stringify(myArray);})();}
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
// Setup
|
||||
@@ -56,30 +61,11 @@ var myArray = [[1,2,3], [4,5,6], [7,8,9], [[10,11,12], 13, 14]];
|
||||
|
||||
// Only change code below this line
|
||||
var myData = myArray[0][0];
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
if(typeof myArray !== "undefined"){(function(){return "myData: " + myData + " myArray: " + JSON.stringify(myArray);})();}
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
var myArray = [[1,2,3],[4,5,6], [7,8,9], [[10,11,12], 13, 14]];
|
||||
var myData = myArray[2][1];
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@@ -6,9 +6,10 @@ videoUrl: 'https://scrimba.com/c/cLeGDtZ'
|
||||
forumTopicId: 16160
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
# --description--
|
||||
|
||||
As we have seen in earlier examples, objects can contain both nested objects and nested arrays. Similar to accessing nested objects, Array bracket notation can be chained to access nested arrays.
|
||||
|
||||
Here is an example of how to access a nested array:
|
||||
|
||||
```js
|
||||
@@ -34,31 +35,38 @@ ourPets[0].names[1]; // "Fluffy"
|
||||
ourPets[1].names[0]; // "Spot"
|
||||
```
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Retrieve the second tree from the variable <code>myPlants</code> using object dot and array bracket notation.
|
||||
</section>
|
||||
Retrieve the second tree from the variable `myPlants` using object dot and array bracket notation.
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>secondTree</code> should equal "pine".
|
||||
testString: assert(secondTree === "pine");
|
||||
- text: Your code should use dot and bracket notation to access <code>myPlants</code>.
|
||||
testString: assert(/=\s*myPlants\[1\].list\[1\]/.test(code));
|
||||
`secondTree` should equal "pine".
|
||||
|
||||
```js
|
||||
assert(secondTree === 'pine');
|
||||
```
|
||||
|
||||
</section>
|
||||
Your code should use dot and bracket notation to access `myPlants`.
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
```js
|
||||
assert(/=\s*myPlants\[1\].list\[1\]/.test(code));
|
||||
```
|
||||
|
||||
<div id='js-seed'>
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
(function(x) {
|
||||
if(typeof x != 'undefined') {
|
||||
return "secondTree = " + x;
|
||||
}
|
||||
return "secondTree is undefined";
|
||||
})(secondTree);
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
// Setup
|
||||
@@ -84,31 +92,9 @@ var myPlants = [
|
||||
// Only change code below this line
|
||||
|
||||
var secondTree = ""; // Change this line
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
(function(x) {
|
||||
if(typeof x != 'undefined') {
|
||||
return "secondTree = " + x;
|
||||
}
|
||||
return "secondTree is undefined";
|
||||
})(secondTree);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
var myPlants = [
|
||||
@@ -134,5 +120,3 @@ var myPlants = [
|
||||
|
||||
var secondTree = myPlants[1].list[1];
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@@ -6,9 +6,10 @@ videoUrl: 'https://scrimba.com/c/cRnRnfa'
|
||||
forumTopicId: 16161
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
# --description--
|
||||
|
||||
The sub-properties of objects can be accessed by chaining together the dot or bracket notation.
|
||||
|
||||
Here is a nested object:
|
||||
|
||||
```js
|
||||
@@ -28,31 +29,38 @@ ourStorage.cabinet["top drawer"].folder2; // "secrets"
|
||||
ourStorage.desk.drawer; // "stapler"
|
||||
```
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Access the <code>myStorage</code> object and assign the contents of the <code>glove box</code> property to the <code>gloveBoxContents</code> variable. Use dot notation for all properties where possible, otherwise use bracket notation.
|
||||
</section>
|
||||
Access the `myStorage` object and assign the contents of the `glove box` property to the `gloveBoxContents` variable. Use dot notation for all properties where possible, otherwise use bracket notation.
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>gloveBoxContents</code> should equal "maps".
|
||||
testString: assert(gloveBoxContents === "maps");
|
||||
- text: Your code should use dot and bracket notation to access <code>myStorage</code>.
|
||||
testString: assert(/=\s*myStorage\.car\.inside\[\s*("|')glove box\1\s*\]/g.test(code));
|
||||
`gloveBoxContents` should equal "maps".
|
||||
|
||||
```js
|
||||
assert(gloveBoxContents === 'maps');
|
||||
```
|
||||
|
||||
</section>
|
||||
Your code should use dot and bracket notation to access `myStorage`.
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
```js
|
||||
assert(/=\s*myStorage\.car\.inside\[\s*("|')glove box\1\s*\]/g.test(code));
|
||||
```
|
||||
|
||||
<div id='js-seed'>
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
(function(x) {
|
||||
if(typeof x != 'undefined') {
|
||||
return "gloveBoxContents = " + x;
|
||||
}
|
||||
return "gloveBoxContents is undefined";
|
||||
})(gloveBoxContents);
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
// Setup
|
||||
@@ -69,31 +77,9 @@ var myStorage = {
|
||||
};
|
||||
|
||||
var gloveBoxContents = undefined; // Change this line
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
(function(x) {
|
||||
if(typeof x != 'undefined') {
|
||||
return "gloveBoxContents = " + x;
|
||||
}
|
||||
return "gloveBoxContents is undefined";
|
||||
})(gloveBoxContents);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
var myStorage = {
|
||||
@@ -109,5 +95,3 @@ var myStorage = {
|
||||
};
|
||||
var gloveBoxContents = myStorage.car.inside["glove box"];
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@@ -6,10 +6,12 @@ videoUrl: 'https://scrimba.com/c/cBvmEHP'
|
||||
forumTopicId: 16163
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
The second way to access the properties of an object is bracket notation (<code>[]</code>). If the property of the object you are trying to access has a space in its name, you will need to use bracket notation.
|
||||
# --description--
|
||||
|
||||
The second way to access the properties of an object is bracket notation (`[]`). If the property of the object you are trying to access has a space in its name, you will need to use bracket notation.
|
||||
|
||||
However, you can still use bracket notation on object properties without spaces.
|
||||
|
||||
Here is a sample of using bracket notation to read an object's property:
|
||||
|
||||
```js
|
||||
@@ -24,37 +26,52 @@ myObj["NoSpace"]; // USS Enterprise
|
||||
```
|
||||
|
||||
Note that property names with spaces in them must be in quotes (single or double).
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Read the values of the properties <code>"an entree"</code> and <code>"the drink"</code> of <code>testObj</code> using bracket notation and assign them to <code>entreeValue</code> and <code>drinkValue</code> respectively.
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
Read the values of the properties `"an entree"` and `"the drink"` of `testObj` using bracket notation and assign them to `entreeValue` and `drinkValue` respectively.
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>entreeValue</code> should be a string
|
||||
testString: assert(typeof entreeValue === 'string' );
|
||||
- text: The value of <code>entreeValue</code> should be <code>"hamburger"</code>
|
||||
testString: assert(entreeValue === 'hamburger' );
|
||||
- text: <code>drinkValue</code> should be a string
|
||||
testString: assert(typeof drinkValue === 'string' );
|
||||
- text: The value of <code>drinkValue</code> should be <code>"water"</code>
|
||||
testString: assert(drinkValue === 'water' );
|
||||
- text: You should use bracket notation twice
|
||||
testString: assert(code.match(/testObj\s*?\[('|")[^'"]+\1\]/g).length > 1);
|
||||
# --hints--
|
||||
|
||||
`entreeValue` should be a string
|
||||
|
||||
```js
|
||||
assert(typeof entreeValue === 'string');
|
||||
```
|
||||
|
||||
</section>
|
||||
The value of `entreeValue` should be `"hamburger"`
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
```js
|
||||
assert(entreeValue === 'hamburger');
|
||||
```
|
||||
|
||||
<div id='js-seed'>
|
||||
`drinkValue` should be a string
|
||||
|
||||
```js
|
||||
assert(typeof drinkValue === 'string');
|
||||
```
|
||||
|
||||
The value of `drinkValue` should be `"water"`
|
||||
|
||||
```js
|
||||
assert(drinkValue === 'water');
|
||||
```
|
||||
|
||||
You should use bracket notation twice
|
||||
|
||||
```js
|
||||
assert(code.match(/testObj\s*?\[('|")[^'"]+\1\]/g).length > 1);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
(function(a,b) { return "entreeValue = '" + a + "', drinkValue = '" + b + "'"; })(entreeValue,drinkValue);
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
// Setup
|
||||
@@ -70,23 +87,7 @@ var entreeValue = testObj; // Change this line
|
||||
var drinkValue = testObj; // Change this line
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
(function(a,b) { return "entreeValue = '" + a + "', drinkValue = '" + b + "'"; })(entreeValue,drinkValue);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
var testObj = {
|
||||
@@ -97,5 +98,3 @@ var testObj = {
|
||||
var entreeValue = testObj["an entree"];
|
||||
var drinkValue = testObj['the drink'];
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@@ -6,11 +6,13 @@ videoUrl: 'https://scrimba.com/c/cGryJs8'
|
||||
forumTopicId: 16164
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
There are two ways to access the properties of an object: dot notation (<code>.</code>) and bracket notation (<code>[]</code>), similar to an array.
|
||||
# --description--
|
||||
|
||||
There are two ways to access the properties of an object: dot notation (`.`) and bracket notation (`[]`), similar to an array.
|
||||
|
||||
Dot notation is what you use when you know the name of the property you're trying to access ahead of time.
|
||||
Here is a sample of using dot notation (<code>.</code>) to read an object's property:
|
||||
|
||||
Here is a sample of using dot notation (`.`) to read an object's property:
|
||||
|
||||
```js
|
||||
var myObj = {
|
||||
@@ -21,37 +23,51 @@ var prop1val = myObj.prop1; // val1
|
||||
var prop2val = myObj.prop2; // val2
|
||||
```
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Read in the property values of <code>testObj</code> using dot notation. Set the variable <code>hatValue</code> equal to the object's property <code>hat</code> and set the variable <code>shirtValue</code> equal to the object's property <code>shirt</code>.
|
||||
</section>
|
||||
Read in the property values of `testObj` using dot notation. Set the variable `hatValue` equal to the object's property `hat` and set the variable `shirtValue` equal to the object's property `shirt`.
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>hatValue</code> should be a string
|
||||
testString: assert(typeof hatValue === 'string' );
|
||||
- text: The value of <code>hatValue</code> should be <code>"ballcap"</code>
|
||||
testString: assert(hatValue === 'ballcap' );
|
||||
- text: <code>shirtValue</code> should be a string
|
||||
testString: assert(typeof shirtValue === 'string' );
|
||||
- text: The value of <code>shirtValue</code> should be <code>"jersey"</code>
|
||||
testString: assert(shirtValue === 'jersey' );
|
||||
- text: You should use dot notation twice
|
||||
testString: assert(code.match(/testObj\.\w+/g).length > 1);
|
||||
`hatValue` should be a string
|
||||
|
||||
```js
|
||||
assert(typeof hatValue === 'string');
|
||||
```
|
||||
|
||||
</section>
|
||||
The value of `hatValue` should be `"ballcap"`
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
```js
|
||||
assert(hatValue === 'ballcap');
|
||||
```
|
||||
|
||||
<div id='js-seed'>
|
||||
`shirtValue` should be a string
|
||||
|
||||
```js
|
||||
assert(typeof shirtValue === 'string');
|
||||
```
|
||||
|
||||
The value of `shirtValue` should be `"jersey"`
|
||||
|
||||
```js
|
||||
assert(shirtValue === 'jersey');
|
||||
```
|
||||
|
||||
You should use dot notation twice
|
||||
|
||||
```js
|
||||
assert(code.match(/testObj\.\w+/g).length > 1);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
(function(a,b) { return "hatValue = '" + a + "', shirtValue = '" + b + "'"; })(hatValue,shirtValue);
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
// Setup
|
||||
@@ -67,23 +83,7 @@ var hatValue = testObj; // Change this line
|
||||
var shirtValue = testObj; // Change this line
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
(function(a,b) { return "hatValue = '" + a + "', shirtValue = '" + b + "'"; })(hatValue,shirtValue);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
var testObj = {
|
||||
@@ -95,5 +95,3 @@ var testObj = {
|
||||
var hatValue = testObj.hat;
|
||||
var shirtValue = testObj.shirt;
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@@ -6,9 +6,10 @@ videoUrl: 'https://scrimba.com/c/cnQyKur'
|
||||
forumTopicId: 16165
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
# --description--
|
||||
|
||||
Another use of bracket notation on objects is to access a property which is stored as the value of a variable. This can be very useful for iterating through an object's properties or when accessing a lookup table.
|
||||
|
||||
Here is an example of using a variable to access a property:
|
||||
|
||||
```js
|
||||
@@ -34,40 +35,59 @@ var someProp = propPrefix("Name"); // someProp now holds the value 'propName'
|
||||
console.log(someObj[someProp]); // "John"
|
||||
```
|
||||
|
||||
Note that we do <em>not</em> use quotes around the variable name when using it to access the property because we are using the <em>value</em> of the variable, not the <em>name</em>.
|
||||
</section>
|
||||
Note that we do *not* use quotes around the variable name when using it to access the property because we are using the *value* of the variable, not the *name*.
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Set the <code>playerNumber</code> variable to <code>16</code>. Then, use the variable to look up the player's name and assign it to <code>player</code>.
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
Set the `playerNumber` variable to `16`. Then, use the variable to look up the player's name and assign it to `player`.
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>playerNumber</code> should be a number
|
||||
testString: assert(typeof playerNumber === 'number');
|
||||
- text: The variable <code>player</code> should be a string
|
||||
testString: assert(typeof player === 'string');
|
||||
- text: The value of <code>player</code> should be "Montana"
|
||||
testString: assert(player === 'Montana');
|
||||
- text: You should use bracket notation to access <code>testObj</code>
|
||||
testString: assert(/testObj\s*?\[.*?\]/.test(code));
|
||||
- text: You should not assign the value <code>Montana</code> to the variable <code>player</code> directly.
|
||||
testString: assert(!code.match(/player\s*=\s*"|\'\s*Montana\s*"|\'\s*;/gi));
|
||||
- text: You should be using the variable <code>playerNumber</code> in your bracket notation
|
||||
testString: assert(/testObj\s*?\[\s*playerNumber\s*\]/.test(code));
|
||||
# --hints--
|
||||
|
||||
`playerNumber` should be a number
|
||||
|
||||
```js
|
||||
assert(typeof playerNumber === 'number');
|
||||
```
|
||||
|
||||
</section>
|
||||
The variable `player` should be a string
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
```js
|
||||
assert(typeof player === 'string');
|
||||
```
|
||||
|
||||
<div id='js-seed'>
|
||||
The value of `player` should be "Montana"
|
||||
|
||||
```js
|
||||
assert(player === 'Montana');
|
||||
```
|
||||
|
||||
You should use bracket notation to access `testObj`
|
||||
|
||||
```js
|
||||
assert(/testObj\s*?\[.*?\]/.test(code));
|
||||
```
|
||||
|
||||
You should not assign the value `Montana` to the variable `player` directly.
|
||||
|
||||
```js
|
||||
assert(!code.match(/player\s*=\s*"|\'\s*Montana\s*"|\'\s*;/gi));
|
||||
```
|
||||
|
||||
You should be using the variable `playerNumber` in your bracket notation
|
||||
|
||||
```js
|
||||
assert(/testObj\s*?\[\s*playerNumber\s*\]/.test(code));
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
if(typeof player !== "undefined"){(function(v){return v;})(player);}
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
// Setup
|
||||
@@ -83,23 +103,7 @@ var playerNumber; // Change this line
|
||||
var player = testObj; // Change this line
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
if(typeof player !== "undefined"){(function(v){return v;})(player);}
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
var testObj = {
|
||||
@@ -110,5 +114,3 @@ var testObj = {
|
||||
var playerNumber = 16;
|
||||
var player = testObj[playerNumber];
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@@ -6,14 +6,19 @@ videoUrl: 'https://scrimba.com/c/cQe38UD'
|
||||
forumTopicId: 301169
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
# --description--
|
||||
|
||||
You can add new properties to existing JavaScript objects the same way you would modify them.
|
||||
Here's how we would add a <code>"bark"</code> property to <code>ourDog</code>:
|
||||
<code>ourDog.bark = "bow-wow";</code>
|
||||
|
||||
Here's how we would add a `"bark"` property to `ourDog`:
|
||||
|
||||
`ourDog.bark = "bow-wow";`
|
||||
|
||||
or
|
||||
<code>ourDog["bark"] = "bow-wow";</code>
|
||||
Now when we evaluate <code>ourDog.bark</code>, we'll get his bark, "bow-wow".
|
||||
|
||||
`ourDog["bark"] = "bow-wow";`
|
||||
|
||||
Now when we evaluate `ourDog.bark`, we'll get his bark, "bow-wow".
|
||||
|
||||
Example:
|
||||
|
||||
@@ -28,31 +33,33 @@ var ourDog = {
|
||||
ourDog.bark = "bow-wow";
|
||||
```
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Add a <code>"bark"</code> property to <code>myDog</code> and set it to a dog sound, such as "woof". You may use either dot or bracket notation.
|
||||
</section>
|
||||
Add a `"bark"` property to `myDog` and set it to a dog sound, such as "woof". You may use either dot or bracket notation.
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: You should add the property <code>"bark"</code> to <code>myDog</code>.
|
||||
testString: assert(myDog.bark !== undefined);
|
||||
- text: You should not add <code>"bark"</code> to the setup section.
|
||||
testString: assert(!/bark[^\n]:/.test(code));
|
||||
You should add the property `"bark"` to `myDog`.
|
||||
|
||||
```js
|
||||
assert(myDog.bark !== undefined);
|
||||
```
|
||||
|
||||
</section>
|
||||
You should not add `"bark"` to the setup section.
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
```js
|
||||
assert(!/bark[^\n]:/.test(code));
|
||||
```
|
||||
|
||||
<div id='js-seed'>
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
(function(z){return z;})(myDog);
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
// Setup
|
||||
@@ -64,26 +71,9 @@ var myDog = {
|
||||
};
|
||||
|
||||
// Only change code below this line
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
(function(z){return z;})(myDog);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
var myDog = {
|
||||
@@ -94,5 +84,3 @@ var myDog = {
|
||||
};
|
||||
myDog.bark = "Woof Woof";
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@@ -6,68 +6,54 @@ videoUrl: 'https://scrimba.com/c/cM2KBAG'
|
||||
forumTopicId: 16650
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
<code>Number</code> is a data type in JavaScript which represents numeric data.
|
||||
# --description--
|
||||
|
||||
`Number` is a data type in JavaScript which represents numeric data.
|
||||
|
||||
Now let's try to add two numbers using JavaScript.
|
||||
JavaScript uses the <code>+</code> symbol as an addition operator when placed between two numbers.
|
||||
<strong>Example:</strong>
|
||||
|
||||
JavaScript uses the `+` symbol as an addition operator when placed between two numbers.
|
||||
|
||||
**Example:**
|
||||
|
||||
```js
|
||||
myVar = 5 + 10; // assigned 15
|
||||
```
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Change the <code>0</code> so that sum will equal <code>20</code>.
|
||||
</section>
|
||||
Change the `0` so that sum will equal `20`.
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>sum</code> should equal <code>20</code>.
|
||||
testString: assert(sum === 20);
|
||||
- text: You should use the <code>+</code> operator.
|
||||
testString: assert(/\+/.test(code));
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`sum` should equal `20`.
|
||||
|
||||
```js
|
||||
var sum = 10 + 0;
|
||||
|
||||
assert(sum === 20);
|
||||
```
|
||||
|
||||
</div>
|
||||
You should use the `+` operator.
|
||||
|
||||
```js
|
||||
assert(/\+/.test(code));
|
||||
```
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
(function(z){return 'sum = '+z;})(sum);
|
||||
```
|
||||
|
||||
</div>
|
||||
## --seed-contents--
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
```js
|
||||
var sum = 10 + 0;
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
var sum = 10 + 10;
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@@ -6,10 +6,11 @@ videoUrl: 'https://scrimba.com/c/c3JvVfg'
|
||||
forumTopicId: 16653
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
In a <code>switch</code> statement you may not be able to specify all possible values as <code>case</code> statements. Instead, you can add the <code>default</code> statement which will be executed if no matching <code>case</code> statements are found. Think of it like the final <code>else</code> statement in an <code>if/else</code> chain.
|
||||
A <code>default</code> statement should be the last case.
|
||||
# --description--
|
||||
|
||||
In a `switch` statement you may not be able to specify all possible values as `case` statements. Instead, you can add the `default` statement which will be executed if no matching `case` statements are found. Think of it like the final `else` statement in an `if/else` chain.
|
||||
|
||||
A `default` statement should be the last case.
|
||||
|
||||
```js
|
||||
switch (num) {
|
||||
@@ -26,43 +27,67 @@ switch (num) {
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Write a switch statement to set <code>answer</code> for the following conditions:<br><code>"a"</code> - "apple"<br><code>"b"</code> - "bird"<br><code>"c"</code> - "cat"<br><code>default</code> - "stuff"
|
||||
</section>
|
||||
Write a switch statement to set `answer` for the following conditions:
|
||||
`"a"` - "apple"
|
||||
`"b"` - "bird"
|
||||
`"c"` - "cat"
|
||||
`default` - "stuff"
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>switchOfStuff("a")</code> should have a value of "apple"
|
||||
testString: assert(switchOfStuff("a") === "apple");
|
||||
- text: <code>switchOfStuff("b")</code> should have a value of "bird"
|
||||
testString: assert(switchOfStuff("b") === "bird");
|
||||
- text: <code>switchOfStuff("c")</code> should have a value of "cat"
|
||||
testString: assert(switchOfStuff("c") === "cat");
|
||||
- text: <code>switchOfStuff("d")</code> should have a value of "stuff"
|
||||
testString: assert(switchOfStuff("d") === "stuff");
|
||||
- text: <code>switchOfStuff(4)</code> should have a value of "stuff"
|
||||
testString: assert(switchOfStuff(4) === "stuff");
|
||||
- text: You should not use any <code>if</code> or <code>else</code> statements
|
||||
testString: assert(!/else/g.test(code) || !/if/g.test(code));
|
||||
- text: You should use a <code>default</code> statement
|
||||
testString: assert(switchOfStuff("string-to-trigger-default-case") === "stuff");
|
||||
- text: You should have at least 3 <code>break</code> statements
|
||||
testString: assert(code.match(/break/g).length > 2);
|
||||
`switchOfStuff("a")` should have a value of "apple"
|
||||
|
||||
```js
|
||||
assert(switchOfStuff('a') === 'apple');
|
||||
```
|
||||
|
||||
</section>
|
||||
`switchOfStuff("b")` should have a value of "bird"
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
```js
|
||||
assert(switchOfStuff('b') === 'bird');
|
||||
```
|
||||
|
||||
<div id='js-seed'>
|
||||
`switchOfStuff("c")` should have a value of "cat"
|
||||
|
||||
```js
|
||||
assert(switchOfStuff('c') === 'cat');
|
||||
```
|
||||
|
||||
`switchOfStuff("d")` should have a value of "stuff"
|
||||
|
||||
```js
|
||||
assert(switchOfStuff('d') === 'stuff');
|
||||
```
|
||||
|
||||
`switchOfStuff(4)` should have a value of "stuff"
|
||||
|
||||
```js
|
||||
assert(switchOfStuff(4) === 'stuff');
|
||||
```
|
||||
|
||||
You should not use any `if` or `else` statements
|
||||
|
||||
```js
|
||||
assert(!/else/g.test(code) || !/if/g.test(code));
|
||||
```
|
||||
|
||||
You should use a `default` statement
|
||||
|
||||
```js
|
||||
assert(switchOfStuff('string-to-trigger-default-case') === 'stuff');
|
||||
```
|
||||
|
||||
You should have at least 3 `break` statements
|
||||
|
||||
```js
|
||||
assert(code.match(/break/g).length > 2);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function switchOfStuff(val) {
|
||||
@@ -76,18 +101,9 @@ function switchOfStuff(val) {
|
||||
}
|
||||
|
||||
switchOfStuff(1);
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function switchOfStuff(val) {
|
||||
@@ -109,5 +125,3 @@ function switchOfStuff(val) {
|
||||
return answer;
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@@ -6,9 +6,9 @@ videoUrl: 'https://scrimba.com/c/cbQmZfa'
|
||||
forumTopicId: 16656
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
Just as we can build a string over multiple lines out of string <dfn>literals</dfn>, we can also append variables to a string using the plus equals (<code>+=</code>) operator.
|
||||
# --description--
|
||||
|
||||
Just as we can build a string over multiple lines out of string <dfn>literals</dfn>, we can also append variables to a string using the plus equals (`+=`) operator.
|
||||
|
||||
Example:
|
||||
|
||||
@@ -19,45 +19,27 @@ ourStr += anAdjective;
|
||||
// ourStr is now "freeCodeCamp is awesome!"
|
||||
```
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Set <code>someAdjective</code> to a string of at least 3 characters and append it to <code>myStr</code> using the <code>+=</code> operator.
|
||||
</section>
|
||||
Set `someAdjective` to a string of at least 3 characters and append it to `myStr` using the `+=` operator.
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>someAdjective</code> should be set to a string at least 3 characters long.
|
||||
testString: assert(typeof someAdjective !== 'undefined' && someAdjective.length > 2);
|
||||
- text: You should append <code>someAdjective</code> to <code>myStr</code> using the <code>+=</code> operator.
|
||||
testString: assert(code.match(/myStr\s*\+=\s*someAdjective\s*/).length > 0);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`someAdjective` should be set to a string at least 3 characters long.
|
||||
|
||||
```js
|
||||
// Change code below this line
|
||||
|
||||
var someAdjective;
|
||||
var myStr = "Learning to code is ";
|
||||
|
||||
assert(typeof someAdjective !== 'undefined' && someAdjective.length > 2);
|
||||
```
|
||||
|
||||
</div>
|
||||
You should append `someAdjective` to `myStr` using the `+=` operator.
|
||||
|
||||
```js
|
||||
assert(code.match(/myStr\s*\+=\s*someAdjective\s*/).length > 0);
|
||||
```
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
(function(){
|
||||
@@ -76,18 +58,19 @@ var myStr = "Learning to code is ";
|
||||
})();
|
||||
```
|
||||
|
||||
</div>
|
||||
## --seed-contents--
|
||||
|
||||
</section>
|
||||
```js
|
||||
// Change code below this line
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
var someAdjective;
|
||||
var myStr = "Learning to code is ";
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
var someAdjective = "neat";
|
||||
var myStr = "Learning to code is ";
|
||||
myStr += someAdjective;
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@@ -6,9 +6,8 @@ videoUrl: ''
|
||||
forumTopicId: 418265
|
||||
---
|
||||
|
||||
## Description
|
||||
# --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
|
||||
@@ -20,50 +19,33 @@ 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--
|
||||
|
||||
## Instructions
|
||||
Assign the contents of `a` to variable `b`.
|
||||
|
||||
<section id='instructions'>
|
||||
Assign the contents of <code>a</code> to variable <code>b</code>.
|
||||
</section>
|
||||
# --hints--
|
||||
|
||||
## 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'>
|
||||
You should not change code above the specified comment.
|
||||
|
||||
```js
|
||||
// Setup
|
||||
var a;
|
||||
a = 7;
|
||||
var b;
|
||||
|
||||
// Only change code below this line
|
||||
assert(/var a;/.test(code) && /a = 7;/.test(code) && /var b;/.test(code));
|
||||
```
|
||||
|
||||
</div>
|
||||
`b` should have a value of 7.
|
||||
|
||||
### Before Test
|
||||
```js
|
||||
assert(typeof b === 'number' && b === 7);
|
||||
```
|
||||
|
||||
<div id='js-setup'>
|
||||
`a` should be assigned to `b` with `=`.
|
||||
|
||||
```js
|
||||
assert(/b\s*=\s*a\s*/g.test(code));
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --before-user-code--
|
||||
|
||||
```js
|
||||
if (typeof a != 'undefined') {
|
||||
@@ -74,11 +56,7 @@ if (typeof b != 'undefined') {
|
||||
}
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
### After Test
|
||||
|
||||
<div id='js-teardown'>
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
(function(a, b) {
|
||||
@@ -86,13 +64,18 @@ if (typeof b != 'undefined') {
|
||||
})(a, b);
|
||||
```
|
||||
|
||||
</div>
|
||||
## --seed-contents--
|
||||
|
||||
</section>
|
||||
```js
|
||||
// Setup
|
||||
var a;
|
||||
a = 7;
|
||||
var b;
|
||||
|
||||
## Solution
|
||||
// Only change code below this line
|
||||
```
|
||||
|
||||
<section id='solution'>
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
var a;
|
||||
@@ -100,5 +83,3 @@ a = 7;
|
||||
var b;
|
||||
b = a;
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@@ -6,37 +6,43 @@ videoUrl: 'https://scrimba.com/c/ce2pEtB'
|
||||
forumTopicId: 16658
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
If you'll recall from our discussion of <a href="/learn/javascript-algorithms-and-data-structures/basic-javascript/storing-values-with-the-assignment-operator" target="_blank">Storing Values with the Assignment Operator</a>, everything to the right of the equal sign is resolved before the value is assigned. This means we can take the return value of a function and assign it to a variable.
|
||||
Assume we have pre-defined a function <code>sum</code> which adds two numbers together, then:
|
||||
<code>ourSum = sum(5, 12);</code>
|
||||
will call <code>sum</code> function, which returns a value of <code>17</code> and assigns it to <code>ourSum</code> variable.
|
||||
</section>
|
||||
# --description--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Call the <code>processArg</code> function with an argument of <code>7</code> and assign its return value to the variable <code>processed</code>.
|
||||
</section>
|
||||
If you'll recall from our discussion of [Storing Values with the Assignment Operator](/learn/javascript-algorithms-and-data-structures/basic-javascript/storing-values-with-the-assignment-operator), everything to the right of the equal sign is resolved before the value is assigned. This means we can take the return value of a function and assign it to a variable.
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
Assume we have pre-defined a function `sum` which adds two numbers together, then:
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>processed</code> should have a value of <code>2</code>
|
||||
testString: assert(processed === 2);
|
||||
- text: You should assign <code>processArg</code> to <code>processed</code>
|
||||
testString: assert(/processed\s*=\s*processArg\(\s*7\s*\)/.test(code));
|
||||
`ourSum = sum(5, 12);`
|
||||
|
||||
will call `sum` function, which returns a value of `17` and assigns it to `ourSum` variable.
|
||||
|
||||
# --instructions--
|
||||
|
||||
Call the `processArg` function with an argument of `7` and assign its return value to the variable `processed`.
|
||||
|
||||
# --hints--
|
||||
|
||||
`processed` should have a value of `2`
|
||||
|
||||
```js
|
||||
assert(processed === 2);
|
||||
```
|
||||
|
||||
</section>
|
||||
You should assign `processArg` to `processed`
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
```js
|
||||
assert(/processed\s*=\s*processArg\(\s*7\s*\)/.test(code));
|
||||
```
|
||||
|
||||
<div id='js-seed'>
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
(function(){return "processed = " + processed})();
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
// Setup
|
||||
@@ -47,26 +53,9 @@ function processArg(num) {
|
||||
}
|
||||
|
||||
// Only change code below this line
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
(function(){return "processed = " + processed})();
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
var processed = 0;
|
||||
@@ -77,5 +66,3 @@ function processArg(num) {
|
||||
|
||||
processed = processArg(7);
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@@ -6,11 +6,14 @@ videoUrl: 'https://scrimba.com/c/cWGkbtd'
|
||||
forumTopicId: 16769
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
You may have heard the term <code>object</code> before.
|
||||
Objects are similar to <code>arrays</code>, except that instead of using indexes to access and modify their data, you access the data in objects through what are called <code>properties</code>.
|
||||
# --description--
|
||||
|
||||
You may have heard the term `object` before.
|
||||
|
||||
Objects are similar to `arrays`, except that instead of using indexes to access and modify their data, you access the data in objects through what are called `properties`.
|
||||
|
||||
Objects are useful for storing data in a structured way, and can represent real world objects, like a cat.
|
||||
|
||||
Here's a sample cat object:
|
||||
|
||||
```js
|
||||
@@ -22,7 +25,7 @@ var cat = {
|
||||
};
|
||||
```
|
||||
|
||||
In this example, all the properties are stored as strings, such as - <code>"name"</code>, <code>"legs"</code>, and <code>"tails"</code>. However, you can also use numbers as properties. You can even omit the quotes for single-word string properties, as follows:
|
||||
In this example, all the properties are stored as strings, such as - `"name"`, `"legs"`, and `"tails"`. However, you can also use numbers as properties. You can even omit the quotes for single-word string properties, as follows:
|
||||
|
||||
```js
|
||||
var anotherObject = {
|
||||
@@ -33,38 +36,106 @@ var anotherObject = {
|
||||
```
|
||||
|
||||
However, if your object has any non-string properties, JavaScript will automatically typecast them as strings.
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Make an object that represents a dog called <code>myDog</code> which contains the properties <code>"name"</code> (a string), <code>"legs"</code>, <code>"tails"</code> and <code>"friends"</code>.
|
||||
You can set these object properties to whatever values you want, as long as <code>"name"</code> is a string, <code>"legs"</code> and <code>"tails"</code> are numbers, and <code>"friends"</code> is an array.
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
Make an object that represents a dog called `myDog` which contains the properties `"name"` (a string), `"legs"`, `"tails"` and `"friends"`.
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>myDog</code> should contain the property <code>name</code> and it should be a <code>string</code>.
|
||||
testString: assert((function(z){if(z.hasOwnProperty("name") && z.name !== undefined && typeof z.name === "string"){return true;}else{return false;}})(myDog));
|
||||
- text: <code>myDog</code> should contain the property <code>legs</code> and it should be a <code>number</code>.
|
||||
testString: assert((function(z){if(z.hasOwnProperty("legs") && z.legs !== undefined && typeof z.legs === "number"){return true;}else{return false;}})(myDog));
|
||||
- text: <code>myDog</code> should contain the property <code>tails</code> and it should be a <code>number</code>.
|
||||
testString: assert((function(z){if(z.hasOwnProperty("tails") && z.tails !== undefined && typeof z.tails === "number"){return true;}else{return false;}})(myDog));
|
||||
- text: <code>myDog</code> should contain the property <code>friends</code> and it should be an <code>array</code>.
|
||||
testString: assert((function(z){if(z.hasOwnProperty("friends") && z.friends !== undefined && Array.isArray(z.friends)){return true;}else{return false;}})(myDog));
|
||||
- text: <code>myDog</code> should only contain all the given properties.
|
||||
testString: assert((function(z){return Object.keys(z).length === 4;})(myDog));
|
||||
You can set these object properties to whatever values you want, as long as `"name"` is a string, `"legs"` and `"tails"` are numbers, and `"friends"` is an array.
|
||||
|
||||
# --hints--
|
||||
|
||||
`myDog` should contain the property `name` and it should be a `string`.
|
||||
|
||||
```js
|
||||
assert(
|
||||
(function (z) {
|
||||
if (
|
||||
z.hasOwnProperty('name') &&
|
||||
z.name !== undefined &&
|
||||
typeof z.name === 'string'
|
||||
) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
})(myDog)
|
||||
);
|
||||
```
|
||||
|
||||
</section>
|
||||
`myDog` should contain the property `legs` and it should be a `number`.
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
```js
|
||||
assert(
|
||||
(function (z) {
|
||||
if (
|
||||
z.hasOwnProperty('legs') &&
|
||||
z.legs !== undefined &&
|
||||
typeof z.legs === 'number'
|
||||
) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
})(myDog)
|
||||
);
|
||||
```
|
||||
|
||||
<div id='js-seed'>
|
||||
`myDog` should contain the property `tails` and it should be a `number`.
|
||||
|
||||
```js
|
||||
assert(
|
||||
(function (z) {
|
||||
if (
|
||||
z.hasOwnProperty('tails') &&
|
||||
z.tails !== undefined &&
|
||||
typeof z.tails === 'number'
|
||||
) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
})(myDog)
|
||||
);
|
||||
```
|
||||
|
||||
`myDog` should contain the property `friends` and it should be an `array`.
|
||||
|
||||
```js
|
||||
assert(
|
||||
(function (z) {
|
||||
if (
|
||||
z.hasOwnProperty('friends') &&
|
||||
z.friends !== undefined &&
|
||||
Array.isArray(z.friends)
|
||||
) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
})(myDog)
|
||||
);
|
||||
```
|
||||
|
||||
`myDog` should only contain all the given properties.
|
||||
|
||||
```js
|
||||
assert(
|
||||
(function (z) {
|
||||
return Object.keys(z).length === 4;
|
||||
})(myDog)
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
(function(z){return z;})(myDog);
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
var myDog = {
|
||||
@@ -75,23 +146,7 @@ var myDog = {
|
||||
};
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
(function(z){return z;})(myDog);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
var myDog = {
|
||||
@@ -101,5 +156,3 @@ var myDog = {
|
||||
"friends": ["everything!"]
|
||||
};
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@@ -6,9 +6,9 @@ videoUrl: 'https://scrimba.com/c/caeJgsw'
|
||||
forumTopicId: 16772
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
<code>if/else</code> statements can be chained together for complex logic. Here is <dfn>pseudocode</dfn> of multiple chained <code>if</code> / <code>else if</code> statements:
|
||||
# --description--
|
||||
|
||||
`if/else` statements can be chained together for complex logic. Here is <dfn>pseudocode</dfn> of multiple chained `if` / `else if` statements:
|
||||
|
||||
```js
|
||||
if (condition1) {
|
||||
@@ -23,54 +23,99 @@ if (condition1) {
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Write chained <code>if</code>/<code>else if</code> statements to fulfill the following conditions:
|
||||
<code>num < 5</code> - return "Tiny"<br><code>num < 10</code> - return "Small"<br><code>num < 15</code> - return "Medium"<br><code>num < 20</code> - return "Large"<br><code>num >= 20</code> - return "Huge"
|
||||
</section>
|
||||
Write chained `if`/`else if` statements to fulfill the following conditions:
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
`num < 5` - return "Tiny"
|
||||
`num < 10` - return "Small"
|
||||
`num < 15` - return "Medium"
|
||||
`num < 20` - return "Large"
|
||||
`num >= 20` - return "Huge"
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: You should have at least four <code>else</code> statements
|
||||
testString: assert(code.match(/else/g).length > 3);
|
||||
- text: You should have at least four <code>if</code> statements
|
||||
testString: assert(code.match(/if/g).length > 3);
|
||||
- text: You should have at least one <code>return</code> statement
|
||||
testString: assert(code.match(/return/g).length >= 1);
|
||||
- text: <code>testSize(0)</code> should return "Tiny"
|
||||
testString: assert(testSize(0) === "Tiny");
|
||||
- text: <code>testSize(4)</code> should return "Tiny"
|
||||
testString: assert(testSize(4) === "Tiny");
|
||||
- text: <code>testSize(5)</code> should return "Small"
|
||||
testString: assert(testSize(5) === "Small");
|
||||
- text: <code>testSize(8)</code> should return "Small"
|
||||
testString: assert(testSize(8) === "Small");
|
||||
- text: <code>testSize(10)</code> should return "Medium"
|
||||
testString: assert(testSize(10) === "Medium");
|
||||
- text: <code>testSize(14)</code> should return "Medium"
|
||||
testString: assert(testSize(14) === "Medium");
|
||||
- text: <code>testSize(15)</code> should return "Large"
|
||||
testString: assert(testSize(15) === "Large");
|
||||
- text: <code>testSize(17)</code> should return "Large"
|
||||
testString: assert(testSize(17) === "Large");
|
||||
- text: <code>testSize(20)</code> should return "Huge"
|
||||
testString: assert(testSize(20) === "Huge");
|
||||
- text: <code>testSize(25)</code> should return "Huge"
|
||||
testString: assert(testSize(25) === "Huge");
|
||||
# --hints--
|
||||
|
||||
You should have at least four `else` statements
|
||||
|
||||
```js
|
||||
assert(code.match(/else/g).length > 3);
|
||||
```
|
||||
|
||||
</section>
|
||||
You should have at least four `if` statements
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
```js
|
||||
assert(code.match(/if/g).length > 3);
|
||||
```
|
||||
|
||||
<div id='js-seed'>
|
||||
You should have at least one `return` statement
|
||||
|
||||
```js
|
||||
assert(code.match(/return/g).length >= 1);
|
||||
```
|
||||
|
||||
`testSize(0)` should return "Tiny"
|
||||
|
||||
```js
|
||||
assert(testSize(0) === 'Tiny');
|
||||
```
|
||||
|
||||
`testSize(4)` should return "Tiny"
|
||||
|
||||
```js
|
||||
assert(testSize(4) === 'Tiny');
|
||||
```
|
||||
|
||||
`testSize(5)` should return "Small"
|
||||
|
||||
```js
|
||||
assert(testSize(5) === 'Small');
|
||||
```
|
||||
|
||||
`testSize(8)` should return "Small"
|
||||
|
||||
```js
|
||||
assert(testSize(8) === 'Small');
|
||||
```
|
||||
|
||||
`testSize(10)` should return "Medium"
|
||||
|
||||
```js
|
||||
assert(testSize(10) === 'Medium');
|
||||
```
|
||||
|
||||
`testSize(14)` should return "Medium"
|
||||
|
||||
```js
|
||||
assert(testSize(14) === 'Medium');
|
||||
```
|
||||
|
||||
`testSize(15)` should return "Large"
|
||||
|
||||
```js
|
||||
assert(testSize(15) === 'Large');
|
||||
```
|
||||
|
||||
`testSize(17)` should return "Large"
|
||||
|
||||
```js
|
||||
assert(testSize(17) === 'Large');
|
||||
```
|
||||
|
||||
`testSize(20)` should return "Huge"
|
||||
|
||||
```js
|
||||
assert(testSize(20) === 'Huge');
|
||||
```
|
||||
|
||||
`testSize(25)` should return "Huge"
|
||||
|
||||
```js
|
||||
assert(testSize(25) === 'Huge');
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function testSize(num) {
|
||||
@@ -84,15 +129,7 @@ function testSize(num) {
|
||||
testSize(7);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function testSize(num) {
|
||||
@@ -109,5 +146,3 @@ function testSize(num) {
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@@ -6,67 +6,56 @@ videoUrl: 'https://scrimba.com/c/c7ynnTp'
|
||||
forumTopicId: 16783
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
# --description--
|
||||
|
||||
Comments are lines of code that JavaScript will intentionally ignore. Comments are a great way to leave notes to yourself and to other people who will later need to figure out what that code does.
|
||||
|
||||
There are two ways to write comments in JavaScript:
|
||||
Using <code>//</code> will tell JavaScript to ignore the remainder of the text on the current line:
|
||||
|
||||
Using `//` will tell JavaScript to ignore the remainder of the text on the current line:
|
||||
|
||||
```js
|
||||
// This is an in-line comment.
|
||||
```
|
||||
|
||||
You can make a multi-line comment beginning with <code>/\*</code> and ending with <code>\*/</code>:
|
||||
You can make a multi-line comment beginning with `/*` and ending with `*/`:
|
||||
|
||||
```js
|
||||
/* This is a
|
||||
multi-line comment */
|
||||
```
|
||||
|
||||
<strong>Best Practice</strong><br>As you write code, you should regularly add comments to clarify the function of parts of your code. Good commenting can help communicate the intent of your code—both for others <em>and</em> for your future self.
|
||||
</section>
|
||||
**Best Practice**
|
||||
As you write code, you should regularly add comments to clarify the function of parts of your code. Good commenting can help communicate the intent of your code—both for others *and* for your future self.
|
||||
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Try creating one of each type of comment.
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: You should create a <code>//</code> style comment that contains at least five letters.
|
||||
testString: assert(code.match(/(\/\/)...../g));
|
||||
- text: You should create a <code>/* */</code> style comment that contains at least five letters.
|
||||
testString: assert(code.match(/(\/\*)([^\/]{5,})(?=\*\/)/gm));
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
You should create a `//` style comment that contains at least five letters.
|
||||
|
||||
```js
|
||||
|
||||
assert(code.match(/(\/\/)...../g));
|
||||
```
|
||||
|
||||
</div>
|
||||
You should create a `/* */` style comment that contains at least five letters.
|
||||
|
||||
```js
|
||||
assert(code.match(/(\/\*)([^\/]{5,})(?=\*\/)/gm));
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
</section>
|
||||
## --seed-contents--
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
```js
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
// Fake Comment
|
||||
/* Another Comment */
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@@ -6,10 +6,11 @@ videoUrl: 'https://scrimba.com/c/cKyVMAL'
|
||||
forumTopicId: 16784
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
There are many <dfn>comparison operators</dfn> in JavaScript. All of these operators return a boolean <code>true</code> or <code>false</code> value.
|
||||
The most basic operator is the equality operator <code>==</code>. The equality operator compares two values and returns <code>true</code> if they're equivalent or <code>false</code> if they are not. Note that equality is different from assignment (<code>=</code>), which assigns the value on the right of the operator to a variable on the left.
|
||||
# --description--
|
||||
|
||||
There are many <dfn>comparison operators</dfn> in JavaScript. All of these operators return a boolean `true` or `false` value.
|
||||
|
||||
The most basic operator is the equality operator `==`. The equality operator compares two values and returns `true` if they're equivalent or `false` if they are not. Note that equality is different from assignment (`=`), which assigns the value on the right of the operator to a variable on the left.
|
||||
|
||||
```js
|
||||
function equalityTest(myVal) {
|
||||
@@ -20,8 +21,7 @@ function equalityTest(myVal) {
|
||||
}
|
||||
```
|
||||
|
||||
If <code>myVal</code> is equal to <code>10</code>, the equality operator returns <code>true</code>, so the code in the curly braces will execute, and the function will return <code>"Equal"</code>. Otherwise, the function will return <code>"Not Equal"</code>.
|
||||
In order for JavaScript to compare two different <dfn>data types</dfn> (for example, <code>numbers</code> and <code>strings</code>), it must convert one type to another. This is known as "Type Coercion". Once it does, however, it can compare terms as follows:
|
||||
If `myVal` is equal to `10`, the equality operator returns `true`, so the code in the curly braces will execute, and the function will return `"Equal"`. Otherwise, the function will return `"Not Equal"`. In order for JavaScript to compare two different <dfn>data types</dfn> (for example, `numbers` and `strings`), it must convert one type to another. This is known as "Type Coercion". Once it does, however, it can compare terms as follows:
|
||||
|
||||
```js
|
||||
1 == 1 // true
|
||||
@@ -30,35 +30,39 @@ In order for JavaScript to compare two different <dfn>data types</dfn> (for exam
|
||||
"3" == 3 // true
|
||||
```
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Add the equality operator to the indicated line so that the function will return "Equal" when <code>val</code> is equivalent to <code>12</code>.
|
||||
</section>
|
||||
Add the equality operator to the indicated line so that the function will return "Equal" when `val` is equivalent to `12`.
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>testEqual(10)</code> should return "Not Equal"
|
||||
testString: assert(testEqual(10) === "Not Equal");
|
||||
- text: <code>testEqual(12)</code> should return "Equal"
|
||||
testString: assert(testEqual(12) === "Equal");
|
||||
- text: <code>testEqual("12")</code> should return "Equal"
|
||||
testString: assert(testEqual("12") === "Equal");
|
||||
- text: You should use the <code>==</code> operator
|
||||
testString: assert(code.match(/==/g) && !code.match(/===/g));
|
||||
`testEqual(10)` should return "Not Equal"
|
||||
|
||||
```js
|
||||
assert(testEqual(10) === 'Not Equal');
|
||||
```
|
||||
|
||||
</section>
|
||||
`testEqual(12)` should return "Equal"
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
```js
|
||||
assert(testEqual(12) === 'Equal');
|
||||
```
|
||||
|
||||
<div id='js-seed'>
|
||||
`testEqual("12")` should return "Equal"
|
||||
|
||||
```js
|
||||
assert(testEqual('12') === 'Equal');
|
||||
```
|
||||
|
||||
You should use the `==` operator
|
||||
|
||||
```js
|
||||
assert(code.match(/==/g) && !code.match(/===/g));
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
// Setup
|
||||
@@ -72,15 +76,7 @@ function testEqual(val) {
|
||||
testEqual(10);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function testEqual(val) {
|
||||
@@ -90,5 +86,3 @@ function testEqual(val) {
|
||||
return "Not Equal";
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@@ -6,11 +6,13 @@ videoUrl: 'https://scrimba.com/c/cp6GbH4'
|
||||
forumTopicId: 16786
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
The greater than operator (<code>></code>) compares the values of two numbers. If the number to the left is greater than the number to the right, it returns <code>true</code>. Otherwise, it returns <code>false</code>.
|
||||
# --description--
|
||||
|
||||
The greater than operator (`>`) compares the values of two numbers. If the number to the left is greater than the number to the right, it returns `true`. Otherwise, it returns `false`.
|
||||
|
||||
Like the equality operator, greater than operator will convert data types of values while comparing.
|
||||
<strong>Examples</strong>
|
||||
|
||||
**Examples**
|
||||
|
||||
```js
|
||||
5 > 3 // true
|
||||
@@ -19,43 +21,63 @@ Like the equality operator, greater than operator will convert data types of val
|
||||
'1' > 9 // false
|
||||
```
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Add the greater than operator to the indicated lines so that the return statements make sense.
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>testGreaterThan(0)</code> should return "10 or Under"
|
||||
testString: assert(testGreaterThan(0) === "10 or Under");
|
||||
- text: <code>testGreaterThan(10)</code> should return "10 or Under"
|
||||
testString: assert(testGreaterThan(10) === "10 or Under");
|
||||
- text: <code>testGreaterThan(11)</code> should return "Over 10"
|
||||
testString: assert(testGreaterThan(11) === "Over 10");
|
||||
- text: <code>testGreaterThan(99)</code> should return "Over 10"
|
||||
testString: assert(testGreaterThan(99) === "Over 10");
|
||||
- text: <code>testGreaterThan(100)</code> should return "Over 10"
|
||||
testString: assert(testGreaterThan(100) === "Over 10");
|
||||
- text: <code>testGreaterThan(101)</code> should return "Over 100"
|
||||
testString: assert(testGreaterThan(101) === "Over 100");
|
||||
- text: <code>testGreaterThan(150)</code> should return "Over 100"
|
||||
testString: assert(testGreaterThan(150) === "Over 100");
|
||||
- text: You should use the <code>></code> operator at least twice
|
||||
testString: assert(code.match(/val\s*>\s*('|")*\d+('|")*/g).length > 1);
|
||||
`testGreaterThan(0)` should return "10 or Under"
|
||||
|
||||
```js
|
||||
assert(testGreaterThan(0) === '10 or Under');
|
||||
```
|
||||
|
||||
</section>
|
||||
`testGreaterThan(10)` should return "10 or Under"
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
```js
|
||||
assert(testGreaterThan(10) === '10 or Under');
|
||||
```
|
||||
|
||||
<div id='js-seed'>
|
||||
`testGreaterThan(11)` should return "Over 10"
|
||||
|
||||
```js
|
||||
assert(testGreaterThan(11) === 'Over 10');
|
||||
```
|
||||
|
||||
`testGreaterThan(99)` should return "Over 10"
|
||||
|
||||
```js
|
||||
assert(testGreaterThan(99) === 'Over 10');
|
||||
```
|
||||
|
||||
`testGreaterThan(100)` should return "Over 10"
|
||||
|
||||
```js
|
||||
assert(testGreaterThan(100) === 'Over 10');
|
||||
```
|
||||
|
||||
`testGreaterThan(101)` should return "Over 100"
|
||||
|
||||
```js
|
||||
assert(testGreaterThan(101) === 'Over 100');
|
||||
```
|
||||
|
||||
`testGreaterThan(150)` should return "Over 100"
|
||||
|
||||
```js
|
||||
assert(testGreaterThan(150) === 'Over 100');
|
||||
```
|
||||
|
||||
You should use the `>` operator at least twice
|
||||
|
||||
```js
|
||||
assert(code.match(/val\s*>\s*('|")*\d+('|")*/g).length > 1);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function testGreaterThan(val) {
|
||||
@@ -73,15 +95,7 @@ function testGreaterThan(val) {
|
||||
testGreaterThan(10);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function testGreaterThan(val) {
|
||||
@@ -94,5 +108,3 @@ function testGreaterThan(val) {
|
||||
return "10 or Under";
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@@ -6,11 +6,13 @@ videoUrl: 'https://scrimba.com/c/c6KBqtV'
|
||||
forumTopicId: 16785
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
The greater than or equal to operator (<code>>=</code>) compares the values of two numbers. If the number to the left is greater than or equal to the number to the right, it returns <code>true</code>. Otherwise, it returns <code>false</code>.
|
||||
Like the equality operator, <code>greater than or equal to</code> operator will convert data types while comparing.
|
||||
<strong>Examples</strong>
|
||||
# --description--
|
||||
|
||||
The greater than or equal to operator (`>=`) compares the values of two numbers. If the number to the left is greater than or equal to the number to the right, it returns `true`. Otherwise, it returns `false`.
|
||||
|
||||
Like the equality operator, `greater than or equal to` operator will convert data types while comparing.
|
||||
|
||||
**Examples**
|
||||
|
||||
```js
|
||||
6 >= 6 // true
|
||||
@@ -19,43 +21,63 @@ Like the equality operator, <code>greater than or equal to</code> operator will
|
||||
'7' >= 9 // false
|
||||
```
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Add the greater than or equal to operator to the indicated lines so that the return statements make sense.
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>testGreaterOrEqual(0)</code> should return "Less than 10"
|
||||
testString: assert(testGreaterOrEqual(0) === "Less than 10");
|
||||
- text: <code>testGreaterOrEqual(9)</code> should return "Less than 10"
|
||||
testString: assert(testGreaterOrEqual(9) === "Less than 10");
|
||||
- text: <code>testGreaterOrEqual(10)</code> should return "10 or Over"
|
||||
testString: assert(testGreaterOrEqual(10) === "10 or Over");
|
||||
- text: <code>testGreaterOrEqual(11)</code> should return "10 or Over"
|
||||
testString: assert(testGreaterOrEqual(11) === "10 or Over");
|
||||
- text: <code>testGreaterOrEqual(19)</code> should return "10 or Over"
|
||||
testString: assert(testGreaterOrEqual(19) === "10 or Over");
|
||||
- text: <code>testGreaterOrEqual(100)</code> should return "20 or Over"
|
||||
testString: assert(testGreaterOrEqual(100) === "20 or Over");
|
||||
- text: <code>testGreaterOrEqual(21)</code> should return "20 or Over"
|
||||
testString: assert(testGreaterOrEqual(21) === "20 or Over");
|
||||
- text: You should use the <code>>=</code> operator at least twice
|
||||
testString: assert(code.match(/val\s*>=\s*('|")*\d+('|")*/g).length > 1);
|
||||
`testGreaterOrEqual(0)` should return "Less than 10"
|
||||
|
||||
```js
|
||||
assert(testGreaterOrEqual(0) === 'Less than 10');
|
||||
```
|
||||
|
||||
</section>
|
||||
`testGreaterOrEqual(9)` should return "Less than 10"
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
```js
|
||||
assert(testGreaterOrEqual(9) === 'Less than 10');
|
||||
```
|
||||
|
||||
<div id='js-seed'>
|
||||
`testGreaterOrEqual(10)` should return "10 or Over"
|
||||
|
||||
```js
|
||||
assert(testGreaterOrEqual(10) === '10 or Over');
|
||||
```
|
||||
|
||||
`testGreaterOrEqual(11)` should return "10 or Over"
|
||||
|
||||
```js
|
||||
assert(testGreaterOrEqual(11) === '10 or Over');
|
||||
```
|
||||
|
||||
`testGreaterOrEqual(19)` should return "10 or Over"
|
||||
|
||||
```js
|
||||
assert(testGreaterOrEqual(19) === '10 or Over');
|
||||
```
|
||||
|
||||
`testGreaterOrEqual(100)` should return "20 or Over"
|
||||
|
||||
```js
|
||||
assert(testGreaterOrEqual(100) === '20 or Over');
|
||||
```
|
||||
|
||||
`testGreaterOrEqual(21)` should return "20 or Over"
|
||||
|
||||
```js
|
||||
assert(testGreaterOrEqual(21) === '20 or Over');
|
||||
```
|
||||
|
||||
You should use the `>=` operator at least twice
|
||||
|
||||
```js
|
||||
assert(code.match(/val\s*>=\s*('|")*\d+('|")*/g).length > 1);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function testGreaterOrEqual(val) {
|
||||
@@ -73,15 +95,7 @@ function testGreaterOrEqual(val) {
|
||||
testGreaterOrEqual(10);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function testGreaterOrEqual(val) {
|
||||
@@ -96,5 +110,3 @@ function testGreaterOrEqual(val) {
|
||||
return "Less than 10";
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@@ -6,10 +6,11 @@ videoUrl: 'https://scrimba.com/c/cdBm9Sr'
|
||||
forumTopicId: 16787
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
The inequality operator (<code>!=</code>) is the opposite of the equality operator. It means "Not Equal" and returns <code>false</code> where equality would return <code>true</code> and <em>vice versa</em>. Like the equality operator, the inequality operator will convert data types of values while comparing.
|
||||
<strong>Examples</strong>
|
||||
# --description--
|
||||
|
||||
The inequality operator (`!=`) is the opposite of the equality operator. It means "Not Equal" and returns `false` where equality would return `true` and *vice versa*. Like the equality operator, the inequality operator will convert data types of values while comparing.
|
||||
|
||||
**Examples**
|
||||
|
||||
```js
|
||||
1 != 2 // true
|
||||
@@ -19,39 +20,51 @@ The inequality operator (<code>!=</code>) is the opposite of the equality operat
|
||||
0 != false // false
|
||||
```
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Add the inequality operator <code>!=</code> in the <code>if</code> statement so that the function will return "Not Equal" when <code>val</code> is not equivalent to <code>99</code>
|
||||
</section>
|
||||
Add the inequality operator `!=` in the `if` statement so that the function will return "Not Equal" when `val` is not equivalent to `99`
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>testNotEqual(99)</code> should return "Equal"
|
||||
testString: assert(testNotEqual(99) === "Equal");
|
||||
- text: <code>testNotEqual("99")</code> should return "Equal"
|
||||
testString: assert(testNotEqual("99") === "Equal");
|
||||
- text: <code>testNotEqual(12)</code> should return "Not Equal"
|
||||
testString: assert(testNotEqual(12) === "Not Equal");
|
||||
- text: <code>testNotEqual("12")</code> should return "Not Equal"
|
||||
testString: assert(testNotEqual("12") === "Not Equal");
|
||||
- text: <code>testNotEqual("bob")</code> should return "Not Equal"
|
||||
testString: assert(testNotEqual("bob") === "Not Equal");
|
||||
- text: You should use the <code>!=</code> operator
|
||||
testString: assert(code.match(/(?!!==)!=/));
|
||||
`testNotEqual(99)` should return "Equal"
|
||||
|
||||
```js
|
||||
assert(testNotEqual(99) === 'Equal');
|
||||
```
|
||||
|
||||
</section>
|
||||
`testNotEqual("99")` should return "Equal"
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
```js
|
||||
assert(testNotEqual('99') === 'Equal');
|
||||
```
|
||||
|
||||
<div id='js-seed'>
|
||||
`testNotEqual(12)` should return "Not Equal"
|
||||
|
||||
```js
|
||||
assert(testNotEqual(12) === 'Not Equal');
|
||||
```
|
||||
|
||||
`testNotEqual("12")` should return "Not Equal"
|
||||
|
||||
```js
|
||||
assert(testNotEqual('12') === 'Not Equal');
|
||||
```
|
||||
|
||||
`testNotEqual("bob")` should return "Not Equal"
|
||||
|
||||
```js
|
||||
assert(testNotEqual('bob') === 'Not Equal');
|
||||
```
|
||||
|
||||
You should use the `!=` operator
|
||||
|
||||
```js
|
||||
assert(code.match(/(?!!==)!=/));
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
// Setup
|
||||
@@ -65,15 +78,7 @@ function testNotEqual(val) {
|
||||
testNotEqual(10);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function testNotEqual(val) {
|
||||
@@ -83,5 +88,3 @@ function testNotEqual(val) {
|
||||
return "Equal";
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@@ -6,10 +6,11 @@ videoUrl: 'https://scrimba.com/c/cNVRWtB'
|
||||
forumTopicId: 16789
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
The <dfn>less than</dfn> operator (<code><</code>) compares the values of two numbers. If the number to the left is less than the number to the right, it returns <code>true</code>. Otherwise, it returns <code>false</code>. Like the equality operator, <dfn>less than</dfn> operator converts data types while comparing.
|
||||
<strong>Examples</strong>
|
||||
# --description--
|
||||
|
||||
The <dfn>less than</dfn> operator (`<`) compares the values of two numbers. If the number to the left is less than the number to the right, it returns `true`. Otherwise, it returns `false`. Like the equality operator, <dfn>less than</dfn> operator converts data types while comparing.
|
||||
|
||||
**Examples**
|
||||
|
||||
```js
|
||||
2 < 5 // true
|
||||
@@ -19,41 +20,57 @@ The <dfn>less than</dfn> operator (<code><</code>) compares the values of two
|
||||
'8' < 4 // false
|
||||
```
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Add the less than operator to the indicated lines so that the return statements make sense.
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>testLessThan(0)</code> should return "Under 25"
|
||||
testString: assert(testLessThan(0) === "Under 25");
|
||||
- text: <code>testLessThan(24)</code> should return "Under 25"
|
||||
testString: assert(testLessThan(24) === "Under 25");
|
||||
- text: <code>testLessThan(25)</code> should return "Under 55"
|
||||
testString: assert(testLessThan(25) === "Under 55");
|
||||
- text: <code>testLessThan(54)</code> should return "Under 55"
|
||||
testString: assert(testLessThan(54) === "Under 55");
|
||||
- text: <code>testLessThan(55)</code> should return "55 or Over"
|
||||
testString: assert(testLessThan(55) === "55 or Over");
|
||||
- text: <code>testLessThan(99)</code> should return "55 or Over"
|
||||
testString: assert(testLessThan(99) === "55 or Over");
|
||||
- text: You should use the <code><</code> operator at least twice
|
||||
testString: assert(code.match(/val\s*<\s*('|")*\d+('|")*/g).length > 1);
|
||||
`testLessThan(0)` should return "Under 25"
|
||||
|
||||
```js
|
||||
assert(testLessThan(0) === 'Under 25');
|
||||
```
|
||||
|
||||
</section>
|
||||
`testLessThan(24)` should return "Under 25"
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
```js
|
||||
assert(testLessThan(24) === 'Under 25');
|
||||
```
|
||||
|
||||
<div id='js-seed'>
|
||||
`testLessThan(25)` should return "Under 55"
|
||||
|
||||
```js
|
||||
assert(testLessThan(25) === 'Under 55');
|
||||
```
|
||||
|
||||
`testLessThan(54)` should return "Under 55"
|
||||
|
||||
```js
|
||||
assert(testLessThan(54) === 'Under 55');
|
||||
```
|
||||
|
||||
`testLessThan(55)` should return "55 or Over"
|
||||
|
||||
```js
|
||||
assert(testLessThan(55) === '55 or Over');
|
||||
```
|
||||
|
||||
`testLessThan(99)` should return "55 or Over"
|
||||
|
||||
```js
|
||||
assert(testLessThan(99) === '55 or Over');
|
||||
```
|
||||
|
||||
You should use the `<` operator at least twice
|
||||
|
||||
```js
|
||||
assert(code.match(/val\s*<\s*('|")*\d+('|")*/g).length > 1);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function testLessThan(val) {
|
||||
@@ -71,15 +88,7 @@ function testLessThan(val) {
|
||||
testLessThan(10);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function testLessThan(val) {
|
||||
@@ -94,5 +103,3 @@ function testLessThan(val) {
|
||||
return "55 or Over";
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@@ -6,10 +6,11 @@ videoUrl: 'https://scrimba.com/c/cNVR7Am'
|
||||
forumTopicId: 16788
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
The less than or equal to operator (<code><=</code>) compares the values of two numbers. If the number to the left is less than or equal to the number to the right, it returns <code>true</code>. If the number on the left is greater than the number on the right, it returns <code>false</code>. Like the equality operator, <code>less than or equal to</code> converts data types.
|
||||
<strong>Examples</strong>
|
||||
# --description--
|
||||
|
||||
The less than or equal to operator (`<=`) compares the values of two numbers. If the number to the left is less than or equal to the number to the right, it returns `true`. If the number on the left is greater than the number on the right, it returns `false`. Like the equality operator, `less than or equal to` converts data types.
|
||||
|
||||
**Examples**
|
||||
|
||||
```js
|
||||
4 <= 5 // true
|
||||
@@ -19,43 +20,63 @@ The less than or equal to operator (<code><=</code>) compares the values of t
|
||||
'8' <= 4 // false
|
||||
```
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Add the less than or equal to operator to the indicated lines so that the return statements make sense.
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>testLessOrEqual(0)</code> should return "Smaller Than or Equal to 12"
|
||||
testString: assert(testLessOrEqual(0) === "Smaller Than or Equal to 12");
|
||||
- text: <code>testLessOrEqual(11)</code> should return "Smaller Than or Equal to 12"
|
||||
testString: assert(testLessOrEqual(11) === "Smaller Than or Equal to 12");
|
||||
- text: <code>testLessOrEqual(12)</code> should return "Smaller Than or Equal to 12"
|
||||
testString: assert(testLessOrEqual(12) === "Smaller Than or Equal to 12");
|
||||
- text: <code>testLessOrEqual(23)</code> should return "Smaller Than or Equal to 24"
|
||||
testString: assert(testLessOrEqual(23) === "Smaller Than or Equal to 24");
|
||||
- text: <code>testLessOrEqual(24)</code> should return "Smaller Than or Equal to 24"
|
||||
testString: assert(testLessOrEqual(24) === "Smaller Than or Equal to 24");
|
||||
- text: <code>testLessOrEqual(25)</code> should return "More Than 24"
|
||||
testString: assert(testLessOrEqual(25) === "More Than 24");
|
||||
- text: <code>testLessOrEqual(55)</code> should return "More Than 24"
|
||||
testString: assert(testLessOrEqual(55) === "More Than 24");
|
||||
- text: You should use the <code><=</code> operator at least twice
|
||||
testString: assert(code.match(/val\s*<=\s*('|")*\d+('|")*/g).length > 1);
|
||||
`testLessOrEqual(0)` should return "Smaller Than or Equal to 12"
|
||||
|
||||
```js
|
||||
assert(testLessOrEqual(0) === 'Smaller Than or Equal to 12');
|
||||
```
|
||||
|
||||
</section>
|
||||
`testLessOrEqual(11)` should return "Smaller Than or Equal to 12"
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
```js
|
||||
assert(testLessOrEqual(11) === 'Smaller Than or Equal to 12');
|
||||
```
|
||||
|
||||
<div id='js-seed'>
|
||||
`testLessOrEqual(12)` should return "Smaller Than or Equal to 12"
|
||||
|
||||
```js
|
||||
assert(testLessOrEqual(12) === 'Smaller Than or Equal to 12');
|
||||
```
|
||||
|
||||
`testLessOrEqual(23)` should return "Smaller Than or Equal to 24"
|
||||
|
||||
```js
|
||||
assert(testLessOrEqual(23) === 'Smaller Than or Equal to 24');
|
||||
```
|
||||
|
||||
`testLessOrEqual(24)` should return "Smaller Than or Equal to 24"
|
||||
|
||||
```js
|
||||
assert(testLessOrEqual(24) === 'Smaller Than or Equal to 24');
|
||||
```
|
||||
|
||||
`testLessOrEqual(25)` should return "More Than 24"
|
||||
|
||||
```js
|
||||
assert(testLessOrEqual(25) === 'More Than 24');
|
||||
```
|
||||
|
||||
`testLessOrEqual(55)` should return "More Than 24"
|
||||
|
||||
```js
|
||||
assert(testLessOrEqual(55) === 'More Than 24');
|
||||
```
|
||||
|
||||
You should use the `<=` operator at least twice
|
||||
|
||||
```js
|
||||
assert(code.match(/val\s*<=\s*('|")*\d+('|")*/g).length > 1);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function testLessOrEqual(val) {
|
||||
@@ -71,18 +92,9 @@ function testLessOrEqual(val) {
|
||||
}
|
||||
|
||||
testLessOrEqual(10);
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function testLessOrEqual(val) {
|
||||
@@ -97,5 +109,3 @@ function testLessOrEqual(val) {
|
||||
return "More Than 24";
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@@ -6,47 +6,54 @@ videoUrl: 'https://scrimba.com/c/cy87atr'
|
||||
forumTopicId: 16790
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
Strict equality (<code>===</code>) is the counterpart to the equality operator (<code>==</code>). However, unlike the equality operator, which attempts to convert both values being compared to a common type, the strict equality operator does not perform a type conversion.
|
||||
# --description--
|
||||
|
||||
Strict equality (`===`) is the counterpart to the equality operator (`==`). However, unlike the equality operator, which attempts to convert both values being compared to a common type, the strict equality operator does not perform a type conversion.
|
||||
|
||||
If the values being compared have different types, they are considered unequal, and the strict equality operator will return false.
|
||||
<strong>Examples</strong>
|
||||
|
||||
**Examples**
|
||||
|
||||
```js
|
||||
3 === 3 // true
|
||||
3 === '3' // false
|
||||
```
|
||||
|
||||
In the second example, <code>3</code> is a <code>Number</code> type and <code>'3'</code> is a <code>String</code> type.
|
||||
</section>
|
||||
In the second example, `3` is a `Number` type and `'3'` is a `String` type.
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Use the strict equality operator in the <code>if</code> statement so the function will return "Equal" when <code>val</code> is strictly equal to <code>7</code>
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
Use the strict equality operator in the `if` statement so the function will return "Equal" when `val` is strictly equal to `7`
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>testStrict(10)</code> should return "Not Equal"
|
||||
testString: assert(testStrict(10) === "Not Equal");
|
||||
- text: <code>testStrict(7)</code> should return "Equal"
|
||||
testString: assert(testStrict(7) === "Equal");
|
||||
- text: <code>testStrict("7")</code> should return "Not Equal"
|
||||
testString: assert(testStrict("7") === "Not Equal");
|
||||
- text: You should use the <code>===</code> operator
|
||||
testString: assert(code.match(/(val\s*===\s*\d+)|(\d+\s*===\s*val)/g).length > 0);
|
||||
# --hints--
|
||||
|
||||
`testStrict(10)` should return "Not Equal"
|
||||
|
||||
```js
|
||||
assert(testStrict(10) === 'Not Equal');
|
||||
```
|
||||
|
||||
</section>
|
||||
`testStrict(7)` should return "Equal"
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
```js
|
||||
assert(testStrict(7) === 'Equal');
|
||||
```
|
||||
|
||||
<div id='js-seed'>
|
||||
`testStrict("7")` should return "Not Equal"
|
||||
|
||||
```js
|
||||
assert(testStrict('7') === 'Not Equal');
|
||||
```
|
||||
|
||||
You should use the `===` operator
|
||||
|
||||
```js
|
||||
assert(code.match(/(val\s*===\s*\d+)|(\d+\s*===\s*val)/g).length > 0);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
// Setup
|
||||
@@ -60,15 +67,7 @@ function testStrict(val) {
|
||||
testStrict(10);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function testStrict(val) {
|
||||
@@ -78,5 +77,3 @@ function testStrict(val) {
|
||||
return "Not Equal";
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@@ -6,10 +6,11 @@ videoUrl: 'https://scrimba.com/c/cKekkUy'
|
||||
forumTopicId: 16791
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
The strict inequality operator (<code>!==</code>) is the logical opposite of the strict equality operator. It means "Strictly Not Equal" and returns <code>false</code> where strict equality would return <code>true</code> and <em>vice versa</em>. Strict inequality will not convert data types.
|
||||
<strong>Examples</strong>
|
||||
# --description--
|
||||
|
||||
The strict inequality operator (`!==`) is the logical opposite of the strict equality operator. It means "Strictly Not Equal" and returns `false` where strict equality would return `true` and *vice versa*. Strict inequality will not convert data types.
|
||||
|
||||
**Examples**
|
||||
|
||||
```js
|
||||
3 !== 3 // false
|
||||
@@ -17,37 +18,45 @@ The strict inequality operator (<code>!==</code>) is the logical opposite of the
|
||||
4 !== 3 // true
|
||||
```
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Add the strict inequality operator to the <code>if</code> statement so the function will return "Not Equal" when <code>val</code> is not strictly equal to <code>17</code>
|
||||
</section>
|
||||
Add the strict inequality operator to the `if` statement so the function will return "Not Equal" when `val` is not strictly equal to `17`
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>testStrictNotEqual(17)</code> should return "Equal"
|
||||
testString: assert(testStrictNotEqual(17) === "Equal");
|
||||
- text: <code>testStrictNotEqual("17")</code> should return "Not Equal"
|
||||
testString: assert(testStrictNotEqual("17") === "Not Equal");
|
||||
- text: <code>testStrictNotEqual(12)</code> should return "Not Equal"
|
||||
testString: assert(testStrictNotEqual(12) === "Not Equal");
|
||||
- text: <code>testStrictNotEqual("bob")</code> should return "Not Equal"
|
||||
testString: assert(testStrictNotEqual("bob") === "Not Equal");
|
||||
- text: You should use the <code>!==</code> operator
|
||||
testString: assert(code.match(/(val\s*!==\s*\d+)|(\d+\s*!==\s*val)/g).length > 0);
|
||||
`testStrictNotEqual(17)` should return "Equal"
|
||||
|
||||
```js
|
||||
assert(testStrictNotEqual(17) === 'Equal');
|
||||
```
|
||||
|
||||
</section>
|
||||
`testStrictNotEqual("17")` should return "Not Equal"
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
```js
|
||||
assert(testStrictNotEqual('17') === 'Not Equal');
|
||||
```
|
||||
|
||||
<div id='js-seed'>
|
||||
`testStrictNotEqual(12)` should return "Not Equal"
|
||||
|
||||
```js
|
||||
assert(testStrictNotEqual(12) === 'Not Equal');
|
||||
```
|
||||
|
||||
`testStrictNotEqual("bob")` should return "Not Equal"
|
||||
|
||||
```js
|
||||
assert(testStrictNotEqual('bob') === 'Not Equal');
|
||||
```
|
||||
|
||||
You should use the `!==` operator
|
||||
|
||||
```js
|
||||
assert(code.match(/(val\s*!==\s*\d+)|(\d+\s*!==\s*val)/g).length > 0);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
// Setup
|
||||
@@ -61,15 +70,7 @@ function testStrictNotEqual(val) {
|
||||
testStrictNotEqual(10);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function testStrictNotEqual(val) {
|
||||
@@ -79,5 +80,3 @@ function testStrictNotEqual(val) {
|
||||
return "Equal";
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@@ -6,9 +6,10 @@ videoUrl: 'https://scrimba.com/c/cvbRVtr'
|
||||
forumTopicId: 16799
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
Sometimes you will need to test more than one thing at a time. The <dfn>logical and</dfn> operator (<code>&&</code>) returns <code>true</code> if and only if the <dfn>operands</dfn> to the left and right of it are true.
|
||||
# --description--
|
||||
|
||||
Sometimes you will need to test more than one thing at a time. The <dfn>logical and</dfn> operator (`&&`) returns `true` if and only if the <dfn>operands</dfn> to the left and right of it are true.
|
||||
|
||||
The same effect could be achieved by nesting an if statement inside another if:
|
||||
|
||||
```js
|
||||
@@ -20,7 +21,7 @@ if (num > 5) {
|
||||
return "No";
|
||||
```
|
||||
|
||||
will only return "Yes" if <code>num</code> is greater than <code>5</code> and less than <code>10</code>. The same logic can be written as:
|
||||
will only return "Yes" if `num` is greater than `5` and less than `10`. The same logic can be written as:
|
||||
|
||||
```js
|
||||
if (num > 5 && num < 10) {
|
||||
@@ -29,47 +30,75 @@ if (num > 5 && num < 10) {
|
||||
return "No";
|
||||
```
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Replace the two if statements with one statement, using the && operator, which will return <code>"Yes"</code> if <code>val</code> is less than or equal to <code>50</code> and greater than or equal to <code>25</code>. Otherwise, will return <code>"No"</code>.
|
||||
</section>
|
||||
Replace the two if statements with one statement, using the && operator, which will return `"Yes"` if `val` is less than or equal to `50` and greater than or equal to `25`. Otherwise, will return `"No"`.
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: You should use the <code>&&</code> operator once
|
||||
testString: assert(code.match(/&&/g).length === 1);
|
||||
- text: You should only have one <code>if</code> statement
|
||||
testString: assert(code.match(/if/g).length === 1);
|
||||
- text: <code>testLogicalAnd(0)</code> should return "No"
|
||||
testString: assert(testLogicalAnd(0) === "No");
|
||||
- text: <code>testLogicalAnd(24)</code> should return "No"
|
||||
testString: assert(testLogicalAnd(24) === "No");
|
||||
- text: <code>testLogicalAnd(25)</code> should return "Yes"
|
||||
testString: assert(testLogicalAnd(25) === "Yes");
|
||||
- text: <code>testLogicalAnd(30)</code> should return "Yes"
|
||||
testString: assert(testLogicalAnd(30) === "Yes");
|
||||
- text: <code>testLogicalAnd(50)</code> should return "Yes"
|
||||
testString: assert(testLogicalAnd(50) === "Yes");
|
||||
- text: <code>testLogicalAnd(51)</code> should return "No"
|
||||
testString: assert(testLogicalAnd(51) === "No");
|
||||
- text: <code>testLogicalAnd(75)</code> should return "No"
|
||||
testString: assert(testLogicalAnd(75) === "No");
|
||||
- text: <code>testLogicalAnd(80)</code> should return "No"
|
||||
testString: assert(testLogicalAnd(80) === "No");
|
||||
You should use the `&&` operator once
|
||||
|
||||
```js
|
||||
assert(code.match(/&&/g).length === 1);
|
||||
```
|
||||
|
||||
</section>
|
||||
You should only have one `if` statement
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
```js
|
||||
assert(code.match(/if/g).length === 1);
|
||||
```
|
||||
|
||||
<div id='js-seed'>
|
||||
`testLogicalAnd(0)` should return "No"
|
||||
|
||||
```js
|
||||
assert(testLogicalAnd(0) === 'No');
|
||||
```
|
||||
|
||||
`testLogicalAnd(24)` should return "No"
|
||||
|
||||
```js
|
||||
assert(testLogicalAnd(24) === 'No');
|
||||
```
|
||||
|
||||
`testLogicalAnd(25)` should return "Yes"
|
||||
|
||||
```js
|
||||
assert(testLogicalAnd(25) === 'Yes');
|
||||
```
|
||||
|
||||
`testLogicalAnd(30)` should return "Yes"
|
||||
|
||||
```js
|
||||
assert(testLogicalAnd(30) === 'Yes');
|
||||
```
|
||||
|
||||
`testLogicalAnd(50)` should return "Yes"
|
||||
|
||||
```js
|
||||
assert(testLogicalAnd(50) === 'Yes');
|
||||
```
|
||||
|
||||
`testLogicalAnd(51)` should return "No"
|
||||
|
||||
```js
|
||||
assert(testLogicalAnd(51) === 'No');
|
||||
```
|
||||
|
||||
`testLogicalAnd(75)` should return "No"
|
||||
|
||||
```js
|
||||
assert(testLogicalAnd(75) === 'No');
|
||||
```
|
||||
|
||||
`testLogicalAnd(80)` should return "No"
|
||||
|
||||
```js
|
||||
assert(testLogicalAnd(80) === 'No');
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function testLogicalAnd(val) {
|
||||
@@ -88,15 +117,7 @@ function testLogicalAnd(val) {
|
||||
testLogicalAnd(10);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function testLogicalAnd(val) {
|
||||
@@ -106,5 +127,3 @@ function testLogicalAnd(val) {
|
||||
return "No";
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@@ -6,10 +6,12 @@ videoUrl: 'https://scrimba.com/c/cEPrGTN'
|
||||
forumTopicId: 16800
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
The <dfn>logical or</dfn> operator (<code>||</code>) returns <code>true</code> if either of the <dfn>operands</dfn> is <code>true</code>. Otherwise, it returns <code>false</code>.
|
||||
The <dfn>logical or</dfn> operator is composed of two pipe symbols: (<code>||</code>). This can typically be found between your Backspace and Enter keys.
|
||||
# --description--
|
||||
|
||||
The <dfn>logical or</dfn> operator (`||`) returns `true` if either of the <dfn>operands</dfn> is `true`. Otherwise, it returns `false`.
|
||||
|
||||
The <dfn>logical or</dfn> operator is composed of two pipe symbols: (`||`). This can typically be found between your Backspace and Enter keys.
|
||||
|
||||
The pattern below should look familiar from prior waypoints:
|
||||
|
||||
```js
|
||||
@@ -22,7 +24,7 @@ if (num < 5) {
|
||||
return "Yes";
|
||||
```
|
||||
|
||||
will return "Yes" only if <code>num</code> is between <code>5</code> and <code>10</code> (5 and 10 included). The same logic can be written as:
|
||||
will return "Yes" only if `num` is between `5` and `10` (5 and 10 included). The same logic can be written as:
|
||||
|
||||
```js
|
||||
if (num > 10 || num < 5) {
|
||||
@@ -31,47 +33,75 @@ if (num > 10 || num < 5) {
|
||||
return "Yes";
|
||||
```
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Combine the two <code>if</code> statements into one statement which returns <code>"Outside"</code> if <code>val</code> is not between <code>10</code> and <code>20</code>, inclusive. Otherwise, return <code>"Inside"</code>.
|
||||
</section>
|
||||
Combine the two `if` statements into one statement which returns `"Outside"` if `val` is not between `10` and `20`, inclusive. Otherwise, return `"Inside"`.
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: You should use the <code>||</code> operator once
|
||||
testString: assert(code.match(/\|\|/g).length === 1);
|
||||
- text: You should only have one <code>if</code> statement
|
||||
testString: assert(code.match(/if/g).length === 1);
|
||||
- text: <code>testLogicalOr(0)</code> should return "Outside"
|
||||
testString: assert(testLogicalOr(0) === "Outside");
|
||||
- text: <code>testLogicalOr(9)</code> should return "Outside"
|
||||
testString: assert(testLogicalOr(9) === "Outside");
|
||||
- text: <code>testLogicalOr(10)</code> should return "Inside"
|
||||
testString: assert(testLogicalOr(10) === "Inside");
|
||||
- text: <code>testLogicalOr(15)</code> should return "Inside"
|
||||
testString: assert(testLogicalOr(15) === "Inside");
|
||||
- text: <code>testLogicalOr(19)</code> should return "Inside"
|
||||
testString: assert(testLogicalOr(19) === "Inside");
|
||||
- text: <code>testLogicalOr(20)</code> should return "Inside"
|
||||
testString: assert(testLogicalOr(20) === "Inside");
|
||||
- text: <code>testLogicalOr(21)</code> should return "Outside"
|
||||
testString: assert(testLogicalOr(21) === "Outside");
|
||||
- text: <code>testLogicalOr(25)</code> should return "Outside"
|
||||
testString: assert(testLogicalOr(25) === "Outside");
|
||||
You should use the `||` operator once
|
||||
|
||||
```js
|
||||
assert(code.match(/\|\|/g).length === 1);
|
||||
```
|
||||
|
||||
</section>
|
||||
You should only have one `if` statement
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
```js
|
||||
assert(code.match(/if/g).length === 1);
|
||||
```
|
||||
|
||||
<div id='js-seed'>
|
||||
`testLogicalOr(0)` should return "Outside"
|
||||
|
||||
```js
|
||||
assert(testLogicalOr(0) === 'Outside');
|
||||
```
|
||||
|
||||
`testLogicalOr(9)` should return "Outside"
|
||||
|
||||
```js
|
||||
assert(testLogicalOr(9) === 'Outside');
|
||||
```
|
||||
|
||||
`testLogicalOr(10)` should return "Inside"
|
||||
|
||||
```js
|
||||
assert(testLogicalOr(10) === 'Inside');
|
||||
```
|
||||
|
||||
`testLogicalOr(15)` should return "Inside"
|
||||
|
||||
```js
|
||||
assert(testLogicalOr(15) === 'Inside');
|
||||
```
|
||||
|
||||
`testLogicalOr(19)` should return "Inside"
|
||||
|
||||
```js
|
||||
assert(testLogicalOr(19) === 'Inside');
|
||||
```
|
||||
|
||||
`testLogicalOr(20)` should return "Inside"
|
||||
|
||||
```js
|
||||
assert(testLogicalOr(20) === 'Inside');
|
||||
```
|
||||
|
||||
`testLogicalOr(21)` should return "Outside"
|
||||
|
||||
```js
|
||||
assert(testLogicalOr(21) === 'Outside');
|
||||
```
|
||||
|
||||
`testLogicalOr(25)` should return "Outside"
|
||||
|
||||
```js
|
||||
assert(testLogicalOr(25) === 'Outside');
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function testLogicalOr(val) {
|
||||
@@ -92,15 +122,7 @@ function testLogicalOr(val) {
|
||||
testLogicalOr(15);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function testLogicalOr(val) {
|
||||
@@ -110,5 +132,3 @@ function testLogicalOr(val) {
|
||||
return "Inside";
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@@ -6,12 +6,15 @@ videoUrl: 'https://scrimba.com/c/cDR6LCb'
|
||||
forumTopicId: 16661
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
# --description--
|
||||
|
||||
In programming, it is common to use assignments to modify the contents of a variable. Remember that everything to the right of the equals sign is evaluated first, so we can say:
|
||||
<code>myVar = myVar + 5;</code>
|
||||
to add <code>5</code> to <code>myVar</code>. Since this is such a common pattern, there are operators which do both a mathematical operation and assignment in one step.
|
||||
One such operator is the <code>+=</code> operator.
|
||||
|
||||
`myVar = myVar + 5;`
|
||||
|
||||
to add `5` to `myVar`. Since this is such a common pattern, there are operators which do both a mathematical operation and assignment in one step.
|
||||
|
||||
One such operator is the `+=` operator.
|
||||
|
||||
```js
|
||||
var myVar = 1;
|
||||
@@ -19,37 +22,55 @@ myVar += 5;
|
||||
console.log(myVar); // Returns 6
|
||||
```
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Convert the assignments for <code>a</code>, <code>b</code>, and <code>c</code> to use the <code>+=</code> operator.
|
||||
</section>
|
||||
Convert the assignments for `a`, `b`, and `c` to use the `+=` operator.
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>a</code> should equal <code>15</code>.
|
||||
testString: assert(a === 15);
|
||||
- text: <code>b</code> should equal <code>26</code>.
|
||||
testString: assert(b === 26);
|
||||
- text: <code>c</code> should equal <code>19</code>.
|
||||
testString: assert(c === 19);
|
||||
- text: You should use the <code>+=</code> operator for each variable.
|
||||
testString: assert(code.match(/\+=/g).length === 3);
|
||||
- text: You should not modify the code above the specified comment.
|
||||
testString: assert(/var a = 3;/.test(code) && /var b = 17;/.test(code) && /var c = 12;/.test(code));
|
||||
`a` should equal `15`.
|
||||
|
||||
```js
|
||||
assert(a === 15);
|
||||
```
|
||||
|
||||
</section>
|
||||
`b` should equal `26`.
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
```js
|
||||
assert(b === 26);
|
||||
```
|
||||
|
||||
<div id='js-seed'>
|
||||
`c` should equal `19`.
|
||||
|
||||
```js
|
||||
assert(c === 19);
|
||||
```
|
||||
|
||||
You should use the `+=` operator for each variable.
|
||||
|
||||
```js
|
||||
assert(code.match(/\+=/g).length === 3);
|
||||
```
|
||||
|
||||
You should not modify the code above the specified comment.
|
||||
|
||||
```js
|
||||
assert(
|
||||
/var a = 3;/.test(code) &&
|
||||
/var b = 17;/.test(code) &&
|
||||
/var c = 12;/.test(code)
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
(function(a,b,c){ return "a = " + a + ", b = " + b + ", c = " + c; })(a,b,c);
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
var a = 3;
|
||||
@@ -62,23 +83,7 @@ b = 9 + b;
|
||||
c = c + 7;
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
(function(a,b,c){ return "a = " + a + ", b = " + b + ", c = " + c; })(a,b,c);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
var a = 3;
|
||||
@@ -89,5 +94,3 @@ a += 12;
|
||||
b += 9;
|
||||
c += 7;
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@@ -6,43 +6,65 @@ videoUrl: 'https://scrimba.com/c/c2QvKT2'
|
||||
forumTopicId: 16659
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
The <code>/=</code> operator divides a variable by another number.
|
||||
<code>myVar = myVar / 5;</code>
|
||||
Will divide <code>myVar</code> by <code>5</code>. This can be rewritten as:
|
||||
<code>myVar /= 5;</code>
|
||||
</section>
|
||||
# --description--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Convert the assignments for <code>a</code>, <code>b</code>, and <code>c</code> to use the <code>/=</code> operator.
|
||||
</section>
|
||||
The `/=` operator divides a variable by another number.
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
`myVar = myVar / 5;`
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>a</code> should equal <code>4</code>.
|
||||
testString: assert(a === 4);
|
||||
- text: <code>b</code> should equal <code>27</code>.
|
||||
testString: assert(b === 27);
|
||||
- text: <code>c</code> should equal <code>3</code>.
|
||||
testString: assert(c === 3);
|
||||
- text: You should use the <code>/=</code> operator for each variable.
|
||||
testString: assert(code.match(/\/=/g).length === 3);
|
||||
- text: You should not modify the code above the specified comment.
|
||||
testString: assert(/var a = 48;/.test(code) && /var b = 108;/.test(code) && /var c = 33;/.test(code));
|
||||
Will divide `myVar` by `5`. This can be rewritten as:
|
||||
|
||||
`myVar /= 5;`
|
||||
|
||||
# --instructions--
|
||||
|
||||
Convert the assignments for `a`, `b`, and `c` to use the `/=` operator.
|
||||
|
||||
# --hints--
|
||||
|
||||
`a` should equal `4`.
|
||||
|
||||
```js
|
||||
assert(a === 4);
|
||||
```
|
||||
|
||||
</section>
|
||||
`b` should equal `27`.
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
```js
|
||||
assert(b === 27);
|
||||
```
|
||||
|
||||
<div id='js-seed'>
|
||||
`c` should equal `3`.
|
||||
|
||||
```js
|
||||
assert(c === 3);
|
||||
```
|
||||
|
||||
You should use the `/=` operator for each variable.
|
||||
|
||||
```js
|
||||
assert(code.match(/\/=/g).length === 3);
|
||||
```
|
||||
|
||||
You should not modify the code above the specified comment.
|
||||
|
||||
```js
|
||||
assert(
|
||||
/var a = 48;/.test(code) &&
|
||||
/var b = 108;/.test(code) &&
|
||||
/var c = 33;/.test(code)
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
(function(a,b,c){ return "a = " + a + ", b = " + b + ", c = " + c; })(a,b,c);
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
var a = 48;
|
||||
@@ -55,23 +77,7 @@ b = b / 4;
|
||||
c = c / 11;
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
(function(a,b,c){ return "a = " + a + ", b = " + b + ", c = " + c; })(a,b,c);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
var a = 48;
|
||||
@@ -82,5 +88,3 @@ a /= 12;
|
||||
b /= 4;
|
||||
c /= 11;
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@@ -6,43 +6,65 @@ videoUrl: 'https://scrimba.com/c/c83vrfa'
|
||||
forumTopicId: 16662
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
The <code>*=</code> operator multiplies a variable by a number.
|
||||
<code>myVar = myVar * 5;</code>
|
||||
will multiply <code>myVar</code> by <code>5</code>. This can be rewritten as:
|
||||
<code>myVar *= 5;</code>
|
||||
</section>
|
||||
# --description--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Convert the assignments for <code>a</code>, <code>b</code>, and <code>c</code> to use the <code>*=</code> operator.
|
||||
</section>
|
||||
The `*=` operator multiplies a variable by a number.
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
`myVar = myVar * 5;`
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>a</code> should equal <code>25</code>.
|
||||
testString: assert(a === 25);
|
||||
- text: <code>b</code> should equal <code>36</code>.
|
||||
testString: assert(b === 36);
|
||||
- text: <code>c</code> should equal <code>46</code>.
|
||||
testString: assert(c === 46);
|
||||
- text: You should use the <code>*=</code> operator for each variable.
|
||||
testString: assert(code.match(/\*=/g).length === 3);
|
||||
- text: You should not modify the code above the specified comment.
|
||||
testString: assert(/var a = 5;/.test(code) && /var b = 12;/.test(code) && /var c = 4\.6;/.test(code));
|
||||
will multiply `myVar` by `5`. This can be rewritten as:
|
||||
|
||||
`myVar *= 5;`
|
||||
|
||||
# --instructions--
|
||||
|
||||
Convert the assignments for `a`, `b`, and `c` to use the `*=` operator.
|
||||
|
||||
# --hints--
|
||||
|
||||
`a` should equal `25`.
|
||||
|
||||
```js
|
||||
assert(a === 25);
|
||||
```
|
||||
|
||||
</section>
|
||||
`b` should equal `36`.
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
```js
|
||||
assert(b === 36);
|
||||
```
|
||||
|
||||
<div id='js-seed'>
|
||||
`c` should equal `46`.
|
||||
|
||||
```js
|
||||
assert(c === 46);
|
||||
```
|
||||
|
||||
You should use the `*=` operator for each variable.
|
||||
|
||||
```js
|
||||
assert(code.match(/\*=/g).length === 3);
|
||||
```
|
||||
|
||||
You should not modify the code above the specified comment.
|
||||
|
||||
```js
|
||||
assert(
|
||||
/var a = 5;/.test(code) &&
|
||||
/var b = 12;/.test(code) &&
|
||||
/var c = 4\.6;/.test(code)
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
(function(a,b,c){ return "a = " + a + ", b = " + b + ", c = " + c; })(a,b,c);
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
var a = 5;
|
||||
@@ -55,23 +77,7 @@ b = 3 * b;
|
||||
c = c * 10;
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
(function(a,b,c){ return "a = " + a + ", b = " + b + ", c = " + c; })(a,b,c);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
var a = 5;
|
||||
@@ -82,5 +88,3 @@ a *= 5;
|
||||
b *= 3;
|
||||
c *= 10;
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@@ -6,43 +6,63 @@ videoUrl: 'https://scrimba.com/c/c2Qv7AV'
|
||||
forumTopicId: 16660
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
Like the <code>+=</code> operator, <code>-=</code> subtracts a number from a variable.
|
||||
<code>myVar = myVar - 5;</code>
|
||||
will subtract <code>5</code> from <code>myVar</code>. This can be rewritten as:
|
||||
<code>myVar -= 5;</code>
|
||||
</section>
|
||||
# --description--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Convert the assignments for <code>a</code>, <code>b</code>, and <code>c</code> to use the <code>-=</code> operator.
|
||||
</section>
|
||||
Like the `+=` operator, `-=` subtracts a number from a variable.
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
`myVar = myVar - 5;`
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>a</code> should equal <code>5</code>.
|
||||
testString: assert(a === 5);
|
||||
- text: <code>b</code> should equal <code>-6</code>.
|
||||
testString: assert(b === -6);
|
||||
- text: <code>c</code> should equal <code>2</code>.
|
||||
testString: assert(c === 2);
|
||||
- text: You should use the <code>-=</code> operator for each variable.
|
||||
testString: assert(code.match(/-=/g).length === 3);
|
||||
- text: You should not modify the code above the specified comment.
|
||||
testString: assert(/var a = 11;/.test(code) && /var b = 9;/.test(code) && /var c = 3;/.test(code));
|
||||
will subtract `5` from `myVar`. This can be rewritten as:
|
||||
|
||||
`myVar -= 5;`
|
||||
|
||||
# --instructions--
|
||||
|
||||
Convert the assignments for `a`, `b`, and `c` to use the `-=` operator.
|
||||
|
||||
# --hints--
|
||||
|
||||
`a` should equal `5`.
|
||||
|
||||
```js
|
||||
assert(a === 5);
|
||||
```
|
||||
|
||||
</section>
|
||||
`b` should equal `-6`.
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
```js
|
||||
assert(b === -6);
|
||||
```
|
||||
|
||||
<div id='js-seed'>
|
||||
`c` should equal `2`.
|
||||
|
||||
```js
|
||||
assert(c === 2);
|
||||
```
|
||||
|
||||
You should use the `-=` operator for each variable.
|
||||
|
||||
```js
|
||||
assert(code.match(/-=/g).length === 3);
|
||||
```
|
||||
|
||||
You should not modify the code above the specified comment.
|
||||
|
||||
```js
|
||||
assert(
|
||||
/var a = 11;/.test(code) && /var b = 9;/.test(code) && /var c = 3;/.test(code)
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
(function(a,b,c){ return "a = " + a + ", b = " + b + ", c = " + c; })(a,b,c);
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
var a = 11;
|
||||
@@ -55,23 +75,7 @@ b = b - 15;
|
||||
c = c - 1;
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
(function(a,b,c){ return "a = " + a + ", b = " + b + ", c = " + c; })(a,b,c);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
var a = 11;
|
||||
@@ -81,8 +85,4 @@ var c = 3;
|
||||
a -= 6;
|
||||
b -= 15;
|
||||
c -= 1;
|
||||
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@@ -6,16 +6,18 @@ videoUrl: 'https://scrimba.com/c/cNpM8AN'
|
||||
forumTopicId: 16802
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
In JavaScript, when the <code>+</code> operator is used with a <code>String</code> value, it is called the <dfn>concatenation</dfn> operator. You can build a new string out of other strings by <dfn>concatenating</dfn> them together.
|
||||
<strong>Example</strong>
|
||||
# --description--
|
||||
|
||||
In JavaScript, when the `+` operator is used with a `String` value, it is called the <dfn>concatenation</dfn> operator. You can build a new string out of other strings by <dfn>concatenating</dfn> them together.
|
||||
|
||||
**Example**
|
||||
|
||||
```js
|
||||
'My name is Alan,' + ' I concatenate.'
|
||||
```
|
||||
|
||||
<strong>Note</strong><br>Watch out for spaces. Concatenation does not add spaces between concatenated strings, so you'll need to add them yourself.
|
||||
**Note**
|
||||
Watch out for spaces. Concatenation does not add spaces between concatenated strings, so you'll need to add them yourself.
|
||||
|
||||
Example:
|
||||
|
||||
@@ -24,46 +26,39 @@ var ourStr = "I come first. " + "I come second.";
|
||||
// ourStr is "I come first. I come second."
|
||||
```
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Build <code>myStr</code> from the strings <code>"This is the start. "</code> and <code>"This is the end."</code> using the <code>+</code> operator.
|
||||
</section>
|
||||
Build `myStr` from the strings `"This is the start. "` and `"This is the end."` using the `+` operator.
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>myStr</code> should have a value of <code>This is the start. This is the end.</code>
|
||||
testString: assert(myStr === "This is the start. This is the end.");
|
||||
- text: You should use the <code>+</code> operator to build <code>myStr</code>.
|
||||
testString: assert(code.match(/(["']).*\1\s*\+\s*(["']).*\2/g));
|
||||
- text: <code>myStr</code> should be created using the <code>var</code> keyword.
|
||||
testString: assert(/var\s+myStr/.test(code));
|
||||
- text: You should assign the result to the <code>myStr</code> variable.
|
||||
testString: assert(/myStr\s*=/.test(code));
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`myStr` should have a value of `This is the start. This is the end.`
|
||||
|
||||
```js
|
||||
var myStr; // Change this line
|
||||
|
||||
assert(myStr === 'This is the start. This is the end.');
|
||||
```
|
||||
|
||||
</div>
|
||||
You should use the `+` operator to build `myStr`.
|
||||
|
||||
```js
|
||||
assert(code.match(/(["']).*\1\s*\+\s*(["']).*\2/g));
|
||||
```
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
`myStr` should be created using the `var` keyword.
|
||||
|
||||
```js
|
||||
assert(/var\s+myStr/.test(code));
|
||||
```
|
||||
|
||||
You should assign the result to the `myStr` variable.
|
||||
|
||||
```js
|
||||
assert(/myStr\s*=/.test(code));
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
(function(){
|
||||
@@ -75,16 +70,14 @@ var myStr; // Change this line
|
||||
})();
|
||||
```
|
||||
|
||||
</div>
|
||||
## --seed-contents--
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
```js
|
||||
var myStr; // Change this line
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
var myStr = "This is the start. " + "This is the end.";
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@@ -6,10 +6,12 @@ videoUrl: 'https://scrimba.com/c/cbQmmC4'
|
||||
forumTopicId: 16803
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
We can also use the <code>+=</code> operator to <dfn>concatenate</dfn> a string onto the end of an existing string variable. This can be very helpful to break a long string over several lines.
|
||||
<strong>Note</strong><br>Watch out for spaces. Concatenation does not add spaces between concatenated strings, so you'll need to add them yourself.
|
||||
# --description--
|
||||
|
||||
We can also use the `+=` operator to <dfn>concatenate</dfn> a string onto the end of an existing string variable. This can be very helpful to break a long string over several lines.
|
||||
|
||||
**Note**
|
||||
Watch out for spaces. Concatenation does not add spaces between concatenated strings, so you'll need to add them yourself.
|
||||
|
||||
Example:
|
||||
|
||||
@@ -19,46 +21,27 @@ ourStr += "I come second.";
|
||||
// ourStr is now "I come first. I come second."
|
||||
```
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Build <code>myStr</code> over several lines by concatenating these two strings: <code>"This is the first sentence. "</code> and <code>"This is the second sentence."</code> using the <code>+=</code> operator. Use the <code>+=</code> operator similar to how it is shown in the editor. Start by assigning the first string to <code>myStr</code>, then add on the second string.
|
||||
</section>
|
||||
Build `myStr` over several lines by concatenating these two strings: `"This is the first sentence. "` and `"This is the second sentence."` using the `+=` operator. Use the `+=` operator similar to how it is shown in the editor. Start by assigning the first string to `myStr`, then add on the second string.
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>myStr</code> should have a value of <code>This is the first sentence. This is the second sentence.</code>
|
||||
testString: assert(myStr === "This is the first sentence. This is the second sentence.");
|
||||
- text: You should use the <code>+=</code> operator to build <code>myStr</code>.
|
||||
testString: assert(code.match(/myStr\s*\+=\s*(["']).*\1/g));
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`myStr` should have a value of `This is the first sentence. This is the second sentence.`
|
||||
|
||||
```js
|
||||
|
||||
// Only change code below this line
|
||||
|
||||
var myStr;
|
||||
|
||||
|
||||
assert(myStr === 'This is the first sentence. This is the second sentence.');
|
||||
```
|
||||
|
||||
</div>
|
||||
You should use the `+=` operator to build `myStr`.
|
||||
|
||||
```js
|
||||
assert(code.match(/myStr\s*\+=\s*(["']).*\1/g));
|
||||
```
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
(function(){
|
||||
@@ -70,17 +53,17 @@ var myStr;
|
||||
})();
|
||||
```
|
||||
|
||||
</div>
|
||||
## --seed-contents--
|
||||
|
||||
</section>
|
||||
```js
|
||||
// Only change code below this line
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
var myStr;
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
var myStr = "This is the first sentence. ";
|
||||
myStr += "This is the second sentence.";
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@@ -6,9 +6,9 @@ videoUrl: 'https://scrimba.com/c/cqk8rf4'
|
||||
forumTopicId: 16805
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
Sometimes you will need to build a string, <a href="https://en.wikipedia.org/wiki/Mad_Libs" target="_blank">Mad Libs</a> style. By using the concatenation operator (<code>+</code>), you can insert one or more variables into a string you're building.
|
||||
# --description--
|
||||
|
||||
Sometimes you will need to build a string, [Mad Libs](https://en.wikipedia.org/wiki/Mad_Libs) style. By using the concatenation operator (`+`), you can insert one or more variables into a string you're building.
|
||||
|
||||
Example:
|
||||
|
||||
@@ -18,45 +18,27 @@ var ourStr = "Hello, our name is " + ourName + ", how are you?";
|
||||
// ourStr is now "Hello, our name is freeCodeCamp, how are you?"
|
||||
```
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Set <code>myName</code> to a string equal to your name and build <code>myStr</code> with <code>myName</code> between the strings <code>"My name is "</code> and <code>" and I am well!"</code>
|
||||
</section>
|
||||
Set `myName` to a string equal to your name and build `myStr` with `myName` between the strings `"My name is "` and `" and I am well!"`
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>myName</code> should be set to a string at least 3 characters long.
|
||||
testString: assert(typeof myName !== 'undefined' && myName.length > 2);
|
||||
- text: You should use two <code>+</code> operators to build <code>myStr</code> with <code>myName</code> inside it.
|
||||
testString: assert(code.match(/["']\s*\+\s*myName\s*\+\s*["']/g).length > 0);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`myName` should be set to a string at least 3 characters long.
|
||||
|
||||
```js
|
||||
// Only change code below this line
|
||||
var myName;
|
||||
var myStr;
|
||||
|
||||
|
||||
assert(typeof myName !== 'undefined' && myName.length > 2);
|
||||
```
|
||||
|
||||
</div>
|
||||
You should use two `+` operators to build `myStr` with `myName` inside it.
|
||||
|
||||
```js
|
||||
assert(code.match(/["']\s*\+\s*myName\s*\+\s*["']/g).length > 0);
|
||||
```
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
(function(){
|
||||
@@ -75,17 +57,17 @@ var myStr;
|
||||
})();
|
||||
```
|
||||
|
||||
</div>
|
||||
## --seed-contents--
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
```js
|
||||
// Only change code below this line
|
||||
var myName;
|
||||
var myStr;
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
var myName = "Bob";
|
||||
var myStr = "My name is " + myName + " and I am well!";
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@@ -6,11 +6,13 @@ videoUrl: 'https://scrimba.com/c/c2R6BHa'
|
||||
forumTopicId: 16808
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
# --description--
|
||||
|
||||
A for loop can also count backwards, so long as we can define the right conditions.
|
||||
In order to count backwards by twos, we'll need to change our <code>initialization</code>, <code>condition</code>, and <code>final-expression</code>.
|
||||
We'll start at <code>i = 10</code> and loop while <code>i > 0</code>. We'll decrement <code>i</code> by 2 each loop with <code>i -= 2</code>.
|
||||
|
||||
In order to count backwards by twos, we'll need to change our `initialization`, `condition`, and `final-expression`.
|
||||
|
||||
We'll start at `i = 10` and loop while `i > 0`. We'll decrement `i` by 2 each loop with `i -= 2`.
|
||||
|
||||
```js
|
||||
var ourArray = [];
|
||||
@@ -19,62 +21,50 @@ for (var i = 10; i > 0; i -= 2) {
|
||||
}
|
||||
```
|
||||
|
||||
<code>ourArray</code> will now contain <code>[10,8,6,4,2]</code>.
|
||||
Let's change our <code>initialization</code> and <code>final-expression</code> so we can count backward by twos by odd numbers.
|
||||
</section>
|
||||
`ourArray` will now contain `[10,8,6,4,2]`. Let's change our `initialization` and `final-expression` so we can count backward by twos by odd numbers.
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Push the odd numbers from 9 through 1 to <code>myArray</code> using a <code>for</code> loop.
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
Push the odd numbers from 9 through 1 to `myArray` using a `for` loop.
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: You should be using a <code>for</code> loop for this.
|
||||
testString: assert(/for\s*\([^)]+?\)/.test(code));
|
||||
- text: You should be using the array method <code>push</code>.
|
||||
testString: assert(code.match(/myArray.push/));
|
||||
- text: <code>myArray</code> should equal <code>[9,7,5,3,1]</code>.
|
||||
testString: assert.deepEqual(myArray, [9,7,5,3,1]);
|
||||
# --hints--
|
||||
|
||||
You should be using a `for` loop for this.
|
||||
|
||||
```js
|
||||
assert(/for\s*\([^)]+?\)/.test(code));
|
||||
```
|
||||
|
||||
</section>
|
||||
You should be using the array method `push`.
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
```js
|
||||
assert(code.match(/myArray.push/));
|
||||
```
|
||||
|
||||
<div id='js-seed'>
|
||||
`myArray` should equal `[9,7,5,3,1]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(myArray, [9, 7, 5, 3, 1]);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
if(typeof myArray !== "undefined"){(function(){return myArray;})();}
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
// Setup
|
||||
var myArray = [];
|
||||
|
||||
// Only change code below this line
|
||||
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
if(typeof myArray !== "undefined"){(function(){return myArray;})();}
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
var myArray = [];
|
||||
@@ -82,5 +72,3 @@ for (var i = 9; i > 0; i -= 2) {
|
||||
myArray.push(i);
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@@ -6,49 +6,159 @@ videoUrl: 'https://scrimba.com/c/c6KE7ty'
|
||||
forumTopicId: 16809
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
In the casino game Blackjack, a player can gain an advantage over the house by keeping track of the relative number of high and low cards remaining in the deck. This is called <a href='https://en.wikipedia.org/wiki/Card_counting' target='_blank'>Card Counting</a>.
|
||||
# --description--
|
||||
|
||||
In the casino game Blackjack, a player can gain an advantage over the house by keeping track of the relative number of high and low cards remaining in the deck. This is called [Card Counting](https://en.wikipedia.org/wiki/Card_counting).
|
||||
|
||||
Having more high cards remaining in the deck favors the player. Each card is assigned a value according to the table below. When the count is positive, the player should bet high. When the count is zero or negative, the player should bet low.
|
||||
<table class="table table-striped"><thead><tr><th>Count Change</th><th>Cards</th></tr></thead><tbody><tr><td>+1</td><td>2, 3, 4, 5, 6</td></tr><tr><td>0</td><td>7, 8, 9</td></tr><tr><td>-1</td><td>10, 'J', 'Q', 'K', 'A'</td></tr></tbody></table>
|
||||
You will write a card counting function. It will receive a <code>card</code> parameter, which can be a number or a string, and increment or decrement the global <code>count</code> variable according to the card's value (see table). The function will then return a string with the current count and the string <code>Bet</code> if the count is positive, or <code>Hold</code> if the count is zero or negative. The current count and the player's decision (<code>Bet</code> or <code>Hold</code>) should be separated by a single space.
|
||||
<strong>Example Output</strong><br><code>-3 Hold</code><br><code>5 Bet</code>
|
||||
<strong>Hint</strong><br>Do NOT reset <code>count</code> to 0 when value is 7, 8, or 9.<br>Do NOT return an array.<br>Do NOT include quotes (single or double) in the output.
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
<table class='table table-striped'><thead><tr><th>Count Change</th><th>Cards</th></tr></thead><tbody><tr><td>+1</td><td>2, 3, 4, 5, 6</td></tr><tr><td>0</td><td>7, 8, 9</td></tr><tr><td>-1</td><td>10, 'J', 'Q', 'K', 'A'</td></tr></tbody></table>
|
||||
|
||||
</section>
|
||||
You will write a card counting function. It will receive a `card` parameter, which can be a number or a string, and increment or decrement the global `count` variable according to the card's value (see table). The function will then return a string with the current count and the string `Bet` if the count is positive, or `Hold` if the count is zero or negative. The current count and the player's decision (`Bet` or `Hold`) should be separated by a single space.
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
**Example Output**
|
||||
`-3 Hold`
|
||||
`5 Bet`
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: Cards Sequence 2, 3, 4, 5, 6 should return <code>5 Bet</code>
|
||||
testString: assert((function(){ count = 0; cc(2);cc(3);cc(4);cc(5);var out = cc(6); if(out === "5 Bet") {return true;} return false; })());
|
||||
- text: Cards Sequence 7, 8, 9 should return <code>0 Hold</code>
|
||||
testString: assert((function(){ count = 0; cc(7);cc(8);var out = cc(9); if(out === "0 Hold") {return true;} return false; })());
|
||||
- text: Cards Sequence 10, J, Q, K, A should return <code>-5 Hold</code>
|
||||
testString: assert((function(){ count = 0; cc(10);cc('J');cc('Q');cc('K');var out = cc('A'); if(out === "-5 Hold") {return true;} return false; })());
|
||||
- text: Cards Sequence 3, 7, Q, 8, A should return <code>-1 Hold</code>
|
||||
testString: assert((function(){ count = 0; cc(3);cc(7);cc('Q');cc(8);var out = cc('A'); if(out === "-1 Hold") {return true;} return false; })());
|
||||
- text: Cards Sequence 2, J, 9, 2, 7 should return <code>1 Bet</code>
|
||||
testString: assert((function(){ count = 0; cc(2);cc('J');cc(9);cc(2);var out = cc(7); if(out === "1 Bet") {return true;} return false; })());
|
||||
- text: Cards Sequence 2, 2, 10 should return <code>1 Bet</code>
|
||||
testString: assert((function(){ count = 0; cc(2);cc(2);var out = cc(10); if(out === "1 Bet") {return true;} return false; })());
|
||||
- text: Cards Sequence 3, 2, A, 10, K should return <code>-1 Hold</code>
|
||||
testString: assert((function(){ count = 0; cc(3);cc(2);cc('A');cc(10);var out = cc('K'); if(out === "-1 Hold") {return true;} return false; })());
|
||||
**Hint**
|
||||
Do NOT reset `count` to 0 when value is 7, 8, or 9.
|
||||
Do NOT return an array.
|
||||
Do NOT include quotes (single or double) in the output.
|
||||
|
||||
# --hints--
|
||||
|
||||
Cards Sequence 2, 3, 4, 5, 6 should return `5 Bet`
|
||||
|
||||
```js
|
||||
assert(
|
||||
(function () {
|
||||
count = 0;
|
||||
cc(2);
|
||||
cc(3);
|
||||
cc(4);
|
||||
cc(5);
|
||||
var out = cc(6);
|
||||
if (out === '5 Bet') {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
})()
|
||||
);
|
||||
```
|
||||
|
||||
</section>
|
||||
Cards Sequence 7, 8, 9 should return `0 Hold`
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
```js
|
||||
assert(
|
||||
(function () {
|
||||
count = 0;
|
||||
cc(7);
|
||||
cc(8);
|
||||
var out = cc(9);
|
||||
if (out === '0 Hold') {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
})()
|
||||
);
|
||||
```
|
||||
|
||||
<div id='js-seed'>
|
||||
Cards Sequence 10, J, Q, K, A should return `-5 Hold`
|
||||
|
||||
```js
|
||||
assert(
|
||||
(function () {
|
||||
count = 0;
|
||||
cc(10);
|
||||
cc('J');
|
||||
cc('Q');
|
||||
cc('K');
|
||||
var out = cc('A');
|
||||
if (out === '-5 Hold') {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
})()
|
||||
);
|
||||
```
|
||||
|
||||
Cards Sequence 3, 7, Q, 8, A should return `-1 Hold`
|
||||
|
||||
```js
|
||||
assert(
|
||||
(function () {
|
||||
count = 0;
|
||||
cc(3);
|
||||
cc(7);
|
||||
cc('Q');
|
||||
cc(8);
|
||||
var out = cc('A');
|
||||
if (out === '-1 Hold') {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
})()
|
||||
);
|
||||
```
|
||||
|
||||
Cards Sequence 2, J, 9, 2, 7 should return `1 Bet`
|
||||
|
||||
```js
|
||||
assert(
|
||||
(function () {
|
||||
count = 0;
|
||||
cc(2);
|
||||
cc('J');
|
||||
cc(9);
|
||||
cc(2);
|
||||
var out = cc(7);
|
||||
if (out === '1 Bet') {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
})()
|
||||
);
|
||||
```
|
||||
|
||||
Cards Sequence 2, 2, 10 should return `1 Bet`
|
||||
|
||||
```js
|
||||
assert(
|
||||
(function () {
|
||||
count = 0;
|
||||
cc(2);
|
||||
cc(2);
|
||||
var out = cc(10);
|
||||
if (out === '1 Bet') {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
})()
|
||||
);
|
||||
```
|
||||
|
||||
Cards Sequence 3, 2, A, 10, K should return `-1 Hold`
|
||||
|
||||
```js
|
||||
assert(
|
||||
(function () {
|
||||
count = 0;
|
||||
cc(3);
|
||||
cc(2);
|
||||
cc('A');
|
||||
cc(10);
|
||||
var out = cc('K');
|
||||
if (out === '-1 Hold') {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
})()
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
var count = 0;
|
||||
@@ -64,15 +174,7 @@ function cc(card) {
|
||||
cc(2); cc(3); cc(7); cc('K'); cc('A');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
var count = 0;
|
||||
@@ -99,5 +201,3 @@ function cc(card) {
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@@ -6,64 +6,49 @@ videoUrl: 'https://scrimba.com/c/ca8GEuW'
|
||||
forumTopicId: 16826
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
# --description--
|
||||
|
||||
We can store decimal numbers in variables too. Decimal numbers are sometimes referred to as <dfn>floating point</dfn> numbers or <dfn>floats</dfn>.
|
||||
<strong>Note</strong><br>Not all real numbers can accurately be represented in <dfn>floating point</dfn>. This can lead to rounding errors. <a href="https://en.wikipedia.org/wiki/Floating_point#Accuracy_problems" target="_blank">Details Here</a>.
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Create a variable <code>myDecimal</code> and give it a decimal value with a fractional part (e.g. <code>5.7</code>).
|
||||
</section>
|
||||
**Note**
|
||||
Not all real numbers can accurately be represented in <dfn>floating point</dfn>. This can lead to rounding errors. [Details Here](https://en.wikipedia.org/wiki/Floating_point#Accuracy_problems).
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --instructions--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>myDecimal</code> should be a number.
|
||||
testString: assert(typeof myDecimal === "number");
|
||||
- text: <code>myDecimal</code> should have a decimal point
|
||||
testString: assert(myDecimal % 1 != 0);
|
||||
Create a variable `myDecimal` and give it a decimal value with a fractional part (e.g. `5.7`).
|
||||
|
||||
```
|
||||
# --hints--
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`myDecimal` should be a number.
|
||||
|
||||
```js
|
||||
var ourDecimal = 5.7;
|
||||
|
||||
// Only change code below this line
|
||||
|
||||
|
||||
assert(typeof myDecimal === 'number');
|
||||
```
|
||||
|
||||
</div>
|
||||
`myDecimal` should have a decimal point
|
||||
|
||||
```js
|
||||
assert(myDecimal % 1 != 0);
|
||||
```
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
(function(){if(typeof myDecimal !== "undefined"){return myDecimal;}})();
|
||||
```
|
||||
|
||||
</div>
|
||||
## --seed-contents--
|
||||
|
||||
</section>
|
||||
```js
|
||||
var ourDecimal = 5.7;
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
// Only change code below this line
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
var myDecimal = 9.9;
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@@ -6,70 +6,54 @@ videoUrl: 'https://scrimba.com/c/cNanrHq'
|
||||
forumTopicId: 17556
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
In computer science, <dfn>data</dfn> is anything that is meaningful to the computer. JavaScript provides eight different <dfn>data types</dfn> which are <code>undefined</code>, <code>null</code>, <code>boolean</code>, <code>string</code>, <code>symbol</code>, <code>bigint</code>, <code>number</code>, and <code>object</code>.
|
||||
For example, computers distinguish between numbers, such as the number <code>12</code>, and <code>strings</code>, such as <code>"12"</code>, <code>"dog"</code>, or <code>"123 cats"</code>, which are collections of characters. Computers can perform mathematical operations on a number, but not on a string.
|
||||
# --description--
|
||||
|
||||
In computer science, <dfn>data</dfn> is anything that is meaningful to the computer. JavaScript provides eight different <dfn>data types</dfn> which are `undefined`, `null`, `boolean`, `string`, `symbol`, `bigint`, `number`, and `object`.
|
||||
|
||||
For example, computers distinguish between numbers, such as the number `12`, and `strings`, such as `"12"`, `"dog"`, or `"123 cats"`, which are collections of characters. Computers can perform mathematical operations on a number, but not on a string.
|
||||
|
||||
<dfn>Variables</dfn> allow computers to store and manipulate data in a dynamic fashion. They do this by using a "label" to point to the data rather than using the data itself. Any of the eight data types may be stored in a variable.
|
||||
<code>Variables</code> are similar to the x and y variables you use in mathematics, which means they're a simple name to represent the data we want to refer to. Computer <code>variables</code> differ from mathematical variables in that they can store different values at different times.
|
||||
We tell JavaScript to create or <dfn>declare</dfn> a variable by putting the keyword <code>var</code> in front of it, like so:
|
||||
|
||||
`Variables` are similar to the x and y variables you use in mathematics, which means they're a simple name to represent the data we want to refer to. Computer `variables` differ from mathematical variables in that they can store different values at different times.
|
||||
|
||||
We tell JavaScript to create or <dfn>declare</dfn> a variable by putting the keyword `var` in front of it, like so:
|
||||
|
||||
```js
|
||||
var ourName;
|
||||
```
|
||||
|
||||
creates a <code>variable</code> called <code>ourName</code>. In JavaScript we end statements with semicolons.
|
||||
<code>Variable</code> names can be made up of numbers, letters, and <code>$</code> or <code>_</code>, but may not contain spaces or start with a number.
|
||||
</section>
|
||||
creates a `variable` called `ourName`. In JavaScript we end statements with semicolons. `Variable` names can be made up of numbers, letters, and `$` or `_`, but may not contain spaces or start with a number.
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Use the <code>var</code> keyword to create a variable called <code>myName</code>.
|
||||
<strong>Hint</strong><br>Look at the <code>ourName</code> example above if you get stuck.
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
Use the `var` keyword to create a variable called `myName`.
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: You should declare <code>myName</code> with the <code>var</code> keyword, ending with a semicolon
|
||||
testString: assert(/var\s+myName\s*;/.test(code));
|
||||
**Hint**
|
||||
Look at the `ourName` example above if you get stuck.
|
||||
|
||||
```
|
||||
# --hints--
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
You should declare `myName` with the `var` keyword, ending with a semicolon
|
||||
|
||||
```js
|
||||
|
||||
|
||||
assert(/var\s+myName\s*;/.test(code));
|
||||
```
|
||||
|
||||
</div>
|
||||
# --seed--
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
if(typeof myName !== "undefined"){(function(v){return v;})(myName);}
|
||||
```
|
||||
|
||||
</div>
|
||||
## --seed-contents--
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
```js
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
var myName;
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@@ -6,64 +6,72 @@ videoUrl: 'https://scrimba.com/c/c2QvWU6'
|
||||
forumTopicId: 17557
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
# --description--
|
||||
|
||||
Previously we have used the code
|
||||
<code>var myName = "your name";</code>
|
||||
<code>"your name"</code> is called a <dfn>string</dfn> <dfn>literal</dfn>. It is a string because it is a series of zero or more characters enclosed in single or double quotes.
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Create two new <code>string</code> variables: <code>myFirstName</code> and <code>myLastName</code> and assign them the values of your first and last name, respectively.
|
||||
</section>
|
||||
`var myName = "your name";`
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
`"your name"` is called a <dfn>string</dfn> <dfn>literal</dfn>. It is a string because it is a series of zero or more characters enclosed in single or double quotes.
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>myFirstName</code> should be a string with at least one character in it.
|
||||
testString: assert((function(){if(typeof myFirstName !== "undefined" && typeof myFirstName === "string" && myFirstName.length > 0){return true;}else{return false;}})());
|
||||
- text: <code>myLastName</code> should be a string with at least one character in it.
|
||||
testString: assert((function(){if(typeof myLastName !== "undefined" && typeof myLastName === "string" && myLastName.length > 0){return true;}else{return false;}})());
|
||||
# --instructions--
|
||||
|
||||
```
|
||||
Create two new `string` variables: `myFirstName` and `myLastName` and assign them the values of your first and last name, respectively.
|
||||
|
||||
</section>
|
||||
# --hints--
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`myFirstName` should be a string with at least one character in it.
|
||||
|
||||
```js
|
||||
|
||||
|
||||
|
||||
assert(
|
||||
(function () {
|
||||
if (
|
||||
typeof myFirstName !== 'undefined' &&
|
||||
typeof myFirstName === 'string' &&
|
||||
myFirstName.length > 0
|
||||
) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
})()
|
||||
);
|
||||
```
|
||||
|
||||
</div>
|
||||
`myLastName` should be a string with at least one character in it.
|
||||
|
||||
```js
|
||||
assert(
|
||||
(function () {
|
||||
if (
|
||||
typeof myLastName !== 'undefined' &&
|
||||
typeof myLastName === 'string' &&
|
||||
myLastName.length > 0
|
||||
) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
})()
|
||||
);
|
||||
```
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
if(typeof myFirstName !== "undefined" && typeof myLastName !== "undefined"){(function(){return myFirstName + ', ' + myLastName;})();}
|
||||
```
|
||||
|
||||
</div>
|
||||
## --seed-contents--
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
```js
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
var myFirstName = "Alan";
|
||||
var myLastName = "Turing";
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@@ -6,72 +6,71 @@ videoUrl: 'https://scrimba.com/c/cM2KeS2'
|
||||
forumTopicId: 17558
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
You can easily <dfn>decrement</dfn> or decrease a variable by one with the <code>--</code> operator.
|
||||
<code>i--;</code>
|
||||
# --description--
|
||||
|
||||
You can easily <dfn>decrement</dfn> or decrease a variable by one with the `--` operator.
|
||||
|
||||
`i--;`
|
||||
|
||||
is the equivalent of
|
||||
<code>i = i - 1;</code>
|
||||
<strong>Note</strong><br>The entire line becomes <code>i--;</code>, eliminating the need for the equal sign.
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Change the code to use the <code>--</code> operator on <code>myVar</code>.
|
||||
</section>
|
||||
`i = i - 1;`
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
**Note**
|
||||
The entire line becomes `i--;`, eliminating the need for the equal sign.
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>myVar</code> should equal <code>10</code>.
|
||||
testString: assert(myVar === 10);
|
||||
- text: <code>myVar = myVar - 1;</code> should be changed.
|
||||
testString: assert(/var\s*myVar\s*=\s*11;\s*\/*.*\s*([-]{2}\s*myVar|myVar\s*[-]{2});/.test(code));
|
||||
- text: You should use the <code>--</code> operator on <code>myVar</code>.
|
||||
testString: assert(/[-]{2}\s*myVar|myVar\s*[-]{2}/.test(code));
|
||||
- text: You should not change code above the specified comment.
|
||||
testString: assert(/var myVar = 11;/.test(code));
|
||||
# --instructions--
|
||||
|
||||
Change the code to use the `--` operator on `myVar`.
|
||||
|
||||
# --hints--
|
||||
|
||||
`myVar` should equal `10`.
|
||||
|
||||
```js
|
||||
assert(myVar === 10);
|
||||
```
|
||||
|
||||
</section>
|
||||
`myVar = myVar - 1;` should be changed.
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
```js
|
||||
assert(
|
||||
/var\s*myVar\s*=\s*11;\s*\/*.*\s*([-]{2}\s*myVar|myVar\s*[-]{2});/.test(code)
|
||||
);
|
||||
```
|
||||
|
||||
<div id='js-seed'>
|
||||
You should use the `--` operator on `myVar`.
|
||||
|
||||
```js
|
||||
assert(/[-]{2}\s*myVar|myVar\s*[-]{2}/.test(code));
|
||||
```
|
||||
|
||||
You should not change code above the specified comment.
|
||||
|
||||
```js
|
||||
assert(/var myVar = 11;/.test(code));
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
(function(z){return 'myVar = ' + z;})(myVar);
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
var myVar = 11;
|
||||
|
||||
// Only change code below this line
|
||||
myVar = myVar - 1;
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
(function(z){return 'myVar = ' + z;})(myVar);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
var myVar = 11;
|
||||
myVar--;
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@@ -6,10 +6,11 @@ videoUrl: 'https://scrimba.com/c/cDqKdTv'
|
||||
forumTopicId: 17560
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
# --description--
|
||||
|
||||
We can also delete properties from objects like this:
|
||||
<code>delete ourDog.bark;</code>
|
||||
|
||||
`delete ourDog.bark;`
|
||||
|
||||
Example:
|
||||
|
||||
@@ -25,7 +26,7 @@ var ourDog = {
|
||||
delete ourDog.bark;
|
||||
```
|
||||
|
||||
After the last line shown above, <code>ourDog</code> looks like:
|
||||
After the last line shown above, `ourDog` looks like:
|
||||
|
||||
```js
|
||||
{
|
||||
@@ -36,31 +37,33 @@ After the last line shown above, <code>ourDog</code> looks like:
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Delete the <code>"tails"</code> property from <code>myDog</code>. You may use either dot or bracket notation.
|
||||
</section>
|
||||
Delete the `"tails"` property from `myDog`. You may use either dot or bracket notation.
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: You should delete the property <code>"tails"</code> from <code>myDog</code>.
|
||||
testString: assert(typeof myDog === "object" && myDog.tails === undefined);
|
||||
- text: You should not modify the <code>myDog</code> setup.
|
||||
testString: 'assert(code.match(/"tails": 1/g).length > 0);'
|
||||
You should delete the property `"tails"` from `myDog`.
|
||||
|
||||
```js
|
||||
assert(typeof myDog === 'object' && myDog.tails === undefined);
|
||||
```
|
||||
|
||||
</section>
|
||||
You should not modify the `myDog` setup.
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
```js
|
||||
assert(code.match(/"tails": 1/g).length > 0);
|
||||
```
|
||||
|
||||
<div id='js-seed'>
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
(function(z){return z;})(myDog);
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
// Setup
|
||||
@@ -73,27 +76,9 @@ var myDog = {
|
||||
};
|
||||
|
||||
// Only change code below this line
|
||||
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
(function(z){return z;})(myDog);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
var myDog = {
|
||||
@@ -105,5 +90,3 @@ var myDog = {
|
||||
};
|
||||
delete myDog.tails;
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@@ -6,60 +6,50 @@ videoUrl: 'https://scrimba.com/c/cBZe9AW'
|
||||
forumTopicId: 18255
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
# --description--
|
||||
|
||||
Now let's divide one decimal by another.
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Change the <code>0.0</code> so that <code>quotient</code> will equal to <code>2.2</code>.
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
Change the `0.0` so that `quotient` will equal to `2.2`.
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: The variable <code>quotient</code> should equal <code>2.2</code>
|
||||
testString: assert(quotient === 2.2);
|
||||
- text: You should use the <code>/</code> operator to divide 4.4 by 2
|
||||
testString: assert(/4\.40*\s*\/\s*2\.*0*/.test(code));
|
||||
- text: The quotient variable should only be assigned once
|
||||
testString: assert(code.match(/quotient/g).length === 1);
|
||||
# --hints--
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
The variable `quotient` should equal `2.2`
|
||||
|
||||
```js
|
||||
var quotient = 0.0 / 2.0; // Change this line
|
||||
assert(quotient === 2.2);
|
||||
```
|
||||
|
||||
</div>
|
||||
You should use the `/` operator to divide 4.4 by 2
|
||||
|
||||
```js
|
||||
assert(/4\.40*\s*\/\s*2\.*0*/.test(code));
|
||||
```
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
The quotient variable should only be assigned once
|
||||
|
||||
```js
|
||||
assert(code.match(/quotient/g).length === 1);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
(function(y){return 'quotient = '+y;})(quotient);
|
||||
```
|
||||
|
||||
</div>
|
||||
## --seed-contents--
|
||||
|
||||
</section>
|
||||
```js
|
||||
var quotient = 0.0 / 2.0; // Change this line
|
||||
```
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
var quotient = 4.4 / 2.0;
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@@ -6,70 +6,52 @@ videoUrl: 'https://scrimba.com/c/cqkbdAr'
|
||||
forumTopicId: 17566
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
We can also divide one number by another.
|
||||
JavaScript uses the <code>/</code> symbol for division.
|
||||
# --description--
|
||||
|
||||
<strong>Example</strong>
|
||||
We can also divide one number by another.
|
||||
|
||||
JavaScript uses the `/` symbol for division.
|
||||
|
||||
**Example**
|
||||
|
||||
```js
|
||||
myVar = 16 / 2; // assigned 8
|
||||
```
|
||||
|
||||
# --instructions--
|
||||
|
||||
</section>
|
||||
Change the `0` so that the `quotient` is equal to `2`.
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Change the <code>0</code> so that the <code>quotient</code> is equal to <code>2</code>.
|
||||
</section>
|
||||
# --hints--
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: The variable <code>quotient</code> should be equal to 2.
|
||||
testString: assert(quotient === 2);
|
||||
- text: You should use the <code>/</code> operator.
|
||||
testString: assert(/\d+\s*\/\s*\d+/.test(code));
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
The variable `quotient` should be equal to 2.
|
||||
|
||||
```js
|
||||
var quotient = 66 / 0;
|
||||
|
||||
|
||||
assert(quotient === 2);
|
||||
```
|
||||
|
||||
</div>
|
||||
You should use the `/` operator.
|
||||
|
||||
```js
|
||||
assert(/\d+\s*\/\s*\d+/.test(code));
|
||||
```
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
(function(z){return 'quotient = '+z;})(quotient);
|
||||
```
|
||||
|
||||
</div>
|
||||
## --seed-contents--
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
```js
|
||||
var quotient = 66 / 0;
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
var quotient = 66 / 33;
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@@ -6,62 +6,80 @@ videoUrl: 'https://scrimba.com/c/cvmqRh6'
|
||||
forumTopicId: 17567
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
Quotes are not the only characters that can be <dfn>escaped</dfn> inside a string. There are two reasons to use escaping characters:<ol><li>To allow you to use characters you may not otherwise be able to type out, such as a carriage return.</li><li>To allow you to represent multiple quotes in a string without JavaScript misinterpreting what you mean.</li></ol>We learned this in the previous challenge.
|
||||
<table class="table table-striped"><thead><tr><th>Code</th><th>Output</th></tr></thead><tbody><tr><td><code>\'</code></td><td>single quote</td></tr><tr><td><code>\"</code></td><td>double quote</td></tr><tr><td><code>\\</code></td><td>backslash</td></tr><tr><td><code>\n</code></td><td>newline</td></tr><tr><td><code>\r</code></td><td>carriage return</td></tr><tr><td><code>\t</code></td><td>tab</td></tr><tr><td><code>\b</code></td><td>word boundary</td></tr><tr><td><code>\f</code></td><td>form feed</td></tr></tbody></table>
|
||||
<em>Note that the backslash itself must be escaped in order to display as a backslash.</em>
|
||||
</section>
|
||||
# --description--
|
||||
|
||||
Quotes are not the only characters that can be <dfn>escaped</dfn> inside a string. There are two reasons to use escaping characters:
|
||||
|
||||
1. To allow you to use characters you may not otherwise be able to type out, such as a carriage return.
|
||||
2. To allow you to represent multiple quotes in a string without JavaScript misinterpreting what you mean.
|
||||
|
||||
We learned this in the previous challenge.
|
||||
|
||||
<table class='table table-striped'><thead><tr><th>Code</th><th>Output</th></tr></thead><tbody><tr><td><code>\'</code></td><td>single quote</td></tr><tr><td><code>\"</code></td><td>double quote</td></tr><tr><td><code>\\</code></td><td>backslash</td></tr><tr><td><code>\n</code></td><td>newline</td></tr><tr><td><code>\r</code></td><td>carriage return</td></tr><tr><td><code>\t</code></td><td>tab</td></tr><tr><td><code>\b</code></td><td>word boundary</td></tr><tr><td><code>\f</code></td><td>form feed</td></tr></tbody></table>
|
||||
|
||||
*Note that the backslash itself must be escaped in order to display as a backslash.*
|
||||
|
||||
# --instructions--
|
||||
|
||||
Assign the following three lines of text into the single variable `myStr` using escape sequences.
|
||||
|
||||
<blockquote>FirstLine<br> \SecondLine<br>ThirdLine</blockquote>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Assign the following three lines of text into the single variable <code>myStr</code> using escape sequences.
|
||||
<blockquote>FirstLine<br/> \SecondLine<br/>ThirdLine</blockquote>
|
||||
You will need to use escape sequences to insert special characters correctly. You will also need to follow the spacing as it looks above, with no spaces between escape sequences or words.
|
||||
|
||||
Here is the text with the escape sequences written out.
|
||||
<q>FirstLine<code>newline</code><code>tab</code><code>backslash</code>SecondLine<code>newline</code>ThirdLine</q>
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
"FirstLine```newline``tab``backslash```SecondLine`newline`ThirdLine"
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>myStr</code> should not contain any spaces
|
||||
testString: assert(!/ /.test(myStr));
|
||||
- text: <code>myStr</code> should contain the strings <code>FirstLine</code>, <code>SecondLine</code> and <code>ThirdLine</code> (remember case sensitivity)
|
||||
testString: assert(/FirstLine/.test(myStr) && /SecondLine/.test(myStr) && /ThirdLine/.test(myStr));
|
||||
- text: <code>FirstLine</code> should be followed by the newline character <code>\n</code>
|
||||
testString: assert(/FirstLine\n/.test(myStr));
|
||||
- text: <code>myStr</code> should contain a tab character <code>\t</code> which follows a newline character
|
||||
testString: assert(/\n\t/.test(myStr));
|
||||
- text: <code>SecondLine</code> should be preceded by the backslash character <code>\</code>
|
||||
testString: assert(/\\SecondLine/.test(myStr));
|
||||
- text: There should be a newline character between <code>SecondLine</code> and <code>ThirdLine</code>
|
||||
testString: assert(/SecondLine\nThirdLine/.test(myStr));
|
||||
- text: <code>myStr</code> should only contain characters shown in the instructions
|
||||
testString: assert(myStr === 'FirstLine\n\t\\SecondLine\nThirdLine');
|
||||
# --hints--
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`myStr` should not contain any spaces
|
||||
|
||||
```js
|
||||
var myStr; // Change this line
|
||||
|
||||
|
||||
assert(!/ /.test(myStr));
|
||||
```
|
||||
|
||||
</div>
|
||||
`myStr` should contain the strings `FirstLine`, `SecondLine` and `ThirdLine` (remember case sensitivity)
|
||||
|
||||
```js
|
||||
assert(
|
||||
/FirstLine/.test(myStr) && /SecondLine/.test(myStr) && /ThirdLine/.test(myStr)
|
||||
);
|
||||
```
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
`FirstLine` should be followed by the newline character `\n`
|
||||
|
||||
```js
|
||||
assert(/FirstLine\n/.test(myStr));
|
||||
```
|
||||
|
||||
`myStr` should contain a tab character `\t` which follows a newline character
|
||||
|
||||
```js
|
||||
assert(/\n\t/.test(myStr));
|
||||
```
|
||||
|
||||
`SecondLine` should be preceded by the backslash character `\`
|
||||
|
||||
```js
|
||||
assert(/\\SecondLine/.test(myStr));
|
||||
```
|
||||
|
||||
There should be a newline character between `SecondLine` and `ThirdLine`
|
||||
|
||||
```js
|
||||
assert(/SecondLine\nThirdLine/.test(myStr));
|
||||
```
|
||||
|
||||
`myStr` should only contain characters shown in the instructions
|
||||
|
||||
```js
|
||||
assert(myStr === 'FirstLine\n\t\\SecondLine\nThirdLine');
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
(function(){
|
||||
@@ -69,16 +87,14 @@ if (myStr !== undefined){
|
||||
console.log('myStr:\n' + myStr);}})();
|
||||
```
|
||||
|
||||
</div>
|
||||
## --seed-contents--
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
```js
|
||||
var myStr; // Change this line
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
var myStr = "FirstLine\n\t\\SecondLine\nThirdLine";
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@@ -6,51 +6,41 @@ videoUrl: 'https://scrimba.com/c/c2QvgSr'
|
||||
forumTopicId: 17568
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
When you are defining a string you must start and end with a single or double quote. What happens when you need a literal quote: <code>"</code> or <code>'</code> inside of your string?
|
||||
In JavaScript, you can <dfn>escape</dfn> a quote from considering it as an end of string quote by placing a <dfn>backslash</dfn> (<code>\</code>) in front of the quote.
|
||||
<code>var sampleStr = "Alan said, \"Peter is learning JavaScript\".";</code>
|
||||
# --description--
|
||||
|
||||
When you are defining a string you must start and end with a single or double quote. What happens when you need a literal quote: `"` or `'` inside of your string?
|
||||
|
||||
In JavaScript, you can <dfn>escape</dfn> a quote from considering it as an end of string quote by placing a <dfn>backslash</dfn> (`\`) in front of the quote.
|
||||
|
||||
`var sampleStr = "Alan said, \"Peter is learning JavaScript\".";`
|
||||
|
||||
This signals to JavaScript that the following quote is not the end of the string, but should instead appear inside the string. So if you were to print this to the console, you would get:
|
||||
<code>Alan said, "Peter is learning JavaScript".</code>
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Use <dfn>backslashes</dfn> to assign a string to the <code>myStr</code> variable so that if you were to print it to the console, you would see:
|
||||
<code>I am a "double quoted" string inside "double quotes".</code>
|
||||
</section>
|
||||
`Alan said, "Peter is learning JavaScript".`
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --instructions--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: You should use two double quotes (<code>"</code>) and four escaped double quotes (<code>\"</code>).
|
||||
testString: assert(code.match(/\\"/g).length === 4 && code.match(/[^\\]"/g).length === 2);
|
||||
- text: 'Variable myStr should contain the string: <code>I am a "double quoted" string inside "double quotes".</code>'
|
||||
testString: 'assert(myStr === "I am a \"double quoted\" string inside \"double quotes\".");'
|
||||
Use <dfn>backslashes</dfn> to assign a string to the `myStr` variable so that if you were to print it to the console, you would see:
|
||||
|
||||
```
|
||||
`I am a "double quoted" string inside "double quotes".`
|
||||
|
||||
</section>
|
||||
# --hints--
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
You should use two double quotes (`"`) and four escaped double quotes (`\"`).
|
||||
|
||||
```js
|
||||
var myStr = ""; // Change this line
|
||||
|
||||
|
||||
assert(code.match(/\\"/g).length === 4 && code.match(/[^\\]"/g).length === 2);
|
||||
```
|
||||
|
||||
</div>
|
||||
Variable myStr should contain the string: `I am a "double quoted" string inside "double quotes".`
|
||||
|
||||
```js
|
||||
assert(myStr === 'I am a "double quoted" string inside "double quotes".');
|
||||
```
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
(function(){
|
||||
@@ -62,16 +52,14 @@ var myStr = ""; // Change this line
|
||||
})();
|
||||
```
|
||||
|
||||
</div>
|
||||
## --seed-contents--
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
```js
|
||||
var myStr = ""; // Change this line
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
var myStr = "I am a \"double quoted\" string inside \"double quotes\".";
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@@ -6,38 +6,44 @@ videoUrl: 'https://scrimba.com/c/cvmqEAd'
|
||||
forumTopicId: 18182
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
You can find the length of a <code>String</code> value by writing <code>.length</code> after the string variable or string literal.
|
||||
<code>"Alan Peter".length; // 10</code>
|
||||
For example, if we created a variable <code>var firstName = "Charles"</code>, we could find out how long the string <code>"Charles"</code> is by using the <code>firstName.length</code> property.
|
||||
</section>
|
||||
# --description--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Use the <code>.length</code> property to count the number of characters in the <code>lastName</code> variable and assign it to <code>lastNameLength</code>.
|
||||
</section>
|
||||
You can find the length of a `String` value by writing `.length` after the string variable or string literal.
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
`"Alan Peter".length; // 10`
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: 'You should not change the variable declarations in the <code>// Setup</code> section.'
|
||||
testString: assert(code.match(/var lastNameLength = 0;/) && code.match(/var lastName = "Lovelace";/));
|
||||
- text: <code>lastNameLength</code> should be equal to eight.
|
||||
testString: assert(typeof lastNameLength !== 'undefined' && lastNameLength === 8);
|
||||
- text: 'You should be getting the length of <code>lastName</code> by using <code>.length</code> like this: <code>lastName.length</code>.'
|
||||
testString: assert(code.match(/=\s*lastName\.length/g) && !code.match(/lastName\s*=\s*8/));
|
||||
For example, if we created a variable `var firstName = "Charles"`, we could find out how long the string `"Charles"` is by using the `firstName.length` property.
|
||||
|
||||
# --instructions--
|
||||
|
||||
Use the `.length` property to count the number of characters in the `lastName` variable and assign it to `lastNameLength`.
|
||||
|
||||
# --hints--
|
||||
|
||||
You should not change the variable declarations in the `// Setup` section.
|
||||
|
||||
```js
|
||||
assert(
|
||||
code.match(/var lastNameLength = 0;/) &&
|
||||
code.match(/var lastName = "Lovelace";/)
|
||||
);
|
||||
```
|
||||
|
||||
</section>
|
||||
`lastNameLength` should be equal to eight.
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
```js
|
||||
assert(typeof lastNameLength !== 'undefined' && lastNameLength === 8);
|
||||
```
|
||||
|
||||
<div id='js-seed'>
|
||||
You should be getting the length of `lastName` by using `.length` like this: `lastName.length`.
|
||||
|
||||
```js
|
||||
assert(code.match(/=\s*lastName\.length/g) && !code.match(/lastName\s*=\s*8/));
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
// Setup
|
||||
@@ -47,22 +53,12 @@ var lastName = "Lovelace";
|
||||
// Only change code below this line
|
||||
|
||||
lastNameLength = lastName;
|
||||
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
var lastNameLength = 0;
|
||||
var lastName = "Lovelace";
|
||||
lastNameLength = lastName.length;
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@@ -6,69 +6,64 @@ videoUrl: 'https://scrimba.com/c/cWP24Ub'
|
||||
forumTopicId: 18184
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
The <dfn>remainder</dfn> operator <code>%</code> gives the remainder of the division of two numbers.
|
||||
<strong>Example</strong>
|
||||
# --description--
|
||||
|
||||
The <dfn>remainder</dfn> operator `%` gives the remainder of the division of two numbers.
|
||||
|
||||
**Example**
|
||||
|
||||
<blockquote>5 % 2 = 1 because<br>Math.floor(5 / 2) = 2 (Quotient)<br>2 * 2 = 4<br>5 - 4 = 1 (Remainder)</blockquote>
|
||||
<strong>Usage</strong><br>In mathematics, a number can be checked to be even or odd by checking the remainder of the division of the number by <code>2</code>.
|
||||
|
||||
**Usage**
|
||||
In mathematics, a number can be checked to be even or odd by checking the remainder of the division of the number by `2`.
|
||||
|
||||
<blockquote>17 % 2 = 1 (17 is Odd)<br>48 % 2 = 0 (48 is Even)</blockquote>
|
||||
<strong>Note</strong><br>The <dfn>remainder</dfn> operator is sometimes incorrectly referred to as the "modulus" operator. It is very similar to modulus, but does not work properly with negative numbers.
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Set <code>remainder</code> equal to the remainder of <code>11</code> divided by <code>3</code> using the <dfn>remainder</dfn> (<code>%</code>) operator.
|
||||
</section>
|
||||
**Note**
|
||||
The <dfn>remainder</dfn> operator is sometimes incorrectly referred to as the "modulus" operator. It is very similar to modulus, but does not work properly with negative numbers.
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --instructions--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: The variable <code>remainder</code> should be initialized
|
||||
testString: assert(/var\s+?remainder/.test(code));
|
||||
- text: The value of <code>remainder</code> should be <code>2</code>
|
||||
testString: assert(remainder === 2);
|
||||
- text: You should use the <code>%</code> operator
|
||||
testString: assert(/\s+?remainder\s*?=\s*?.*%.*;?/.test(code));
|
||||
Set `remainder` equal to the remainder of `11` divided by `3` using the <dfn>remainder</dfn> (`%`) operator.
|
||||
|
||||
```
|
||||
# --hints--
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
The variable `remainder` should be initialized
|
||||
|
||||
```js
|
||||
// Only change code below this line
|
||||
|
||||
var remainder;
|
||||
|
||||
assert(/var\s+?remainder/.test(code));
|
||||
```
|
||||
|
||||
</div>
|
||||
The value of `remainder` should be `2`
|
||||
|
||||
```js
|
||||
assert(remainder === 2);
|
||||
```
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
You should use the `%` operator
|
||||
|
||||
```js
|
||||
assert(/\s+?remainder\s*?=\s*?.*%.*;?/.test(code));
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
(function(y){return 'remainder = '+y;})(remainder);
|
||||
```
|
||||
|
||||
</div>
|
||||
## --seed-contents--
|
||||
|
||||
</section>
|
||||
```js
|
||||
// Only change code below this line
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
var remainder;
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
var remainder = 11 % 3;
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@@ -6,38 +6,48 @@ videoUrl: 'https://scrimba.com/c/cyWJJs3'
|
||||
forumTopicId: 18185
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
# --description--
|
||||
|
||||
Random numbers are useful for creating random behavior.
|
||||
JavaScript has a <code>Math.random()</code> function that generates a random decimal number between <code>0</code> (inclusive) and not quite up to <code>1</code> (exclusive). Thus <code>Math.random()</code> can return a <code>0</code> but never quite return a <code>1</code>
|
||||
<strong>Note</strong><br>Like <a href='/learn/javascript-algorithms-and-data-structures/basic-javascript/storing-values-with-the-assignment-operator' target='_blank'>Storing Values with the Equal Operator</a>, all function calls will be resolved before the <code>return</code> executes, so we can <code>return</code> the value of the <code>Math.random()</code> function.
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Change <code>randomFraction</code> to return a random number instead of returning <code>0</code>.
|
||||
</section>
|
||||
JavaScript has a `Math.random()` function that generates a random decimal number between `0` (inclusive) and not quite up to `1` (exclusive). Thus `Math.random()` can return a `0` but never quite return a `1`
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
**Note**
|
||||
Like [Storing Values with the Equal Operator](/learn/javascript-algorithms-and-data-structures/basic-javascript/storing-values-with-the-assignment-operator), all function calls will be resolved before the `return` executes, so we can `return` the value of the `Math.random()` function.
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>randomFraction</code> should return a random number.
|
||||
testString: assert(typeof randomFraction() === "number");
|
||||
- text: The number returned by <code>randomFraction</code> should be a decimal.
|
||||
testString: assert((randomFraction()+''). match(/\./g));
|
||||
- text: You should be using <code>Math.random</code> to generate the random decimal number.
|
||||
testString: assert(code.match(/Math\.random/g).length >= 0);
|
||||
# --instructions--
|
||||
|
||||
Change `randomFraction` to return a random number instead of returning `0`.
|
||||
|
||||
# --hints--
|
||||
|
||||
`randomFraction` should return a random number.
|
||||
|
||||
```js
|
||||
assert(typeof randomFraction() === 'number');
|
||||
```
|
||||
|
||||
</section>
|
||||
The number returned by `randomFraction` should be a decimal.
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
```js
|
||||
assert((randomFraction() + '').match(/\./g));
|
||||
```
|
||||
|
||||
<div id='js-seed'>
|
||||
You should be using `Math.random` to generate the random decimal number.
|
||||
|
||||
```js
|
||||
assert(code.match(/Math\.random/g).length >= 0);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
(function(){return randomFraction();})();
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function randomFraction() {
|
||||
@@ -50,28 +60,10 @@ function randomFraction() {
|
||||
}
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
(function(){return randomFraction();})();
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function randomFraction() {
|
||||
return Math.random();
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@@ -6,43 +6,68 @@ videoUrl: 'https://scrimba.com/c/cRn6bfr'
|
||||
forumTopicId: 18186
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
# --description--
|
||||
|
||||
It's great that we can generate random decimal numbers, but it's even more useful if we use it to generate random whole numbers.
|
||||
|
||||
<ol><li>Use <code>Math.random()</code> to generate a random decimal.</li><li>Multiply that random decimal by <code>20</code>.</li><li>Use another function, <code>Math.floor()</code> to round the number down to its nearest whole number.</li></ol>
|
||||
Remember that <code>Math.random()</code> can never quite return a <code>1</code> and, because we're rounding down, it's impossible to actually get <code>20</code>. This technique will give us a whole number between <code>0</code> and <code>19</code>.
|
||||
|
||||
Remember that `Math.random()` can never quite return a `1` and, because we're rounding down, it's impossible to actually get `20`. This technique will give us a whole number between `0` and `19`.
|
||||
|
||||
Putting everything together, this is what our code looks like:
|
||||
<code>Math.floor(Math.random() * 20);</code>
|
||||
We are calling <code>Math.random()</code>, multiplying the result by 20, then passing the value to <code>Math.floor()</code> function to round the value down to the nearest whole number.
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Use this technique to generate and return a random whole number between <code>0</code> and <code>9</code>.
|
||||
</section>
|
||||
`Math.floor(Math.random() * 20);`
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
We are calling `Math.random()`, multiplying the result by 20, then passing the value to `Math.floor()` function to round the value down to the nearest whole number.
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: The result of <code>randomWholeNum</code> should be a whole number.
|
||||
testString: assert(typeof randomWholeNum() === "number" && (function(){var r = randomWholeNum();return Math.floor(r) === r;})());
|
||||
- text: You should use <code>Math.random</code> to generate a random number.
|
||||
testString: assert(code.match(/Math.random/g).length >= 1);
|
||||
- text: You should have multiplied the result of <code>Math.random</code> by 10 to make it a number that is between zero and nine.
|
||||
testString: assert(code.match(/\s*?Math.random\s*?\(\s*?\)\s*?\*\s*?10[\D]\s*?/g) || code.match(/\s*?10\s*?\*\s*?Math.random\s*?\(\s*?\)\s*?/g));
|
||||
- text: You should use <code>Math.floor</code> to remove the decimal part of the number.
|
||||
testString: assert(code.match(/Math.floor/g).length >= 1);
|
||||
# --instructions--
|
||||
|
||||
Use this technique to generate and return a random whole number between `0` and `9`.
|
||||
|
||||
# --hints--
|
||||
|
||||
The result of `randomWholeNum` should be a whole number.
|
||||
|
||||
```js
|
||||
assert(
|
||||
typeof randomWholeNum() === 'number' &&
|
||||
(function () {
|
||||
var r = randomWholeNum();
|
||||
return Math.floor(r) === r;
|
||||
})()
|
||||
);
|
||||
```
|
||||
|
||||
</section>
|
||||
You should use `Math.random` to generate a random number.
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
```js
|
||||
assert(code.match(/Math.random/g).length >= 1);
|
||||
```
|
||||
|
||||
<div id='js-seed'>
|
||||
You should have multiplied the result of `Math.random` by 10 to make it a number that is between zero and nine.
|
||||
|
||||
```js
|
||||
assert(
|
||||
code.match(/\s*?Math.random\s*?\(\s*?\)\s*?\*\s*?10[\D]\s*?/g) ||
|
||||
code.match(/\s*?10\s*?\*\s*?Math.random\s*?\(\s*?\)\s*?/g)
|
||||
);
|
||||
```
|
||||
|
||||
You should use `Math.floor` to remove the decimal part of the number.
|
||||
|
||||
```js
|
||||
assert(code.match(/Math.floor/g).length >= 1);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
(function(){return randomWholeNum();})();
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function randomWholeNum() {
|
||||
@@ -53,28 +78,10 @@ function randomWholeNum() {
|
||||
}
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
(function(){return randomWholeNum();})();
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function randomWholeNum() {
|
||||
return Math.floor(Math.random() * 10);
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@@ -6,55 +6,62 @@ videoUrl: 'https://scrimba.com/c/cm83yu6'
|
||||
forumTopicId: 18187
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
# --description--
|
||||
|
||||
Instead of generating a random whole number between zero and a given number like we did before, we can generate a random whole number that falls within a range of two specific numbers.
|
||||
To do this, we'll define a minimum number <code>min</code> and a maximum number <code>max</code>.
|
||||
|
||||
To do this, we'll define a minimum number `min` and a maximum number `max`.
|
||||
|
||||
Here's the formula we'll use. Take a moment to read it and try to understand what this code is doing:
|
||||
<code>Math.floor(Math.random() * (max - min + 1)) + min</code>
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Create a function called <code>randomRange</code> that takes a range <code>myMin</code> and <code>myMax</code> and returns a random whole number that's greater than or equal to <code>myMin</code>, and is less than or equal to <code>myMax</code>, inclusive.
|
||||
</section>
|
||||
`Math.floor(Math.random() * (max - min + 1)) + min`
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --instructions--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: The lowest random number that can be generated by <code>randomRange</code> should be equal to your minimum number, <code>myMin</code>.
|
||||
testString: assert(calcMin === 5);
|
||||
- text: The highest random number that can be generated by <code>randomRange</code> should be equal to your maximum number, <code>myMax</code>.
|
||||
testString: assert(calcMax === 15);
|
||||
- text: The random number generated by <code>randomRange</code> should be an integer, not a decimal.
|
||||
testString: assert(randomRange(0,1) % 1 === 0 );
|
||||
- text: <code>randomRange</code> should use both <code>myMax</code> and <code>myMin</code>, and return a random number in your range.
|
||||
testString: assert((function(){if(code.match(/myMax/g).length > 1 && code.match(/myMin/g).length > 2 && code.match(/Math.floor/g) && code.match(/Math.random/g)){return true;}else{return false;}})());
|
||||
Create a function called `randomRange` that takes a range `myMin` and `myMax` and returns a random whole number that's greater than or equal to `myMin`, and is less than or equal to `myMax`, inclusive.
|
||||
|
||||
```
|
||||
# --hints--
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
The lowest random number that can be generated by `randomRange` should be equal to your minimum number, `myMin`.
|
||||
|
||||
```js
|
||||
function randomRange(myMin, myMax) {
|
||||
// Only change code below this line
|
||||
return 0;
|
||||
// Only change code above this line
|
||||
}
|
||||
assert(calcMin === 5);
|
||||
```
|
||||
|
||||
</div>
|
||||
The highest random number that can be generated by `randomRange` should be equal to your maximum number, `myMax`.
|
||||
|
||||
```js
|
||||
assert(calcMax === 15);
|
||||
```
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
The random number generated by `randomRange` should be an integer, not a decimal.
|
||||
|
||||
```js
|
||||
assert(randomRange(0, 1) % 1 === 0);
|
||||
```
|
||||
|
||||
`randomRange` should use both `myMax` and `myMin`, and return a random number in your range.
|
||||
|
||||
```js
|
||||
assert(
|
||||
(function () {
|
||||
if (
|
||||
code.match(/myMax/g).length > 1 &&
|
||||
code.match(/myMin/g).length > 2 &&
|
||||
code.match(/Math.floor/g) &&
|
||||
code.match(/Math.random/g)
|
||||
) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
})()
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
var calcMin = 100;
|
||||
@@ -73,18 +80,20 @@ for(var i = 0; i < 100; i++) {
|
||||
})()
|
||||
```
|
||||
|
||||
</div>
|
||||
## --seed-contents--
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
```js
|
||||
function randomRange(myMin, myMax) {
|
||||
// Only change code below this line
|
||||
return 0;
|
||||
// Only change code above this line
|
||||
}
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function randomRange(myMin, myMax) {
|
||||
return Math.floor(Math.random() * (myMax - myMin + 1)) + myMin;
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@@ -6,68 +6,47 @@ videoUrl: 'https://scrimba.com/c/cQM7mCN'
|
||||
forumTopicId: 18193
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
# --description--
|
||||
|
||||
In JavaScript, <dfn>scope</dfn> refers to the visibility of variables. Variables which are defined outside of a function block have <dfn>Global</dfn> scope. This means, they can be seen everywhere in your JavaScript code.
|
||||
Variables which are used without the <code>var</code> keyword are automatically created in the <code>global</code> scope. This can create unintended consequences elsewhere in your code or when running a function again. You should always declare your variables with <code>var</code>.
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Using <code>var</code>, declare a global variable named <code>myGlobal</code> outside of any function. Initialize it with a value of <code>10</code>.
|
||||
Inside function <code>fun1</code>, assign <code>5</code> to <code>oopsGlobal</code> <strong><em>without</em></strong> using the <code>var</code> keyword.
|
||||
</section>
|
||||
Variables which are used without the `var` keyword are automatically created in the `global` scope. This can create unintended consequences elsewhere in your code or when running a function again. You should always declare your variables with `var`.
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --instructions--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>myGlobal</code> should be defined
|
||||
testString: assert(typeof myGlobal != "undefined");
|
||||
- text: <code>myGlobal</code> should have a value of <code>10</code>
|
||||
testString: assert(myGlobal === 10);
|
||||
- text: <code>myGlobal</code> should be declared using the <code>var</code> keyword
|
||||
testString: assert(/var\s+myGlobal/.test(code));
|
||||
- text: <code>oopsGlobal</code> should be a global variable and have a value of <code>5</code>
|
||||
testString: assert(typeof oopsGlobal != "undefined" && oopsGlobal === 5);
|
||||
Using `var`, declare a global variable named `myGlobal` outside of any function. Initialize it with a value of `10`.
|
||||
|
||||
```
|
||||
Inside function `fun1`, assign `5` to `oopsGlobal` ***without*** using the `var` keyword.
|
||||
|
||||
</section>
|
||||
# --hints--
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`myGlobal` should be defined
|
||||
|
||||
```js
|
||||
// Declare the myGlobal variable below this line
|
||||
|
||||
|
||||
function fun1() {
|
||||
// Assign 5 to oopsGlobal Here
|
||||
|
||||
}
|
||||
|
||||
// Only change code above this line
|
||||
|
||||
function fun2() {
|
||||
var output = "";
|
||||
if (typeof myGlobal != "undefined") {
|
||||
output += "myGlobal: " + myGlobal;
|
||||
}
|
||||
if (typeof oopsGlobal != "undefined") {
|
||||
output += " oopsGlobal: " + oopsGlobal;
|
||||
}
|
||||
console.log(output);
|
||||
}
|
||||
assert(typeof myGlobal != 'undefined');
|
||||
```
|
||||
|
||||
</div>
|
||||
`myGlobal` should have a value of `10`
|
||||
|
||||
### Before Test
|
||||
<div id='js-setup'>
|
||||
```js
|
||||
assert(myGlobal === 10);
|
||||
```
|
||||
|
||||
`myGlobal` should be declared using the `var` keyword
|
||||
|
||||
```js
|
||||
assert(/var\s+myGlobal/.test(code));
|
||||
```
|
||||
|
||||
`oopsGlobal` should be a global variable and have a value of `5`
|
||||
|
||||
```js
|
||||
assert(typeof oopsGlobal != 'undefined' && oopsGlobal === 5);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --before-user-code--
|
||||
|
||||
```js
|
||||
var logOutput = "";
|
||||
@@ -92,10 +71,7 @@ var oopsGlobal;
|
||||
capture();
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
fun1();
|
||||
@@ -104,13 +80,32 @@ uncapture();
|
||||
(function() { return logOutput || "console.log never called"; })();
|
||||
```
|
||||
|
||||
</div>
|
||||
## --seed-contents--
|
||||
|
||||
</section>
|
||||
```js
|
||||
// Declare the myGlobal variable below this line
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
function fun1() {
|
||||
// Assign 5 to oopsGlobal Here
|
||||
|
||||
}
|
||||
|
||||
// Only change code above this line
|
||||
|
||||
function fun2() {
|
||||
var output = "";
|
||||
if (typeof myGlobal != "undefined") {
|
||||
output += "myGlobal: " + myGlobal;
|
||||
}
|
||||
if (typeof oopsGlobal != "undefined") {
|
||||
output += " oopsGlobal: " + oopsGlobal;
|
||||
}
|
||||
console.log(output);
|
||||
}
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
var myGlobal = 10;
|
||||
@@ -130,5 +125,3 @@ function fun2() {
|
||||
console.log(output);
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@@ -6,9 +6,10 @@ videoUrl: 'https://scrimba.com/c/c2QwKH2'
|
||||
forumTopicId: 18194
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
It is possible to have both <dfn>local</dfn> and <dfn>global</dfn> variables with the same name. When you do this, the <code>local</code> variable takes precedence over the <code>global</code> variable.
|
||||
# --description--
|
||||
|
||||
It is possible to have both <dfn>local</dfn> and <dfn>global</dfn> variables with the same name. When you do this, the `local` variable takes precedence over the `global` variable.
|
||||
|
||||
In this example:
|
||||
|
||||
```js
|
||||
@@ -19,34 +20,35 @@ function myFun() {
|
||||
}
|
||||
```
|
||||
|
||||
The function <code>myFun</code> will return <code>"Head"</code> because the <code>local</code> version of the variable is present.
|
||||
</section>
|
||||
The function `myFun` will return `"Head"` because the `local` version of the variable is present.
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Add a local variable to <code>myOutfit</code> function to override the value of <code>outerWear</code> with <code>"sweater"</code>.
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
Add a local variable to `myOutfit` function to override the value of `outerWear` with `"sweater"`.
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: You should not change the value of the global <code>outerWear</code>.
|
||||
testString: assert(outerWear === "T-Shirt");
|
||||
- text: <code>myOutfit</code> should return <code>"sweater"</code>.
|
||||
testString: assert(myOutfit() === "sweater");
|
||||
- text: You should not change the return statement.
|
||||
testString: assert(/return outerWear/.test(code));
|
||||
# --hints--
|
||||
|
||||
You should not change the value of the global `outerWear`.
|
||||
|
||||
```js
|
||||
assert(outerWear === 'T-Shirt');
|
||||
```
|
||||
|
||||
</section>
|
||||
`myOutfit` should return `"sweater"`.
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
```js
|
||||
assert(myOutfit() === 'sweater');
|
||||
```
|
||||
|
||||
<div id='js-seed'>
|
||||
You should not change the return statement.
|
||||
|
||||
```js
|
||||
assert(/return outerWear/.test(code));
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
// Setup
|
||||
@@ -64,15 +66,7 @@ function myOutfit() {
|
||||
myOutfit();
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
var outerWear = "T-Shirt";
|
||||
@@ -81,5 +75,3 @@ function myOutfit() {
|
||||
return outerWear;
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@@ -6,55 +6,87 @@ videoUrl: 'https://scrimba.com/c/c9ykNUR'
|
||||
forumTopicId: 18195
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
In the game of <a href="https://en.wikipedia.org/wiki/Golf" target="_blank">golf</a> each hole has a <code>par</code> meaning the average number of <code>strokes</code> a golfer is expected to make in order to sink the ball in a hole to complete the play. Depending on how far above or below <code>par</code> your <code>strokes</code> are, there is a different nickname.
|
||||
Your function will be passed <code>par</code> and <code>strokes</code> arguments. Return the correct string according to this table which lists the strokes in order of priority; top (highest) to bottom (lowest):
|
||||
<table class="table table-striped"><thead><tr><th>Strokes</th><th>Return</th></tr></thead><tbody><tr><td>1</td><td>"Hole-in-one!"</td></tr><tr><td><= par - 2</td><td>"Eagle"</td></tr><tr><td>par - 1</td><td>"Birdie"</td></tr><tr><td>par</td><td>"Par"</td></tr><tr><td>par + 1</td><td>"Bogey"</td></tr><tr><td>par + 2</td><td>"Double Bogey"</td></tr><tr><td>>= par + 3</td><td>"Go Home!"</td></tr></tbody></table>
|
||||
<code>par</code> and <code>strokes</code> will always be numeric and positive. We have added an array of all the names for your convenience.
|
||||
</section>
|
||||
# --description--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
In the game of [golf](https://en.wikipedia.org/wiki/Golf) each hole has a `par` meaning the average number of `strokes` a golfer is expected to make in order to sink the ball in a hole to complete the play. Depending on how far above or below `par` your `strokes` are, there is a different nickname.
|
||||
|
||||
</section>
|
||||
Your function will be passed `par` and `strokes` arguments. Return the correct string according to this table which lists the strokes in order of priority; top (highest) to bottom (lowest):
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
<table class='table table-striped'><thead><tr><th>Strokes</th><th>Return</th></tr></thead><tbody><tr><td>1</td><td>"Hole-in-one!"</td></tr><tr><td><= par - 2</td><td>"Eagle"</td></tr><tr><td>par - 1</td><td>"Birdie"</td></tr><tr><td>par</td><td>"Par"</td></tr><tr><td>par + 1</td><td>"Bogey"</td></tr><tr><td>par + 2</td><td>"Double Bogey"</td></tr><tr><td>>= par + 3</td><td>"Go Home!"</td></tr></tbody></table>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>golfScore(4, 1)</code> should return "Hole-in-one!"
|
||||
testString: assert(golfScore(4, 1) === "Hole-in-one!");
|
||||
- text: <code>golfScore(4, 2)</code> should return "Eagle"
|
||||
testString: assert(golfScore(4, 2) === "Eagle");
|
||||
- text: <code>golfScore(5, 2)</code> should return "Eagle"
|
||||
testString: assert(golfScore(5, 2) === "Eagle");
|
||||
- text: <code>golfScore(4, 3)</code> should return "Birdie"
|
||||
testString: assert(golfScore(4, 3) === "Birdie");
|
||||
- text: <code>golfScore(4, 4)</code> should return "Par"
|
||||
testString: assert(golfScore(4, 4) === "Par");
|
||||
- text: <code>golfScore(1, 1)</code> should return "Hole-in-one!"
|
||||
testString: assert(golfScore(1, 1) === "Hole-in-one!");
|
||||
- text: <code>golfScore(5, 5)</code> should return "Par"
|
||||
testString: assert(golfScore(5, 5) === "Par");
|
||||
- text: <code>golfScore(4, 5)</code> should return "Bogey"
|
||||
testString: assert(golfScore(4, 5) === "Bogey");
|
||||
- text: <code>golfScore(4, 6)</code> should return "Double Bogey"
|
||||
testString: assert(golfScore(4, 6) === "Double Bogey");
|
||||
- text: <code>golfScore(4, 7)</code> should return "Go Home!"
|
||||
testString: assert(golfScore(4, 7) === "Go Home!");
|
||||
- text: <code>golfScore(5, 9)</code> should return "Go Home!"
|
||||
testString: assert(golfScore(5, 9) === "Go Home!");
|
||||
`par` and `strokes` will always be numeric and positive. We have added an array of all the names for your convenience.
|
||||
|
||||
# --hints--
|
||||
|
||||
`golfScore(4, 1)` should return "Hole-in-one!"
|
||||
|
||||
```js
|
||||
assert(golfScore(4, 1) === 'Hole-in-one!');
|
||||
```
|
||||
|
||||
</section>
|
||||
`golfScore(4, 2)` should return "Eagle"
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
```js
|
||||
assert(golfScore(4, 2) === 'Eagle');
|
||||
```
|
||||
|
||||
<div id='js-seed'>
|
||||
`golfScore(5, 2)` should return "Eagle"
|
||||
|
||||
```js
|
||||
assert(golfScore(5, 2) === 'Eagle');
|
||||
```
|
||||
|
||||
`golfScore(4, 3)` should return "Birdie"
|
||||
|
||||
```js
|
||||
assert(golfScore(4, 3) === 'Birdie');
|
||||
```
|
||||
|
||||
`golfScore(4, 4)` should return "Par"
|
||||
|
||||
```js
|
||||
assert(golfScore(4, 4) === 'Par');
|
||||
```
|
||||
|
||||
`golfScore(1, 1)` should return "Hole-in-one!"
|
||||
|
||||
```js
|
||||
assert(golfScore(1, 1) === 'Hole-in-one!');
|
||||
```
|
||||
|
||||
`golfScore(5, 5)` should return "Par"
|
||||
|
||||
```js
|
||||
assert(golfScore(5, 5) === 'Par');
|
||||
```
|
||||
|
||||
`golfScore(4, 5)` should return "Bogey"
|
||||
|
||||
```js
|
||||
assert(golfScore(4, 5) === 'Bogey');
|
||||
```
|
||||
|
||||
`golfScore(4, 6)` should return "Double Bogey"
|
||||
|
||||
```js
|
||||
assert(golfScore(4, 6) === 'Double Bogey');
|
||||
```
|
||||
|
||||
`golfScore(4, 7)` should return "Go Home!"
|
||||
|
||||
```js
|
||||
assert(golfScore(4, 7) === 'Go Home!');
|
||||
```
|
||||
|
||||
`golfScore(5, 9)` should return "Go Home!"
|
||||
|
||||
```js
|
||||
assert(golfScore(5, 9) === 'Go Home!');
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
var names = ["Hole-in-one!", "Eagle", "Birdie", "Par", "Bogey", "Double Bogey", "Go Home!"];
|
||||
@@ -69,15 +101,7 @@ function golfScore(par, strokes) {
|
||||
golfScore(5, 4);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function golfScore(par, strokes) {
|
||||
@@ -108,5 +132,3 @@ function golfScore(par, strokes) {
|
||||
return "Go Home!";
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@@ -6,72 +6,71 @@ videoUrl: 'https://scrimba.com/c/ca8GLT9'
|
||||
forumTopicId: 18201
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
You can easily <dfn>increment</dfn> or add one to a variable with the <code>++</code> operator.
|
||||
<code>i++;</code>
|
||||
# --description--
|
||||
|
||||
You can easily <dfn>increment</dfn> or add one to a variable with the `++` operator.
|
||||
|
||||
`i++;`
|
||||
|
||||
is the equivalent of
|
||||
<code>i = i + 1;</code>
|
||||
<strong>Note</strong><br>The entire line becomes <code>i++;</code>, eliminating the need for the equal sign.
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Change the code to use the <code>++</code> operator on <code>myVar</code>.
|
||||
</section>
|
||||
`i = i + 1;`
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
**Note**
|
||||
The entire line becomes `i++;`, eliminating the need for the equal sign.
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>myVar</code> should equal <code>88</code>.
|
||||
testString: assert(myVar === 88);
|
||||
- text: You should not use the assignment operator.
|
||||
testString: assert(/var\s*myVar\s*=\s*87;\s*\/*.*\s*([+]{2}\s*myVar|myVar\s*[+]{2});/.test(code));
|
||||
- text: You should use the <code>++</code> operator.
|
||||
testString: assert(/[+]{2}\s*myVar|myVar\s*[+]{2}/.test(code));
|
||||
- text: You should not change code above the specified comment.
|
||||
testString: assert(/var myVar = 87;/.test(code));
|
||||
# --instructions--
|
||||
|
||||
Change the code to use the `++` operator on `myVar`.
|
||||
|
||||
# --hints--
|
||||
|
||||
`myVar` should equal `88`.
|
||||
|
||||
```js
|
||||
assert(myVar === 88);
|
||||
```
|
||||
|
||||
</section>
|
||||
You should not use the assignment operator.
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
```js
|
||||
assert(
|
||||
/var\s*myVar\s*=\s*87;\s*\/*.*\s*([+]{2}\s*myVar|myVar\s*[+]{2});/.test(code)
|
||||
);
|
||||
```
|
||||
|
||||
<div id='js-seed'>
|
||||
You should use the `++` operator.
|
||||
|
||||
```js
|
||||
assert(/[+]{2}\s*myVar|myVar\s*[+]{2}/.test(code));
|
||||
```
|
||||
|
||||
You should not change code above the specified comment.
|
||||
|
||||
```js
|
||||
assert(/var myVar = 87;/.test(code));
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
(function(z){return 'myVar = ' + z;})(myVar);
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
var myVar = 87;
|
||||
|
||||
// Only change code below this line
|
||||
myVar = myVar + 1;
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
(function(z){return 'myVar = ' + z;})(myVar);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
var myVar = 87;
|
||||
myVar++;
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@@ -6,60 +6,41 @@ videoUrl: 'https://scrimba.com/c/cWJ4Bfb'
|
||||
forumTopicId: 301171
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
# --description--
|
||||
|
||||
It is common to <dfn>initialize</dfn> a variable to an initial value in the same line as it is declared.
|
||||
<code>var myVar = 0;</code>
|
||||
Creates a new variable called <code>myVar</code> and assigns it an initial value of <code>0</code>.
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Define a variable <code>a</code> with <code>var</code> and initialize it to a value of <code>9</code>.
|
||||
</section>
|
||||
`var myVar = 0;`
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
Creates a new variable called `myVar` and assigns it an initial value of `0`.
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: You should initialize <code>a</code> to a value of <code>9</code>.
|
||||
testString: assert(/var\s+a\s*=\s*9\s*/.test(code));
|
||||
# --instructions--
|
||||
|
||||
```
|
||||
Define a variable `a` with `var` and initialize it to a value of `9`.
|
||||
|
||||
</section>
|
||||
# --hints--
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
You should initialize `a` to a value of `9`.
|
||||
|
||||
```js
|
||||
|
||||
|
||||
assert(/var\s+a\s*=\s*9\s*/.test(code));
|
||||
```
|
||||
|
||||
</div>
|
||||
# --seed--
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
if(typeof a !== 'undefined') {(function(a){return "a = " + a;})(a);} else { (function() {return 'a is undefined';})(); }
|
||||
```
|
||||
|
||||
</div>
|
||||
## --seed-contents--
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
```js
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
var a = 9;
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@@ -6,9 +6,9 @@ videoUrl: 'https://scrimba.com/c/caeJ2hm'
|
||||
forumTopicId: 18206
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
If you have multiple conditions that need to be addressed, you can chain <code>if</code> statements together with <code>else if</code> statements.
|
||||
# --description--
|
||||
|
||||
If you have multiple conditions that need to be addressed, you can chain `if` statements together with `else if` statements.
|
||||
|
||||
```js
|
||||
if (num > 15) {
|
||||
@@ -20,43 +20,67 @@ if (num > 15) {
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Convert the logic to use <code>else if</code> statements.
|
||||
</section>
|
||||
Convert the logic to use `else if` statements.
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: You should have at least two <code>else</code> statements
|
||||
testString: assert(code.match(/else/g).length > 1);
|
||||
- text: You should have at least two <code>if</code> statements
|
||||
testString: assert(code.match(/if/g).length > 1);
|
||||
- text: You should have closing and opening curly braces for each <code>if else</code> code block.
|
||||
testString: assert(code.match(/if\s*\((.+)\)\s*\{[\s\S]+\}\s*else\s+if\s*\((.+)\)\s*\{[\s\S]+\}\s*else\s*\{[\s\S]+\s*\}/));
|
||||
- text: <code>testElseIf(0)</code> should return "Smaller than 5"
|
||||
testString: assert(testElseIf(0) === "Smaller than 5");
|
||||
- text: <code>testElseIf(5)</code> should return "Between 5 and 10"
|
||||
testString: assert(testElseIf(5) === "Between 5 and 10");
|
||||
- text: <code>testElseIf(7)</code> should return "Between 5 and 10"
|
||||
testString: assert(testElseIf(7) === "Between 5 and 10");
|
||||
- text: <code>testElseIf(10)</code> should return "Between 5 and 10"
|
||||
testString: assert(testElseIf(10) === "Between 5 and 10");
|
||||
- text: <code>testElseIf(12)</code> should return "Greater than 10"
|
||||
testString: assert(testElseIf(12) === "Greater than 10");
|
||||
You should have at least two `else` statements
|
||||
|
||||
```js
|
||||
assert(code.match(/else/g).length > 1);
|
||||
```
|
||||
|
||||
</section>
|
||||
You should have at least two `if` statements
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
```js
|
||||
assert(code.match(/if/g).length > 1);
|
||||
```
|
||||
|
||||
<div id='js-seed'>
|
||||
You should have closing and opening curly braces for each `if else` code block.
|
||||
|
||||
```js
|
||||
assert(
|
||||
code.match(
|
||||
/if\s*\((.+)\)\s*\{[\s\S]+\}\s*else\s+if\s*\((.+)\)\s*\{[\s\S]+\}\s*else\s*\{[\s\S]+\s*\}/
|
||||
)
|
||||
);
|
||||
```
|
||||
|
||||
`testElseIf(0)` should return "Smaller than 5"
|
||||
|
||||
```js
|
||||
assert(testElseIf(0) === 'Smaller than 5');
|
||||
```
|
||||
|
||||
`testElseIf(5)` should return "Between 5 and 10"
|
||||
|
||||
```js
|
||||
assert(testElseIf(5) === 'Between 5 and 10');
|
||||
```
|
||||
|
||||
`testElseIf(7)` should return "Between 5 and 10"
|
||||
|
||||
```js
|
||||
assert(testElseIf(7) === 'Between 5 and 10');
|
||||
```
|
||||
|
||||
`testElseIf(10)` should return "Between 5 and 10"
|
||||
|
||||
```js
|
||||
assert(testElseIf(10) === 'Between 5 and 10');
|
||||
```
|
||||
|
||||
`testElseIf(12)` should return "Greater than 10"
|
||||
|
||||
```js
|
||||
assert(testElseIf(12) === 'Greater than 10');
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function testElseIf(val) {
|
||||
@@ -72,18 +96,9 @@ function testElseIf(val) {
|
||||
}
|
||||
|
||||
testElseIf(7);
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function testElseIf(val) {
|
||||
@@ -96,5 +111,3 @@ function testElseIf(val) {
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@@ -6,9 +6,9 @@ videoUrl: 'https://scrimba.com/c/cek4Efq'
|
||||
forumTopicId: 18207
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
When a condition for an <code>if</code> statement is true, the block of code following it is executed. What about when that condition is false? Normally nothing would happen. With an <code>else</code> statement, an alternate block of code can be executed.
|
||||
# --description--
|
||||
|
||||
When a condition for an `if` statement is true, the block of code following it is executed. What about when that condition is false? Normally nothing would happen. With an `else` statement, an alternate block of code can be executed.
|
||||
|
||||
```js
|
||||
if (num > 10) {
|
||||
@@ -18,41 +18,57 @@ if (num > 10) {
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Combine the <code>if</code> statements into a single <code>if/else</code> statement.
|
||||
</section>
|
||||
Combine the `if` statements into a single `if/else` statement.
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: You should only have one <code>if</code> statement in the editor
|
||||
testString: assert(code.match(/if/g).length === 1);
|
||||
- text: You should use an <code>else</code> statement
|
||||
testString: assert(/else/g.test(code));
|
||||
- text: <code>testElse(4)</code> should return "5 or Smaller"
|
||||
testString: assert(testElse(4) === "5 or Smaller");
|
||||
- text: <code>testElse(5)</code> should return "5 or Smaller"
|
||||
testString: assert(testElse(5) === "5 or Smaller");
|
||||
- text: <code>testElse(6)</code> should return "Bigger than 5"
|
||||
testString: assert(testElse(6) === "Bigger than 5");
|
||||
- text: <code>testElse(10)</code> should return "Bigger than 5".
|
||||
testString: assert(testElse(10) === "Bigger than 5");
|
||||
- text: You should not change the code above or below the specified comments.
|
||||
testString: assert(/var result = "";/.test(code) && /return result;/.test(code));
|
||||
You should only have one `if` statement in the editor
|
||||
|
||||
```js
|
||||
assert(code.match(/if/g).length === 1);
|
||||
```
|
||||
|
||||
</section>
|
||||
You should use an `else` statement
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
```js
|
||||
assert(/else/g.test(code));
|
||||
```
|
||||
|
||||
<div id='js-seed'>
|
||||
`testElse(4)` should return "5 or Smaller"
|
||||
|
||||
```js
|
||||
assert(testElse(4) === '5 or Smaller');
|
||||
```
|
||||
|
||||
`testElse(5)` should return "5 or Smaller"
|
||||
|
||||
```js
|
||||
assert(testElse(5) === '5 or Smaller');
|
||||
```
|
||||
|
||||
`testElse(6)` should return "Bigger than 5"
|
||||
|
||||
```js
|
||||
assert(testElse(6) === 'Bigger than 5');
|
||||
```
|
||||
|
||||
`testElse(10)` should return "Bigger than 5".
|
||||
|
||||
```js
|
||||
assert(testElse(10) === 'Bigger than 5');
|
||||
```
|
||||
|
||||
You should not change the code above or below the specified comments.
|
||||
|
||||
```js
|
||||
assert(/var result = "";/.test(code) && /return result;/.test(code));
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function testElse(val) {
|
||||
@@ -72,18 +88,9 @@ function testElse(val) {
|
||||
}
|
||||
|
||||
testElse(4);
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function testElse(val) {
|
||||
@@ -96,5 +103,3 @@ function testElse(val) {
|
||||
return result;
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@@ -6,10 +6,11 @@ videoUrl: 'https://scrimba.com/c/cm8n7T9'
|
||||
forumTopicId: 18212
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
For loops don't have to iterate one at a time. By changing our <code>final-expression</code>, we can count by even numbers.
|
||||
We'll start at <code>i = 0</code> and loop while <code>i < 10</code>. We'll increment <code>i</code> by 2 each loop with <code>i += 2</code>.
|
||||
# --description--
|
||||
|
||||
For loops don't have to iterate one at a time. By changing our `final-expression`, we can count by even numbers.
|
||||
|
||||
We'll start at `i = 0` and loop while `i < 10`. We'll increment `i` by 2 each loop with `i += 2`.
|
||||
|
||||
```js
|
||||
var ourArray = [];
|
||||
@@ -18,60 +19,44 @@ for (var i = 0; i < 10; i += 2) {
|
||||
}
|
||||
```
|
||||
|
||||
<code>ourArray</code> will now contain <code>[0,2,4,6,8]</code>.
|
||||
Let's change our <code>initialization</code> so we can count by odd numbers.
|
||||
</section>
|
||||
`ourArray` will now contain `[0,2,4,6,8]`. Let's change our `initialization` so we can count by odd numbers.
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Push the odd numbers from 1 through 9 to <code>myArray</code> using a <code>for</code> loop.
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
Push the odd numbers from 1 through 9 to `myArray` using a `for` loop.
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: You should be using a <code>for</code> loop for this.
|
||||
testString: assert(/for\s*\([^)]+?\)/.test(code));
|
||||
- text: <code>myArray</code> should equal <code>[1,3,5,7,9]</code>.
|
||||
testString: assert.deepEqual(myArray, [1,3,5,7,9]);
|
||||
# --hints--
|
||||
|
||||
You should be using a `for` loop for this.
|
||||
|
||||
```js
|
||||
assert(/for\s*\([^)]+?\)/.test(code));
|
||||
```
|
||||
|
||||
</section>
|
||||
`myArray` should equal `[1,3,5,7,9]`.
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
```js
|
||||
assert.deepEqual(myArray, [1, 3, 5, 7, 9]);
|
||||
```
|
||||
|
||||
<div id='js-seed'>
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
if(typeof myArray !== "undefined"){(function(){return myArray;})();}
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
// Setup
|
||||
var myArray = [];
|
||||
|
||||
// Only change code below this line
|
||||
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
if(typeof myArray !== "undefined"){(function(){return myArray;})();}
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
var myArray = [];
|
||||
@@ -79,5 +64,3 @@ for (var i = 1; i < 10; i += 2) {
|
||||
myArray.push(i);
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@@ -6,9 +6,9 @@ videoUrl: 'https://scrimba.com/c/caeR3HB'
|
||||
forumTopicId: 18216
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
A common task in JavaScript is to iterate through the contents of an array. One way to do that is with a <code>for</code> loop. This code will output each element of the array <code>arr</code> to the console:
|
||||
# --description--
|
||||
|
||||
A common task in JavaScript is to iterate through the contents of an array. One way to do that is with a `for` loop. This code will output each element of the array `arr` to the console:
|
||||
|
||||
```js
|
||||
var arr = [10, 9, 8, 7, 6];
|
||||
@@ -17,59 +17,56 @@ for (var i = 0; i < arr.length; i++) {
|
||||
}
|
||||
```
|
||||
|
||||
Remember that arrays have zero-based indexing, which means the last index of the array is <code>length - 1</code>. Our condition for this loop is <code>i < arr.length</code>, which stops the loop when <code>i</code> is equal to <code>length</code>. In this case the last iteration is <code>i === 4</code> i.e. when <code>i</code> becomes equal to <code>arr.length</code> and outputs <code>6</code> to the console.
|
||||
</section>
|
||||
Remember that arrays have zero-based indexing, which means the last index of the array is `length - 1`. Our condition for this loop is `i < arr.length`, which stops the loop when `i` is equal to `length`. In this case the last iteration is `i === 4` i.e. when `i` becomes equal to `arr.length` and outputs `6` to the console.
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Declare and initialize a variable <code>total</code> to <code>0</code>. Use a <code>for</code> loop to add the value of each element of the <code>myArr</code> array to <code>total</code>.
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
Declare and initialize a variable `total` to `0`. Use a `for` loop to add the value of each element of the `myArr` array to `total`.
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>total</code> should be declared and initialized to 0.
|
||||
testString: assert(code.match(/(var|let|const)\s*?total\s*=\s*0.*?;?/));
|
||||
- text: <code>total</code> should equal 20.
|
||||
testString: assert(total === 20);
|
||||
- text: You should use a <code>for</code> loop to iterate through <code>myArr</code>.
|
||||
testString: assert(/for\s*\(/g.test(code) && /myArr\s*\[/g.test(code));
|
||||
- text: You should not attempt to directly assign the value 20 to <code>total</code>.
|
||||
testString: assert(!__helpers.removeWhiteSpace(code).match(/total[=+-]0*[1-9]+/gm));
|
||||
# --hints--
|
||||
|
||||
`total` should be declared and initialized to 0.
|
||||
|
||||
```js
|
||||
assert(code.match(/(var|let|const)\s*?total\s*=\s*0.*?;?/));
|
||||
```
|
||||
|
||||
</section>
|
||||
`total` should equal 20.
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
<div id='js-seed'>
|
||||
```js
|
||||
assert(total === 20);
|
||||
```
|
||||
|
||||
You should use a `for` loop to iterate through `myArr`.
|
||||
|
||||
```js
|
||||
assert(/for\s*\(/g.test(code) && /myArr\s*\[/g.test(code));
|
||||
```
|
||||
|
||||
You should not attempt to directly assign the value 20 to `total`.
|
||||
|
||||
```js
|
||||
assert(!__helpers.removeWhiteSpace(code).match(/total[=+-]0*[1-9]+/gm));
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
(function(){if(typeof total !== 'undefined') { return "total = " + total; } else { return "total is undefined";}})()
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
// Setup
|
||||
var myArr = [ 2, 3, 4, 5, 6];
|
||||
|
||||
// Only change code below this line
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
(function(){if(typeof total !== 'undefined') { return "total = " + total; } else { return "total is undefined";}})()
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
var myArr = [ 2, 3, 4, 5, 6];
|
||||
@@ -79,5 +76,3 @@ for (var i = 0; i < myArr.length; i++) {
|
||||
total += myArr[i];
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@@ -6,9 +6,9 @@ videoUrl: 'https://scrimba.com/c/cDqWGcp'
|
||||
forumTopicId: 301172
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
The next type of loop you will learn is called a <code>do...while</code> loop. It is called a <code>do...while</code> loop because it will first <code>do</code> one pass of the code inside the loop no matter what, and then continue to run the loop <code>while</code> the specified condition evaluates to <code>true</code>.
|
||||
# --description--
|
||||
|
||||
The next type of loop you will learn is called a `do...while` loop. It is called a `do...while` loop because it will first `do` one pass of the code inside the loop no matter what, and then continue to run the loop `while` the specified condition evaluates to `true`.
|
||||
|
||||
```js
|
||||
var ourArray = [];
|
||||
@@ -19,8 +19,7 @@ do {
|
||||
} while (i < 5);
|
||||
```
|
||||
|
||||
The example above behaves similar to other types of loops, and the resulting array will look like <code>[0, 1, 2, 3, 4]</code>. However, what makes the <code>do...while</code> different from other loops is how it behaves when the condition fails on the first check. Let's see this in action:
|
||||
Here is a regular <code>while</code> loop that will run the code in the loop as long as <code>i < 5</code>:
|
||||
The example above behaves similar to other types of loops, and the resulting array will look like `[0, 1, 2, 3, 4]`. However, what makes the `do...while` different from other loops is how it behaves when the condition fails on the first check. Let's see this in action: Here is a regular `while` loop that will run the code in the loop as long as `i < 5`:
|
||||
|
||||
```js
|
||||
var ourArray = [];
|
||||
@@ -31,8 +30,7 @@ while (i < 5) {
|
||||
}
|
||||
```
|
||||
|
||||
In this example, we initialize the value of <code>ourArray</code> to an empty array and the value of <code>i</code> to 5. When we execute the <code>while</code> loop, the condition evaluates to <code>false</code> because <code>i</code> is not less than 5, so we do not execute the code inside the loop. The result is that <code>ourArray</code> will end up with no values added to it, and it will still look like <code>[]</code> when all of the code in the example above has completed running.
|
||||
Now, take a look at a <code>do...while</code> loop:
|
||||
In this example, we initialize the value of `ourArray` to an empty array and the value of `i` to 5. When we execute the `while` loop, the condition evaluates to `false` because `i` is not less than 5, so we do not execute the code inside the loop. The result is that `ourArray` will end up with no values added to it, and it will still look like `[]` when all of the code in the example above has completed running. Now, take a look at a `do...while` loop:
|
||||
|
||||
```js
|
||||
var ourArray = [];
|
||||
@@ -43,35 +41,41 @@ do {
|
||||
} while (i < 5);
|
||||
```
|
||||
|
||||
In this case, we initialize the value of <code>i</code> to 5, just like we did with the <code>while</code> loop. When we get to the next line, there is no condition to evaluate, so we go to the code inside the curly braces and execute it. We will add a single element to the array and then increment <code>i</code> before we get to the condition check. When we finally evaluate the condition <code>i < 5</code> on the last line, we see that <code>i</code> is now 6, which fails the conditional check, so we exit the loop and are done. At the end of the above example, the value of <code>ourArray</code> is <code>[5]</code>.
|
||||
Essentially, a <code>do...while</code> loop ensures that the code inside the loop will run at least once.
|
||||
Let's try getting a <code>do...while</code> loop to work by pushing values to an array.
|
||||
</section>
|
||||
In this case, we initialize the value of `i` to 5, just like we did with the `while` loop. When we get to the next line, there is no condition to evaluate, so we go to the code inside the curly braces and execute it. We will add a single element to the array and then increment `i` before we get to the condition check. When we finally evaluate the condition `i < 5` on the last line, we see that `i` is now 6, which fails the conditional check, so we exit the loop and are done. At the end of the above example, the value of `ourArray` is `[5]`. Essentially, a `do...while` loop ensures that the code inside the loop will run at least once. Let's try getting a `do...while` loop to work by pushing values to an array.
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Change the <code>while</code> loop in the code to a <code>do...while</code> loop so the loop will push only the number <code>10</code> to <code>myArray</code>, and <code>i</code> will be equal to <code>11</code> when your code has finished running.
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
Change the `while` loop in the code to a `do...while` loop so the loop will push only the number `10` to `myArray`, and `i` will be equal to `11` when your code has finished running.
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: You should be using a <code>do...while</code> loop for this exercise.
|
||||
testString: assert(code.match(/do/g));
|
||||
- text: <code>myArray</code> should equal <code>[10]</code>.
|
||||
testString: assert.deepEqual(myArray, [10]);
|
||||
- text: <code>i</code> should equal <code>11</code>
|
||||
testString: assert.equal(i, 11);
|
||||
# --hints--
|
||||
|
||||
You should be using a `do...while` loop for this exercise.
|
||||
|
||||
```js
|
||||
assert(code.match(/do/g));
|
||||
```
|
||||
|
||||
</section>
|
||||
`myArray` should equal `[10]`.
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
```js
|
||||
assert.deepEqual(myArray, [10]);
|
||||
```
|
||||
|
||||
<div id='js-seed'>
|
||||
`i` should equal `11`
|
||||
|
||||
```js
|
||||
assert.equal(i, 11);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
if(typeof myArray !== "undefined"){(function(){return myArray;})();}
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
// Setup
|
||||
@@ -85,23 +89,7 @@ while (i < 5) {
|
||||
}
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
if(typeof myArray !== "undefined"){(function(){return myArray;})();}
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
var myArray = [];
|
||||
@@ -111,5 +99,3 @@ do {
|
||||
i++;
|
||||
} while (i < 5)
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@@ -6,16 +6,23 @@ videoUrl: 'https://scrimba.com/c/c9yNVCe'
|
||||
forumTopicId: 18219
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
# --description--
|
||||
|
||||
You can run the same code multiple times by using a loop.
|
||||
The most common type of JavaScript loop is called a <code>for</code> loop because it runs "for" a specific number of times.
|
||||
|
||||
The most common type of JavaScript loop is called a `for` loop because it runs "for" a specific number of times.
|
||||
|
||||
For loops are declared with three optional expressions separated by semicolons:
|
||||
<code>for ([initialization]; [condition]; [final-expression])</code>
|
||||
The <code>initialization</code> statement is executed one time only before the loop starts. It is typically used to define and setup your loop variable.
|
||||
The <code>condition</code> statement is evaluated at the beginning of every loop iteration and will continue as long as it evaluates to <code>true</code>. When <code>condition</code> is <code>false</code> at the start of the iteration, the loop will stop executing. This means if <code>condition</code> starts as <code>false</code>, your loop will never execute.
|
||||
The <code>final-expression</code> is executed at the end of each loop iteration, prior to the next <code>condition</code> check and is usually used to increment or decrement your loop counter.
|
||||
In the following example we initialize with <code>i = 0</code> and iterate while our condition <code>i < 5</code> is true. We'll increment <code>i</code> by <code>1</code> in each loop iteration with <code>i++</code> as our <code>final-expression</code>.
|
||||
|
||||
`for ([initialization]; [condition]; [final-expression])`
|
||||
|
||||
The `initialization` statement is executed one time only before the loop starts. It is typically used to define and setup your loop variable.
|
||||
|
||||
The `condition` statement is evaluated at the beginning of every loop iteration and will continue as long as it evaluates to `true`. When `condition` is `false` at the start of the iteration, the loop will stop executing. This means if `condition` starts as `false`, your loop will never execute.
|
||||
|
||||
The `final-expression` is executed at the end of each loop iteration, prior to the next `condition` check and is usually used to increment or decrement your loop counter.
|
||||
|
||||
In the following example we initialize with `i = 0` and iterate while our condition `i < 5` is true. We'll increment `i` by `1` in each loop iteration with `i++` as our `final-expression`.
|
||||
|
||||
```js
|
||||
var ourArray = [];
|
||||
@@ -24,59 +31,44 @@ for (var i = 0; i < 5; i++) {
|
||||
}
|
||||
```
|
||||
|
||||
<code>ourArray</code> will now contain <code>[0,1,2,3,4]</code>.
|
||||
</section>
|
||||
`ourArray` will now contain `[0,1,2,3,4]`.
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Use a <code>for</code> loop to work to push the values 1 through 5 onto <code>myArray</code>.
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
Use a `for` loop to work to push the values 1 through 5 onto `myArray`.
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: You should be using a <code>for</code> loop for this.
|
||||
testString: assert(/for\s*\([^)]+?\)/.test(code));
|
||||
- text: <code>myArray</code> should equal <code>[1,2,3,4,5]</code>.
|
||||
testString: assert.deepEqual(myArray, [1,2,3,4,5]);
|
||||
# --hints--
|
||||
|
||||
You should be using a `for` loop for this.
|
||||
|
||||
```js
|
||||
assert(/for\s*\([^)]+?\)/.test(code));
|
||||
```
|
||||
|
||||
</section>
|
||||
`myArray` should equal `[1,2,3,4,5]`.
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
```js
|
||||
assert.deepEqual(myArray, [1, 2, 3, 4, 5]);
|
||||
```
|
||||
|
||||
<div id='js-seed'>
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
if (typeof myArray !== "undefined"){(function(){return myArray;})();}
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
// Setup
|
||||
var myArray = [];
|
||||
|
||||
// Only change code below this line
|
||||
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
if (typeof myArray !== "undefined"){(function(){return myArray;})();}
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
var myArray = [];
|
||||
@@ -84,5 +76,3 @@ for (var i = 1; i < 6; i++) {
|
||||
myArray.push(i);
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@@ -6,10 +6,11 @@ videoUrl: 'https://scrimba.com/c/c8QbnCM'
|
||||
forumTopicId: 18220
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
# --description--
|
||||
|
||||
You can run the same code multiple times by using a loop.
|
||||
The first type of loop we will learn is called a <code>while</code> loop because it runs "while" a specified condition is true and stops once that condition is no longer true.
|
||||
|
||||
The first type of loop we will learn is called a `while` loop because it runs "while" a specified condition is true and stops once that condition is no longer true.
|
||||
|
||||
```js
|
||||
var ourArray = [];
|
||||
@@ -20,61 +21,46 @@ while(i < 5) {
|
||||
}
|
||||
```
|
||||
|
||||
In the code example above, the <code>while</code> loop will execute 5 times and append the numbers 0 through 4 to <code>ourArray</code>.
|
||||
In the code example above, the `while` loop will execute 5 times and append the numbers 0 through 4 to `ourArray`.
|
||||
|
||||
Let's try getting a while loop to work by pushing values to an array.
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Add the numbers 5 through 0 (inclusive) in descending order to <code>myArray</code> using a <code>while</code> loop.
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
Add the numbers 5 through 0 (inclusive) in descending order to `myArray` using a `while` loop.
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: You should be using a <code>while</code> loop for this.
|
||||
testString: assert(code.match(/while/g));
|
||||
- text: <code>myArray</code> should equal <code>[5,4,3,2,1,0]</code>.
|
||||
testString: assert.deepEqual(myArray, [5,4,3,2,1,0]);
|
||||
# --hints--
|
||||
|
||||
You should be using a `while` loop for this.
|
||||
|
||||
```js
|
||||
assert(code.match(/while/g));
|
||||
```
|
||||
|
||||
</section>
|
||||
`myArray` should equal `[5,4,3,2,1,0]`.
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
```js
|
||||
assert.deepEqual(myArray, [5, 4, 3, 2, 1, 0]);
|
||||
```
|
||||
|
||||
<div id='js-seed'>
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
if(typeof myArray !== "undefined"){(function(){return myArray;})();}
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
// Setup
|
||||
var myArray = [];
|
||||
|
||||
// Only change code below this line
|
||||
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
if(typeof myArray !== "undefined"){(function(){return myArray;})();}
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
var myArray = [];
|
||||
@@ -84,5 +70,3 @@ while(i >= 0) {
|
||||
i--;
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@@ -6,10 +6,11 @@ videoUrl: 'https://scrimba.com/c/cd62NhM'
|
||||
forumTopicId: 18227
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
# --description--
|
||||
|
||||
Variables which are declared within a function, as well as the function parameters have <dfn>local</dfn> scope. That means, they are only visible within that function.
|
||||
Here is a function <code>myTest</code> with a local variable called <code>loc</code>.
|
||||
|
||||
Here is a function `myTest` with a local variable called `loc`.
|
||||
|
||||
```js
|
||||
function myTest() {
|
||||
@@ -20,40 +21,38 @@ myTest(); // logs "foo"
|
||||
console.log(loc); // loc is not defined
|
||||
```
|
||||
|
||||
<code>loc</code> is not defined outside of the function.
|
||||
</section>
|
||||
`loc` is not defined outside of the function.
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
# --instructions--
|
||||
|
||||
The editor has two `console.log`s to help you see what is happening. Check the console as you code to see how it changes. Declare a local variable `myVar` inside `myLocalScope` and run the tests.
|
||||
The editor has two `console.log`s to help you see what is happening. Check the console as you code to see how it changes. Declare a local variable `myVar` inside `myLocalScope` and run the tests.
|
||||
|
||||
**Note:** The console will still have 'ReferenceError: myVar is not defined', but this will not cause the tests to fail.
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: The code should not contain a global <code>myVar</code> variable.
|
||||
testString: |
|
||||
function declared(){
|
||||
myVar;
|
||||
}
|
||||
assert.throws(declared, ReferenceError);
|
||||
- text: You should add a local <code>myVar</code> variable.
|
||||
testString: assert(/functionmyLocalScope\(\)\{.+(var|let|const)myVar[\s\S]*}/.test(__helpers.removeWhiteSpace(code)));
|
||||
# --hints--
|
||||
|
||||
The code should not contain a global `myVar` variable.
|
||||
|
||||
```js
|
||||
function declared() {
|
||||
myVar;
|
||||
}
|
||||
assert.throws(declared, ReferenceError);
|
||||
```
|
||||
|
||||
</section>
|
||||
You should add a local `myVar` variable.
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
```js
|
||||
assert(
|
||||
/functionmyLocalScope\(\)\{.+(var|let|const)myVar[\s\S]*}/.test(
|
||||
__helpers.removeWhiteSpace(code)
|
||||
)
|
||||
);
|
||||
```
|
||||
|
||||
<div id='js-seed'>
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function myLocalScope() {
|
||||
@@ -67,16 +66,9 @@ myLocalScope();
|
||||
// Run and check the console
|
||||
// myVar is not defined outside of myLocalScope
|
||||
console.log('outside myLocalScope', myVar);
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function myLocalScope() {
|
||||
@@ -90,7 +82,4 @@ myLocalScope();
|
||||
// Run and check the console
|
||||
// myVar is not defined outside of myLocalScope
|
||||
console.log('outside myLocalScope', myVar);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@@ -6,11 +6,14 @@ videoUrl: 'https://scrimba.com/c/cwNvMUV'
|
||||
forumTopicId: 18228
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
Order is important in <code>if</code>, <code>else if</code> statements.
|
||||
# --description--
|
||||
|
||||
Order is important in `if`, `else if` statements.
|
||||
|
||||
The function is executed from top to bottom so you will want to be careful of what statement comes first.
|
||||
|
||||
Take these two functions as an example.
|
||||
|
||||
Here's the first:
|
||||
|
||||
```js
|
||||
@@ -46,33 +49,33 @@ foo(0) // "Less than one"
|
||||
bar(0) // "Less than two"
|
||||
```
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Change the order of logic in the function so that it will return the correct statements in all cases.
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>orderMyLogic(4)</code> should return "Less than 5"
|
||||
testString: assert(orderMyLogic(4) === "Less than 5");
|
||||
- text: <code>orderMyLogic(6)</code> should return "Less than 10"
|
||||
testString: assert(orderMyLogic(6) === "Less than 10");
|
||||
- text: <code>orderMyLogic(11)</code> should return "Greater than or equal to 10"
|
||||
testString: assert(orderMyLogic(11) === "Greater than or equal to 10");
|
||||
`orderMyLogic(4)` should return "Less than 5"
|
||||
|
||||
```js
|
||||
assert(orderMyLogic(4) === 'Less than 5');
|
||||
```
|
||||
|
||||
</section>
|
||||
`orderMyLogic(6)` should return "Less than 10"
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
```js
|
||||
assert(orderMyLogic(6) === 'Less than 10');
|
||||
```
|
||||
|
||||
<div id='js-seed'>
|
||||
`orderMyLogic(11)` should return "Greater than or equal to 10"
|
||||
|
||||
```js
|
||||
assert(orderMyLogic(11) === 'Greater than or equal to 10');
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function orderMyLogic(val) {
|
||||
@@ -88,15 +91,7 @@ function orderMyLogic(val) {
|
||||
orderMyLogic(7);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function orderMyLogic(val) {
|
||||
@@ -109,5 +104,3 @@ function orderMyLogic(val) {
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@@ -6,10 +6,12 @@ videoUrl: 'https://scrimba.com/c/cRbVZAB'
|
||||
forumTopicId: 18236
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
Another way to change the data in an array is with the <code>.pop()</code> function.
|
||||
<code>.pop()</code> is used to "pop" a value off of the end of an array. We can store this "popped off" value by assigning it to a variable. In other words, <code>.pop()</code> removes the last element from an array and returns that element.
|
||||
# --description--
|
||||
|
||||
Another way to change the data in an array is with the `.pop()` function.
|
||||
|
||||
`.pop()` is used to "pop" a value off of the end of an array. We can store this "popped off" value by assigning it to a variable. In other words, `.pop()` removes the last element from an array and returns that element.
|
||||
|
||||
Any type of entry can be "popped" off of an array - numbers, strings, even nested arrays.
|
||||
|
||||
```js
|
||||
@@ -19,33 +21,55 @@ console.log(oneDown); // Returns 6
|
||||
console.log(threeArr); // Returns [1, 4]
|
||||
```
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Use the <code>.pop()</code> function to remove the last item from <code>myArray</code>, assigning the "popped off" value to <code>removedFromMyArray</code>.
|
||||
</section>
|
||||
Use the `.pop()` function to remove the last item from `myArray`, assigning the "popped off" value to `removedFromMyArray`.
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>myArray</code> should only contain <code>[["John", 23]]</code>.
|
||||
testString: assert((function(d){if(d[0][0] == 'John' && d[0][1] === 23 && d[1] == undefined){return true;}else{return false;}})(myArray));
|
||||
- text: You should use <code>pop()</code> on <code>myArray</code>.
|
||||
testString: assert(/removedFromMyArray\s*=\s*myArray\s*.\s*pop\s*(\s*)/.test(code));
|
||||
- text: <code>removedFromMyArray</code> should only contain <code>["cat", 2]</code>.
|
||||
testString: assert((function(d){if(d[0] == 'cat' && d[1] === 2 && d[2] == undefined){return true;}else{return false;}})(removedFromMyArray));
|
||||
`myArray` should only contain `[["John", 23]]`.
|
||||
|
||||
```js
|
||||
assert(
|
||||
(function (d) {
|
||||
if (d[0][0] == 'John' && d[0][1] === 23 && d[1] == undefined) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
})(myArray)
|
||||
);
|
||||
```
|
||||
|
||||
</section>
|
||||
You should use `pop()` on `myArray`.
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
```js
|
||||
assert(/removedFromMyArray\s*=\s*myArray\s*.\s*pop\s*(\s*)/.test(code));
|
||||
```
|
||||
|
||||
<div id='js-seed'>
|
||||
`removedFromMyArray` should only contain `["cat", 2]`.
|
||||
|
||||
```js
|
||||
assert(
|
||||
(function (d) {
|
||||
if (d[0] == 'cat' && d[1] === 2 && d[2] == undefined) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
})(removedFromMyArray)
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
(function(y, z){return 'myArray = ' + JSON.stringify(y) + ' & removedFromMyArray = ' + JSON.stringify(z);})(myArray, removedFromMyArray);
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
// Setup
|
||||
@@ -53,31 +77,11 @@ var myArray = [["John", 23], ["cat", 2]];
|
||||
|
||||
// Only change code below this line
|
||||
var removedFromMyArray;
|
||||
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
(function(y, z){return 'myArray = ' + JSON.stringify(y) + ' & removedFromMyArray = ' + JSON.stringify(z);})(myArray, removedFromMyArray);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
var myArray = [["John", 23], ["cat", 2]];
|
||||
var removedFromMyArray = myArray.pop();
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@@ -6,10 +6,11 @@ videoUrl: 'https://scrimba.com/c/cnqmVtJ'
|
||||
forumTopicId: 18237
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
An easy way to append data to the end of an array is via the <code>push()</code> function.
|
||||
<code>.push()</code> takes one or more <dfn>parameters</dfn> and "pushes" them onto the end of the array.
|
||||
# --description--
|
||||
|
||||
An easy way to append data to the end of an array is via the `push()` function.
|
||||
|
||||
`.push()` takes one or more <dfn>parameters</dfn> and "pushes" them onto the end of the array.
|
||||
|
||||
Examples:
|
||||
|
||||
@@ -23,60 +24,53 @@ arr2.push(["happy", "joy"]);
|
||||
// arr2 now equals ["Stimpson", "J", "cat", ["happy", "joy"]]
|
||||
```
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Push <code>["dog", 3]</code> onto the end of the <code>myArray</code> variable.
|
||||
</section>
|
||||
Push `["dog", 3]` onto the end of the `myArray` variable.
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>myArray</code> should now equal <code>[["John", 23], ["cat", 2], ["dog", 3]]</code>.
|
||||
testString: assert((function(d){if(d[2] != undefined && d[0][0] == 'John' && d[0][1] === 23 && d[2][0] == 'dog' && d[2][1] === 3 && d[2].length == 2){return true;}else{return false;}})(myArray));
|
||||
`myArray` should now equal `[["John", 23], ["cat", 2], ["dog", 3]]`.
|
||||
|
||||
```js
|
||||
assert(
|
||||
(function (d) {
|
||||
if (
|
||||
d[2] != undefined &&
|
||||
d[0][0] == 'John' &&
|
||||
d[0][1] === 23 &&
|
||||
d[2][0] == 'dog' &&
|
||||
d[2][1] === 3 &&
|
||||
d[2].length == 2
|
||||
) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
})(myArray)
|
||||
);
|
||||
```
|
||||
|
||||
</section>
|
||||
# --seed--
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
## --after-user-code--
|
||||
|
||||
<div id='js-seed'>
|
||||
```js
|
||||
(function(z){return 'myArray = ' + JSON.stringify(z);})(myArray);
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
// Setup
|
||||
var myArray = [["John", 23], ["cat", 2]];
|
||||
|
||||
// Only change code below this line
|
||||
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
(function(z){return 'myArray = ' + JSON.stringify(z);})(myArray);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
var myArray = [["John", 23], ["cat", 2]];
|
||||
myArray.push(["dog",3]);
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@@ -6,10 +6,11 @@ videoUrl: 'https://scrimba.com/c/cRbVETW'
|
||||
forumTopicId: 18238
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
<code>pop()</code> always removes the last element of an array. What if you want to remove the first?
|
||||
That's where <code>.shift()</code> comes in. It works just like <code>.pop()</code>, except it removes the first element instead of the last.
|
||||
# --description--
|
||||
|
||||
`pop()` always removes the last element of an array. What if you want to remove the first?
|
||||
|
||||
That's where `.shift()` comes in. It works just like `.pop()`, except it removes the first element instead of the last.
|
||||
|
||||
Example:
|
||||
|
||||
@@ -19,31 +20,53 @@ var removedFromOurArray = ourArray.shift();
|
||||
// removedFromOurArray now equals "Stimpson" and ourArray now equals ["J", ["cat"]].
|
||||
```
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Use the <code>.shift()</code> function to remove the first item from <code>myArray</code>, assigning the "shifted off" value to <code>removedFromMyArray</code>.
|
||||
</section>
|
||||
Use the `.shift()` function to remove the first item from `myArray`, assigning the "shifted off" value to `removedFromMyArray`.
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>myArray</code> should now equal <code>[["dog", 3]]</code>.
|
||||
testString: assert((function(d){if(d[0][0] == 'dog' && d[0][1] === 3 && d[1] == undefined){return true;}else{return false;}})(myArray));
|
||||
- text: <code>removedFromMyArray</code> should contain <code>["John", 23]</code>.
|
||||
testString: assert((function(d){if(d[0] == 'John' && d[1] === 23 && typeof removedFromMyArray === 'object'){return true;}else{return false;}})(removedFromMyArray));
|
||||
`myArray` should now equal `[["dog", 3]]`.
|
||||
|
||||
```js
|
||||
assert(
|
||||
(function (d) {
|
||||
if (d[0][0] == 'dog' && d[0][1] === 3 && d[1] == undefined) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
})(myArray)
|
||||
);
|
||||
```
|
||||
|
||||
</section>
|
||||
`removedFromMyArray` should contain `["John", 23]`.
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
```js
|
||||
assert(
|
||||
(function (d) {
|
||||
if (
|
||||
d[0] == 'John' &&
|
||||
d[1] === 23 &&
|
||||
typeof removedFromMyArray === 'object'
|
||||
) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
})(removedFromMyArray)
|
||||
);
|
||||
```
|
||||
|
||||
<div id='js-seed'>
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
(function(y, z){return 'myArray = ' + JSON.stringify(y) + ' & removedFromMyArray = ' + JSON.stringify(z);})(myArray, removedFromMyArray);
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
// Setup
|
||||
@@ -51,27 +74,9 @@ var myArray = [["John", 23], ["dog", 3]];
|
||||
|
||||
// Only change code below this line
|
||||
var removedFromMyArray;
|
||||
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
(function(y, z){return 'myArray = ' + JSON.stringify(y) + ' & removedFromMyArray = ' + JSON.stringify(z);})(myArray, removedFromMyArray);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
var myArray = [["John", 23], ["dog", 3]];
|
||||
@@ -79,5 +84,3 @@ var myArray = [["John", 23], ["dog", 3]];
|
||||
// Only change code below this line
|
||||
var removedFromMyArray = myArray.shift();
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@@ -6,10 +6,11 @@ videoUrl: 'https://scrimba.com/c/ckNDESv'
|
||||
forumTopicId: 18239
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
Not only can you <code>shift</code> elements off of the beginning of an array, you can also <code>unshift</code> elements to the beginning of an array i.e. add elements in front of the array.
|
||||
<code>.unshift()</code> works exactly like <code>.push()</code>, but instead of adding the element at the end of the array, <code>unshift()</code> adds the element at the beginning of the array.
|
||||
# --description--
|
||||
|
||||
Not only can you `shift` elements off of the beginning of an array, you can also `unshift` elements to the beginning of an array i.e. add elements in front of the array.
|
||||
|
||||
`.unshift()` works exactly like `.push()`, but instead of adding the element at the end of the array, `unshift()` adds the element at the beginning of the array.
|
||||
|
||||
Example:
|
||||
|
||||
@@ -20,29 +21,43 @@ ourArray.unshift("Happy");
|
||||
// ourArray now equals ["Happy", "J", "cat"]
|
||||
```
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Add <code>["Paul",35]</code> to the beginning of the <code>myArray</code> variable using <code>unshift()</code>.
|
||||
</section>
|
||||
Add `["Paul",35]` to the beginning of the `myArray` variable using `unshift()`.
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>myArray</code> should now have [["Paul", 35], ["dog", 3]].
|
||||
testString: assert((function(d){if(typeof d[0] === "object" && d[0][0] == 'Paul' && d[0][1] === 35 && d[1][0] != undefined && d[1][0] == 'dog' && d[1][1] != undefined && d[1][1] == 3){return true;}else{return false;}})(myArray));
|
||||
`myArray` should now have \[["Paul", 35], ["dog", 3]].
|
||||
|
||||
```js
|
||||
assert(
|
||||
(function (d) {
|
||||
if (
|
||||
typeof d[0] === 'object' &&
|
||||
d[0][0] == 'Paul' &&
|
||||
d[0][1] === 35 &&
|
||||
d[1][0] != undefined &&
|
||||
d[1][0] == 'dog' &&
|
||||
d[1][1] != undefined &&
|
||||
d[1][1] == 3
|
||||
) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
})(myArray)
|
||||
);
|
||||
```
|
||||
|
||||
</section>
|
||||
# --seed--
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
## --after-user-code--
|
||||
|
||||
<div id='js-seed'>
|
||||
```js
|
||||
(function(y, z){return 'myArray = ' + JSON.stringify(y);})(myArray);
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
// Setup
|
||||
@@ -50,32 +65,12 @@ var myArray = [["John", 23], ["dog", 3]];
|
||||
myArray.shift();
|
||||
|
||||
// Only change code below this line
|
||||
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
(function(y, z){return 'myArray = ' + JSON.stringify(y);})(myArray);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
var myArray = [["John", 23], ["dog", 3]];
|
||||
myArray.shift();
|
||||
myArray.unshift(["Paul", 35]);
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@@ -6,9 +6,10 @@ videoUrl: 'https://scrimba.com/c/c9yNMfR'
|
||||
forumTopicId: 18208
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
# --description--
|
||||
|
||||
Sometimes you may want to store data in a flexible <dfn>Data Structure</dfn>. A JavaScript object is one way to handle flexible data. They allow for arbitrary combinations of <dfn>strings</dfn>, <dfn>numbers</dfn>, <dfn>booleans</dfn>, <dfn>arrays</dfn>, <dfn>functions</dfn>, and <dfn>objects</dfn>.
|
||||
|
||||
Here's an example of a complex data structure:
|
||||
|
||||
```js
|
||||
@@ -27,9 +28,7 @@ var ourMusic = [
|
||||
];
|
||||
```
|
||||
|
||||
This is an array which contains one object inside. The object has various pieces of <dfn>metadata</dfn> about an album. It also has a nested <code>"formats"</code> array. If you want to add more album records, you can do this by adding records to the top level array.
|
||||
Objects hold data in a property, which has a key-value format. In the example above, <code>"artist": "Daft Punk"</code> is a property that has a key of <code>"artist"</code> and a value of <code>"Daft Punk"</code>.
|
||||
<a href='http://www.json.org/' target=_blank>JavaScript Object Notation</a> or <code>JSON</code> is a related data interchange format used to store data.
|
||||
This is an array which contains one object inside. The object has various pieces of <dfn>metadata</dfn> about an album. It also has a nested `"formats"` array. If you want to add more album records, you can do this by adding records to the top level array. Objects hold data in a property, which has a key-value format. In the example above, `"artist": "Daft Punk"` is a property that has a key of `"artist"` and a value of `"Daft Punk"`. [JavaScript Object Notation](http://www.json.org/) or `JSON` is a related data interchange format used to store data.
|
||||
|
||||
```json
|
||||
{
|
||||
@@ -45,46 +44,91 @@ Objects hold data in a property, which has a key-value format. In the example ab
|
||||
}
|
||||
```
|
||||
|
||||
<strong>Note</strong><br>You will need to place a comma after every object in the array, unless it is the last object in the array.
|
||||
</section>
|
||||
**Note**
|
||||
You will need to place a comma after every object in the array, unless it is the last object in the array.
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Add a new album to the <code>myMusic</code> array. Add <code>artist</code> and <code>title</code> strings, <code>release_year</code> number, and a <code>formats</code> array of strings.
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
Add a new album to the `myMusic` array. Add `artist` and `title` strings, `release_year` number, and a `formats` array of strings.
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>myMusic</code> should be an array
|
||||
testString: assert(Array.isArray(myMusic));
|
||||
- text: <code>myMusic</code> should have at least two elements
|
||||
testString: assert(myMusic.length > 1);
|
||||
- text: <code>myMusic[1]</code> should be an object
|
||||
testString: assert(typeof myMusic[1] === 'object');
|
||||
- text: <code>myMusic[1]</code> should have at least 4 properties
|
||||
testString: assert(Object.keys(myMusic[1]).length > 3);
|
||||
- text: <code>myMusic[1]</code> should contain an <code>artist</code> property which is a string
|
||||
testString: assert(myMusic[1].hasOwnProperty('artist') && typeof myMusic[1].artist === 'string');
|
||||
- text: <code>myMusic[1]</code> should contain a <code>title</code> property which is a string
|
||||
testString: assert(myMusic[1].hasOwnProperty('title') && typeof myMusic[1].title === 'string');
|
||||
- text: <code>myMusic[1]</code> should contain a <code>release_year</code> property which is a number
|
||||
testString: assert(myMusic[1].hasOwnProperty('release_year') && typeof myMusic[1].release_year === 'number');
|
||||
- text: <code>myMusic[1]</code> should contain a <code>formats</code> property which is an array
|
||||
testString: assert(myMusic[1].hasOwnProperty('formats') && Array.isArray(myMusic[1].formats));
|
||||
- text: <code>formats</code> should be an array of strings with at least two elements
|
||||
testString: assert(myMusic[1].formats.every(function(item) { return (typeof item === "string")}) && myMusic[1].formats.length > 1);
|
||||
# --hints--
|
||||
|
||||
`myMusic` should be an array
|
||||
|
||||
```js
|
||||
assert(Array.isArray(myMusic));
|
||||
```
|
||||
|
||||
</section>
|
||||
`myMusic` should have at least two elements
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
```js
|
||||
assert(myMusic.length > 1);
|
||||
```
|
||||
|
||||
<div id='js-seed'>
|
||||
`myMusic[1]` should be an object
|
||||
|
||||
```js
|
||||
assert(typeof myMusic[1] === 'object');
|
||||
```
|
||||
|
||||
`myMusic[1]` should have at least 4 properties
|
||||
|
||||
```js
|
||||
assert(Object.keys(myMusic[1]).length > 3);
|
||||
```
|
||||
|
||||
`myMusic[1]` should contain an `artist` property which is a string
|
||||
|
||||
```js
|
||||
assert(
|
||||
myMusic[1].hasOwnProperty('artist') && typeof myMusic[1].artist === 'string'
|
||||
);
|
||||
```
|
||||
|
||||
`myMusic[1]` should contain a `title` property which is a string
|
||||
|
||||
```js
|
||||
assert(
|
||||
myMusic[1].hasOwnProperty('title') && typeof myMusic[1].title === 'string'
|
||||
);
|
||||
```
|
||||
|
||||
`myMusic[1]` should contain a `release_year` property which is a number
|
||||
|
||||
```js
|
||||
assert(
|
||||
myMusic[1].hasOwnProperty('release_year') &&
|
||||
typeof myMusic[1].release_year === 'number'
|
||||
);
|
||||
```
|
||||
|
||||
`myMusic[1]` should contain a `formats` property which is an array
|
||||
|
||||
```js
|
||||
assert(
|
||||
myMusic[1].hasOwnProperty('formats') && Array.isArray(myMusic[1].formats)
|
||||
);
|
||||
```
|
||||
|
||||
`formats` should be an array of strings with at least two elements
|
||||
|
||||
```js
|
||||
assert(
|
||||
myMusic[1].formats.every(function (item) {
|
||||
return typeof item === 'string';
|
||||
}) && myMusic[1].formats.length > 1
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
(function(x){ if (Array.isArray(x)) { return JSON.stringify(x); } return "myMusic is not an array"})(myMusic);
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
var myMusic = [
|
||||
@@ -101,26 +145,9 @@ var myMusic = [
|
||||
}
|
||||
// Add a record here
|
||||
];
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
(function(x){ if (Array.isArray(x)) { return JSON.stringify(x); } return "myMusic is not an array"})(myMusic);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
var myMusic = [
|
||||
@@ -147,5 +174,3 @@ var myMusic = [
|
||||
}
|
||||
];
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@@ -6,73 +6,79 @@ videoUrl: 'https://scrimba.com/c/czQM4A8'
|
||||
forumTopicId: 18241
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
# --description--
|
||||
|
||||
Unlike strings, the entries of arrays are <dfn>mutable</dfn> and can be changed freely.
|
||||
<strong>Example</strong>
|
||||
|
||||
**Example**
|
||||
|
||||
```js
|
||||
var ourArray = [50,40,30];
|
||||
ourArray[0] = 15; // equals [15,40,30]
|
||||
```
|
||||
|
||||
<strong>Note</strong><br>There shouldn't be any spaces between the array name and the square brackets, like <code>array [0]</code>. Although JavaScript is able to process this correctly, this may confuse other programmers reading your code.
|
||||
</section>
|
||||
**Note**
|
||||
There shouldn't be any spaces between the array name and the square brackets, like `array [0]`. Although JavaScript is able to process this correctly, this may confuse other programmers reading your code.
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Modify the data stored at index <code>0</code> of <code>myArray</code> to a value of <code>45</code>.
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
Modify the data stored at index `0` of `myArray` to a value of `45`.
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>myArray</code> should now be [45,64,99].
|
||||
testString: assert((function(){if(typeof myArray != 'undefined' && myArray[0] == 45 && myArray[1] == 64 && myArray[2] == 99){return true;}else{return false;}})());
|
||||
- text: You should be using correct index to modify the value in <code>myArray</code>.
|
||||
testString: assert((function(){if(code.match(/myArray\[0\]\s*=\s*/g)){return true;}else{return false;}})());
|
||||
# --hints--
|
||||
|
||||
`myArray` should now be [45,64,99].
|
||||
|
||||
```js
|
||||
assert(
|
||||
(function () {
|
||||
if (
|
||||
typeof myArray != 'undefined' &&
|
||||
myArray[0] == 45 &&
|
||||
myArray[1] == 64 &&
|
||||
myArray[2] == 99
|
||||
) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
})()
|
||||
);
|
||||
```
|
||||
|
||||
</section>
|
||||
You should be using correct index to modify the value in `myArray`.
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
```js
|
||||
assert(
|
||||
(function () {
|
||||
if (code.match(/myArray\[0\]\s*=\s*/g)) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
})()
|
||||
);
|
||||
```
|
||||
|
||||
<div id='js-seed'>
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
if(typeof myArray !== "undefined"){(function(){return myArray;})();}
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
// Setup
|
||||
var myArray = [18,64,99];
|
||||
|
||||
// Only change code below this line
|
||||
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
if(typeof myArray !== "undefined"){(function(){return myArray;})();}
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
var myArray = [18,64,99];
|
||||
myArray[0] = 45;
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@@ -6,9 +6,9 @@ videoUrl: 'https://scrimba.com/c/cdBKWCV'
|
||||
forumTopicId: 18242
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
If the <code>break</code> statement is omitted from a <code>switch</code> statement's <code>case</code>, the following <code>case</code> statement(s) are executed until a <code>break</code> is encountered. If you have multiple inputs with the same output, you can represent them in a <code>switch</code> statement like this:
|
||||
# --description--
|
||||
|
||||
If the `break` statement is omitted from a `switch` statement's `case`, the following `case` statement(s) are executed until a `break` is encountered. If you have multiple inputs with the same output, you can represent them in a `switch` statement like this:
|
||||
|
||||
```js
|
||||
var result = "";
|
||||
@@ -24,50 +24,88 @@ switch(val) {
|
||||
```
|
||||
|
||||
Cases for 1, 2, and 3 will all produce the same result.
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Write a switch statement to set <code>answer</code> for the following ranges:<br><code>1-3</code> - "Low"<br><code>4-6</code> - "Mid"<br><code>7-9</code> - "High"
|
||||
<strong>Note</strong><br>You will need to have a <code>case</code> statement for each number in the range.
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
Write a switch statement to set `answer` for the following ranges:
|
||||
`1-3` - "Low"
|
||||
`4-6` - "Mid"
|
||||
`7-9` - "High"
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>sequentialSizes(1)</code> should return "Low"
|
||||
testString: assert(sequentialSizes(1) === "Low");
|
||||
- text: <code>sequentialSizes(2)</code> should return "Low"
|
||||
testString: assert(sequentialSizes(2) === "Low");
|
||||
- text: <code>sequentialSizes(3)</code> should return "Low"
|
||||
testString: assert(sequentialSizes(3) === "Low");
|
||||
- text: <code>sequentialSizes(4)</code> should return "Mid"
|
||||
testString: assert(sequentialSizes(4) === "Mid");
|
||||
- text: <code>sequentialSizes(5)</code> should return "Mid"
|
||||
testString: assert(sequentialSizes(5) === "Mid");
|
||||
- text: <code>sequentialSizes(6)</code> should return "Mid"
|
||||
testString: assert(sequentialSizes(6) === "Mid");
|
||||
- text: <code>sequentialSizes(7)</code> should return "High"
|
||||
testString: assert(sequentialSizes(7) === "High");
|
||||
- text: <code>sequentialSizes(8)</code> should return "High"
|
||||
testString: assert(sequentialSizes(8) === "High");
|
||||
- text: <code>sequentialSizes(9)</code> should return "High"
|
||||
testString: assert(sequentialSizes(9) === "High");
|
||||
- text: You should not use any <code>if</code> or <code>else</code> statements
|
||||
testString: assert(!/else/g.test(code) || !/if/g.test(code));
|
||||
- text: You should have nine <code>case</code> statements
|
||||
testString: assert(code.match(/case/g).length === 9);
|
||||
**Note**
|
||||
You will need to have a `case` statement for each number in the range.
|
||||
|
||||
# --hints--
|
||||
|
||||
`sequentialSizes(1)` should return "Low"
|
||||
|
||||
```js
|
||||
assert(sequentialSizes(1) === 'Low');
|
||||
```
|
||||
|
||||
</section>
|
||||
`sequentialSizes(2)` should return "Low"
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
```js
|
||||
assert(sequentialSizes(2) === 'Low');
|
||||
```
|
||||
|
||||
<div id='js-seed'>
|
||||
`sequentialSizes(3)` should return "Low"
|
||||
|
||||
```js
|
||||
assert(sequentialSizes(3) === 'Low');
|
||||
```
|
||||
|
||||
`sequentialSizes(4)` should return "Mid"
|
||||
|
||||
```js
|
||||
assert(sequentialSizes(4) === 'Mid');
|
||||
```
|
||||
|
||||
`sequentialSizes(5)` should return "Mid"
|
||||
|
||||
```js
|
||||
assert(sequentialSizes(5) === 'Mid');
|
||||
```
|
||||
|
||||
`sequentialSizes(6)` should return "Mid"
|
||||
|
||||
```js
|
||||
assert(sequentialSizes(6) === 'Mid');
|
||||
```
|
||||
|
||||
`sequentialSizes(7)` should return "High"
|
||||
|
||||
```js
|
||||
assert(sequentialSizes(7) === 'High');
|
||||
```
|
||||
|
||||
`sequentialSizes(8)` should return "High"
|
||||
|
||||
```js
|
||||
assert(sequentialSizes(8) === 'High');
|
||||
```
|
||||
|
||||
`sequentialSizes(9)` should return "High"
|
||||
|
||||
```js
|
||||
assert(sequentialSizes(9) === 'High');
|
||||
```
|
||||
|
||||
You should not use any `if` or `else` statements
|
||||
|
||||
```js
|
||||
assert(!/else/g.test(code) || !/if/g.test(code));
|
||||
```
|
||||
|
||||
You should have nine `case` statements
|
||||
|
||||
```js
|
||||
assert(code.match(/case/g).length === 9);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function sequentialSizes(val) {
|
||||
@@ -81,18 +119,9 @@ function sequentialSizes(val) {
|
||||
}
|
||||
|
||||
sequentialSizes(1);
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function sequentialSizes(val) {
|
||||
@@ -118,5 +147,3 @@ function sequentialSizes(val) {
|
||||
return answer;
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@@ -6,62 +6,46 @@ videoUrl: 'https://scrimba.com/c/ce2GeHq'
|
||||
forumTopicId: 301173
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
# --description--
|
||||
|
||||
In JavaScript, you can also perform calculations with decimal numbers, just like whole numbers.
|
||||
|
||||
Let's multiply two decimals together to get their product.
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Change the <code>0.0</code> so that product will equal <code>5.0</code>.
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
Change the `0.0` so that product will equal `5.0`.
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: The variable <code>product</code> should equal <code>5.0</code>.
|
||||
testString: assert(product === 5.0);
|
||||
- text: You should use the <code>*</code> operator
|
||||
testString: assert(/\*/.test(code));
|
||||
# --hints--
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
The variable `product` should equal `5.0`.
|
||||
|
||||
```js
|
||||
var product = 2.0 * 0.0;
|
||||
|
||||
|
||||
assert(product === 5.0);
|
||||
```
|
||||
|
||||
</div>
|
||||
You should use the `*` operator
|
||||
|
||||
```js
|
||||
assert(/\*/.test(code));
|
||||
```
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
(function(y){return 'product = '+y;})(product);
|
||||
```
|
||||
|
||||
</div>
|
||||
## --seed-contents--
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
```js
|
||||
var product = 2.0 * 0.0;
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
var product = 2.0 * 2.5;
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@@ -6,70 +6,52 @@ videoUrl: 'https://scrimba.com/c/cP3y3Aq'
|
||||
forumTopicId: 18243
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
We can also multiply one number by another.
|
||||
JavaScript uses the <code>*</code> symbol for multiplication of two numbers.
|
||||
# --description--
|
||||
|
||||
<strong>Example</strong>
|
||||
We can also multiply one number by another.
|
||||
|
||||
JavaScript uses the `*` symbol for multiplication of two numbers.
|
||||
|
||||
**Example**
|
||||
|
||||
```js
|
||||
myVar = 13 * 13; // assigned 169
|
||||
```
|
||||
|
||||
# --instructions--
|
||||
|
||||
</section>
|
||||
Change the `0` so that product will equal `80`.
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Change the <code>0</code> so that product will equal <code>80</code>.
|
||||
</section>
|
||||
# --hints--
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: The variable <code>product</code> should be equal to 80.
|
||||
testString: assert(product === 80);
|
||||
- text: You should use the <code>*</code> operator.
|
||||
testString: assert(/\*/.test(code));
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
The variable `product` should be equal to 80.
|
||||
|
||||
```js
|
||||
var product = 8 * 0;
|
||||
|
||||
|
||||
assert(product === 80);
|
||||
```
|
||||
|
||||
</div>
|
||||
You should use the `*` operator.
|
||||
|
||||
```js
|
||||
assert(/\*/.test(code));
|
||||
```
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
(function(z){return 'product = '+z;})(product);
|
||||
```
|
||||
|
||||
</div>
|
||||
## --seed-contents--
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
```js
|
||||
var product = 8 * 0;
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
var product = 8 * 10;
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@@ -6,65 +6,45 @@ videoUrl: 'https://scrimba.com/c/crZQZf8'
|
||||
forumTopicId: 18247
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
# --description--
|
||||
|
||||
You can also nest arrays within other arrays, like below:
|
||||
|
||||
```js
|
||||
[["Bulls", 23], ["White Sox", 45]]
|
||||
```
|
||||
|
||||
This is also called a <dfn>multi-dimensional array<dfn>.
|
||||
</section>
|
||||
This is also called a <dfn>multi-dimensional array<dfn>.</dfn></dfn>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Create a nested array called <code>myArray</code>.
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
Create a nested array called `myArray`.
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>myArray</code> should have at least one array nested within another array.
|
||||
testString: assert(Array.isArray(myArray) && myArray.some(Array.isArray));
|
||||
# --hints--
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`myArray` should have at least one array nested within another array.
|
||||
|
||||
```js
|
||||
// Only change code below this line
|
||||
var myArray = [];
|
||||
|
||||
assert(Array.isArray(myArray) && myArray.some(Array.isArray));
|
||||
```
|
||||
|
||||
</div>
|
||||
# --seed--
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
if(typeof myArray !== "undefined"){(function(){return myArray;})();}
|
||||
```
|
||||
|
||||
</div>
|
||||
## --seed-contents--
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
```js
|
||||
// Only change code below this line
|
||||
var myArray = [];
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
var myArray = [[1,2,3]];
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@@ -6,8 +6,8 @@ videoUrl: 'https://scrimba.com/c/cRn6GHM'
|
||||
forumTopicId: 18248
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
# --description--
|
||||
|
||||
If you have a multi-dimensional array, you can use the same logic as the prior waypoint to loop through both the array and any sub-arrays. Here is an example:
|
||||
|
||||
```js
|
||||
@@ -21,34 +21,47 @@ for (var i=0; i < arr.length; i++) {
|
||||
}
|
||||
```
|
||||
|
||||
This outputs each sub-element in <code>arr</code> one at a time. Note that for the inner loop, we are checking the <code>.length</code> of <code>arr[i]</code>, since <code>arr[i]</code> is itself an array.
|
||||
</section>
|
||||
This outputs each sub-element in `arr` one at a time. Note that for the inner loop, we are checking the `.length` of `arr[i]`, since `arr[i]` is itself an array.
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Modify function <code>multiplyAll</code> so that it returns the product of all the numbers in the sub-arrays of <code>arr</code>.
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
Modify function `multiplyAll` so that it returns the product of all the numbers in the sub-arrays of `arr`.
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>multiplyAll([[1],[2],[3]])</code> should return <code>6</code>
|
||||
testString: assert(multiplyAll([[1],[2],[3]]) === 6);
|
||||
- text: <code>multiplyAll([[1,2],[3,4],[5,6,7]])</code> should return <code>5040</code>
|
||||
testString: assert(multiplyAll([[1,2],[3,4],[5,6,7]]) === 5040);
|
||||
- text: <code>multiplyAll([[5,1],[0.2, 4, 0.5],[3, 9]])</code> should return <code>54</code>
|
||||
testString: assert(multiplyAll([[5,1],[0.2, 4, 0.5],[3, 9]]) === 54);
|
||||
# --hints--
|
||||
|
||||
`multiplyAll([[1],[2],[3]])` should return `6`
|
||||
|
||||
```js
|
||||
assert(multiplyAll([[1], [2], [3]]) === 6);
|
||||
```
|
||||
|
||||
</section>
|
||||
`multiplyAll([[1,2],[3,4],[5,6,7]])` should return `5040`
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
```js
|
||||
assert(
|
||||
multiplyAll([
|
||||
[1, 2],
|
||||
[3, 4],
|
||||
[5, 6, 7]
|
||||
]) === 5040
|
||||
);
|
||||
```
|
||||
|
||||
<div id='js-seed'>
|
||||
`multiplyAll([[5,1],[0.2, 4, 0.5],[3, 9]])` should return `54`
|
||||
|
||||
```js
|
||||
assert(
|
||||
multiplyAll([
|
||||
[5, 1],
|
||||
[0.2, 4, 0.5],
|
||||
[3, 9]
|
||||
]) === 54
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function multiplyAll(arr) {
|
||||
@@ -60,18 +73,9 @@ function multiplyAll(arr) {
|
||||
}
|
||||
|
||||
multiplyAll([[1,2],[3,4],[5,6,7]]);
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function multiplyAll(arr) {
|
||||
@@ -86,5 +90,3 @@ function multiplyAll(arr) {
|
||||
|
||||
multiplyAll([[1,2],[3,4],[5,6,7]]);
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@@ -6,10 +6,11 @@ videoUrl: 'https://scrimba.com/c/cy8rahW'
|
||||
forumTopicId: 18254
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
# --description--
|
||||
|
||||
<dfn>Parameters</dfn> are variables that act as placeholders for the values that are to be input to a function when it is called. When a function is defined, it is typically defined along with one or more parameters. The actual values that are input (or <dfn>"passed"</dfn>) into a function when it is called are known as <dfn>arguments</dfn>.
|
||||
Here is a function with two parameters, <code>param1</code> and <code>param2</code>:
|
||||
|
||||
Here is a function with two parameters, `param1` and `param2`:
|
||||
|
||||
```js
|
||||
function testFun(param1, param2) {
|
||||
@@ -17,48 +18,55 @@ function testFun(param1, param2) {
|
||||
}
|
||||
```
|
||||
|
||||
Then we can call <code>testFun</code>:
|
||||
<code>testFun("Hello", "World");</code>
|
||||
We have passed two arguments, <code>"Hello"</code> and <code>"World"</code>. Inside the function, <code>param1</code> will equal "Hello" and <code>param2</code> will equal "World". Note that you could call <code>testFun</code> again with different arguments and the parameters would take on the value of the new arguments.
|
||||
</section>
|
||||
Then we can call `testFun`: `testFun("Hello", "World");` We have passed two arguments, `"Hello"` and `"World"`. Inside the function, `param1` will equal "Hello" and `param2` will equal "World". Note that you could call `testFun` again with different arguments and the parameters would take on the value of the new arguments.
|
||||
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
<ol><li>Create a function called <code>functionWithArgs</code> that accepts two arguments and outputs their sum to the dev console.</li><li>Call the function with two numbers as arguments.</li></ol>
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>functionWithArgs</code> should be a function.
|
||||
testString: assert(typeof functionWithArgs === 'function');
|
||||
- text: <code>functionWithArgs(1,2)</code> should output <code>3</code>.
|
||||
testString: if(typeof functionWithArgs === "function") { capture(); functionWithArgs(1,2); uncapture(); } assert(logOutput == 3);
|
||||
- text: <code>functionWithArgs(7,9)</code> should output <code>16</code>.
|
||||
testString: if(typeof functionWithArgs === "function") { capture(); functionWithArgs(7,9); uncapture(); } assert(logOutput == 16);
|
||||
- text: You should call <code>functionWithArgs</code> with two numbers after you define it.
|
||||
testString: assert(/functionWithArgs\([-+]?\d*\.?\d*,[-+]?\d*\.?\d*\)/.test(code.replace(/\s/g, '')));
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`functionWithArgs` should be a function.
|
||||
|
||||
```js
|
||||
|
||||
|
||||
|
||||
assert(typeof functionWithArgs === 'function');
|
||||
```
|
||||
|
||||
</div>
|
||||
`functionWithArgs(1,2)` should output `3`.
|
||||
|
||||
### Before Test
|
||||
<div id='js-setup'>
|
||||
```js
|
||||
if (typeof functionWithArgs === 'function') {
|
||||
capture();
|
||||
functionWithArgs(1, 2);
|
||||
uncapture();
|
||||
}
|
||||
assert(logOutput == 3);
|
||||
```
|
||||
|
||||
`functionWithArgs(7,9)` should output `16`.
|
||||
|
||||
```js
|
||||
if (typeof functionWithArgs === 'function') {
|
||||
capture();
|
||||
functionWithArgs(7, 9);
|
||||
uncapture();
|
||||
}
|
||||
assert(logOutput == 16);
|
||||
```
|
||||
|
||||
You should call `functionWithArgs` with two numbers after you define it.
|
||||
|
||||
```js
|
||||
assert(
|
||||
/functionWithArgs\([-+]?\d*\.?\d*,[-+]?\d*\.?\d*\)/.test(
|
||||
code.replace(/\s/g, '')
|
||||
)
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --before-user-code--
|
||||
|
||||
```js
|
||||
var logOutput = "";
|
||||
@@ -83,10 +91,7 @@ function uncapture() {
|
||||
capture();
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
uncapture();
|
||||
@@ -98,13 +103,12 @@ if (typeof functionWithArgs !== "function") {
|
||||
}
|
||||
```
|
||||
|
||||
</div>
|
||||
## --seed-contents--
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
```js
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function functionWithArgs(a, b) {
|
||||
@@ -112,5 +116,3 @@ function functionWithArgs(a, b) {
|
||||
}
|
||||
functionWithArgs(10, 5);
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@@ -6,51 +6,54 @@ videoUrl: 'https://scrimba.com/c/cm8PqCa'
|
||||
forumTopicId: 301174
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
In the last two challenges, we learned about the equality operator (<code>==</code>) and the strict equality operator (<code>===</code>). Let's do a quick review and practice using these operators some more.
|
||||
# --description--
|
||||
|
||||
In the last two challenges, we learned about the equality operator (`==`) and the strict equality operator (`===`). Let's do a quick review and practice using these operators some more.
|
||||
|
||||
If the values being compared are not of the same type, the equality operator will perform a type conversion, and then evaluate the values. However, the strict equality operator will compare both the data type and value as-is, without converting one type to the other.
|
||||
<strong>Examples</strong>
|
||||
|
||||
**Examples**
|
||||
|
||||
```js
|
||||
3 == '3' // returns true because JavaScript performs type conversion from string to number
|
||||
3 === '3' // returns false because the types are different and type conversion is not performed
|
||||
```
|
||||
|
||||
<strong>Note</strong><br>In JavaScript, you can determine the type of a variable or a value with the <code>typeof</code> operator, as follows:
|
||||
**Note**
|
||||
In JavaScript, you can determine the type of a variable or a value with the `typeof` operator, as follows:
|
||||
|
||||
```js
|
||||
typeof 3 // returns 'number'
|
||||
typeof '3' // returns 'string'
|
||||
```
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
The <code>compareEquality</code> function in the editor compares two values using the equality operator. Modify the function so that it returns "Equal" only when the values are strictly equal.
|
||||
</section>
|
||||
The `compareEquality` function in the editor compares two values using the equality operator. Modify the function so that it returns "Equal" only when the values are strictly equal.
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>compareEquality(10, "10")</code> should return "Not Equal"
|
||||
testString: assert(compareEquality(10, "10") === "Not Equal");
|
||||
- text: <code>compareEquality("20", 20)</code> should return "Not Equal"
|
||||
testString: assert(compareEquality("20", 20) === "Not Equal");
|
||||
- text: You should use the <code>===</code> operator
|
||||
testString: assert(code.match(/===/g));
|
||||
`compareEquality(10, "10")` should return "Not Equal"
|
||||
|
||||
```js
|
||||
assert(compareEquality(10, '10') === 'Not Equal');
|
||||
```
|
||||
|
||||
</section>
|
||||
`compareEquality("20", 20)` should return "Not Equal"
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
```js
|
||||
assert(compareEquality('20', 20) === 'Not Equal');
|
||||
```
|
||||
|
||||
<div id='js-seed'>
|
||||
You should use the `===` operator
|
||||
|
||||
```js
|
||||
assert(code.match(/===/g));
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
// Setup
|
||||
@@ -64,15 +67,7 @@ function compareEquality(a, b) {
|
||||
compareEquality(10, "10");
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function compareEquality(a,b) {
|
||||
@@ -82,5 +77,3 @@ function compareEquality(a,b) {
|
||||
return "Not Equal";
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@@ -6,47 +6,64 @@ videoUrl: 'https://scrimba.com/c/cDqW2Cg'
|
||||
forumTopicId: 18259
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
# --description--
|
||||
|
||||
We have an array of objects representing different people in our contacts lists.
|
||||
A <code>lookUpProfile</code> function that takes <code>name</code> and a property (<code>prop</code>) as arguments has been pre-written for you.
|
||||
The function should check if <code>name</code> is an actual contact's <code>firstName</code> and the given property (<code>prop</code>) is a property of that contact.
|
||||
|
||||
A `lookUpProfile` function that takes `name` and a property (`prop`) as arguments has been pre-written for you.
|
||||
|
||||
The function should check if `name` is an actual contact's `firstName` and the given property (`prop`) is a property of that contact.
|
||||
|
||||
If both are true, then return the "value" of that property.
|
||||
If <code>name</code> does not correspond to any contacts then return <code>"No such contact"</code>.
|
||||
If <code>prop</code> does not correspond to any valid properties of a contact found to match <code>name</code> then return <code>"No such property"</code>.
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
If `name` does not correspond to any contacts then return `"No such contact"`.
|
||||
|
||||
</section>
|
||||
If `prop` does not correspond to any valid properties of a contact found to match `name` then return `"No such property"`.
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>lookUpProfile("Kristian", "lastName")</code> should return <code>"Vos"</code>
|
||||
testString: assert(lookUpProfile('Kristian','lastName') === "Vos");
|
||||
- text: <code>lookUpProfile("Sherlock", "likes")</code> should return <code>["Intriguing Cases", "Violin"]</code>
|
||||
testString: assert.deepEqual(lookUpProfile("Sherlock", "likes"), ["Intriguing Cases", "Violin"]);
|
||||
- text: <code>lookUpProfile("Harry", "likes")</code> should return an array
|
||||
testString: assert(typeof lookUpProfile("Harry", "likes") === "object");
|
||||
- text: <code>lookUpProfile("Bob", "number")</code> should return "No such contact"
|
||||
testString: assert(lookUpProfile("Bob", "number") === "No such contact");
|
||||
- text: <code>lookUpProfile("Bob", "potato")</code> should return "No such contact"
|
||||
testString: assert(lookUpProfile("Bob", "potato") === "No such contact");
|
||||
- text: <code>lookUpProfile("Akira", "address")</code> should return "No such property"
|
||||
testString: assert(lookUpProfile("Akira", "address") === "No such property");
|
||||
`lookUpProfile("Kristian", "lastName")` should return `"Vos"`
|
||||
|
||||
```js
|
||||
assert(lookUpProfile('Kristian', 'lastName') === 'Vos');
|
||||
```
|
||||
|
||||
</section>
|
||||
`lookUpProfile("Sherlock", "likes")` should return `["Intriguing Cases", "Violin"]`
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
```js
|
||||
assert.deepEqual(lookUpProfile('Sherlock', 'likes'), [
|
||||
'Intriguing Cases',
|
||||
'Violin'
|
||||
]);
|
||||
```
|
||||
|
||||
<div id='js-seed'>
|
||||
`lookUpProfile("Harry", "likes")` should return an array
|
||||
|
||||
```js
|
||||
assert(typeof lookUpProfile('Harry', 'likes') === 'object');
|
||||
```
|
||||
|
||||
`lookUpProfile("Bob", "number")` should return "No such contact"
|
||||
|
||||
```js
|
||||
assert(lookUpProfile('Bob', 'number') === 'No such contact');
|
||||
```
|
||||
|
||||
`lookUpProfile("Bob", "potato")` should return "No such contact"
|
||||
|
||||
```js
|
||||
assert(lookUpProfile('Bob', 'potato') === 'No such contact');
|
||||
```
|
||||
|
||||
`lookUpProfile("Akira", "address")` should return "No such property"
|
||||
|
||||
```js
|
||||
assert(lookUpProfile('Akira', 'address') === 'No such property');
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
// Setup
|
||||
@@ -87,15 +104,7 @@ function lookUpProfile(name, prop){
|
||||
lookUpProfile("Akira", "likes");
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
var contacts = [
|
||||
@@ -139,5 +148,3 @@ function lookUpProfile(name, prop){
|
||||
|
||||
lookUpProfile("Akira", "likes");
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@@ -6,8 +6,8 @@ videoUrl: 'https://scrimba.com/c/cbQmnhM'
|
||||
forumTopicId: 18260
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
# --description--
|
||||
|
||||
<dfn>String</dfn> values in JavaScript may be written with single or double quotes, as long as you start and end with the same type of quote. Unlike some other programming languages, single and double quotes work the same in JavaScript.
|
||||
|
||||
```js
|
||||
@@ -15,7 +15,7 @@ doubleQuoteStr = "This is a string";
|
||||
singleQuoteStr = 'This is also a string';
|
||||
```
|
||||
|
||||
The reason why you might want to use one type of quote over the other is if you want to use both in a string. This might happen if you want to save a conversation in a string and have the conversation in quotes. Another use for it would be saving an <code><a></code> tag with various attributes in quotes, all within a string.
|
||||
The reason why you might want to use one type of quote over the other is if you want to use both in a string. This might happen if you want to save a conversation in a string and have the conversation in quotes. Another use for it would be saving an `<a>` tag with various attributes in quotes, all within a string.
|
||||
|
||||
```js
|
||||
conversation = 'Finn exclaims to Jake, "Algebraic!"';
|
||||
@@ -28,61 +28,50 @@ goodStr = 'Jake asks Finn, "Hey, let\'s go on an adventure?"';
|
||||
badStr = 'Finn responds, "Let's go!"'; // Throws an error
|
||||
```
|
||||
|
||||
In the <dfn>goodStr</dfn> above, you can use both quotes safely by using the backslash <code>\</code> as an escape character.
|
||||
<strong>Note</strong><br/>The backslash <code>\</code> should not be confused with the forward slash <code>/</code>. They do not do the same thing.
|
||||
</section>
|
||||
In the <dfn>goodStr</dfn> above, you can use both quotes safely by using the backslash `\` as an escape character. **Note**
|
||||
The backslash `\` should not be confused with the forward slash `/`. They do not do the same thing.
|
||||
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Change the provided string to a string with single quotes at the beginning and end and no escape characters.
|
||||
Right now, the <code><a></code> tag in the string uses double quotes everywhere. You will need to change the outer quotes to single quotes so you can remove the escape characters.
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
Right now, the `<a>` tag in the string uses double quotes everywhere. You will need to change the outer quotes to single quotes so you can remove the escape characters.
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: You should remove all the <code>backslashes</code> (<code>\</code>).
|
||||
testString: assert(!/\\/g.test(code) && myStr.match('\\s*<a href\\s*=\\s*"http://www.example.com"\\s*target\\s*=\\s*"_blank">\\s*Link\\s*</a>\\s*'));
|
||||
- text: You should have two single quotes <code>'</code> and four double quotes <code>"</code>.
|
||||
testString: assert(code.match(/"/g).length === 4 && code.match(/'/g).length === 2);
|
||||
# --hints--
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
You should remove all the `backslashes` (`\`).
|
||||
|
||||
```js
|
||||
var myStr = "<a href=\"http://www.example.com\" target=\"_blank\">Link</a>";
|
||||
|
||||
|
||||
assert(
|
||||
!/\\/g.test(code) &&
|
||||
myStr.match(
|
||||
'\\s*<a href\\s*=\\s*"http://www.example.com"\\s*target\\s*=\\s*"_blank">\\s*Link\\s*</a>\\s*'
|
||||
)
|
||||
);
|
||||
```
|
||||
|
||||
</div>
|
||||
You should have two single quotes `'` and four double quotes `"`.
|
||||
|
||||
```js
|
||||
assert(code.match(/"/g).length === 4 && code.match(/'/g).length === 2);
|
||||
```
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
(function() { return "myStr = " + myStr; })();
|
||||
```
|
||||
|
||||
</div>
|
||||
## --seed-contents--
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
```js
|
||||
var myStr = "<a href=\"http://www.example.com\" target=\"_blank\">Link</a>";
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
var myStr = '<a href="http://www.example.com" target="_blank">Link</a>';
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@@ -5,57 +5,112 @@ challengeType: 1
|
||||
forumTopicId: 18261
|
||||
---
|
||||
|
||||
## Description
|
||||
|
||||
<section id='description'>
|
||||
# --description--
|
||||
|
||||
You are given a JSON object representing a part of your musical album collection. Each album has a unique id number as its key and several other properties. Not all albums have complete information.
|
||||
|
||||
You start with an `updateRecords` function that takes an object like `collection`, an `id`, a `prop` (like `artist` or `tracks`), and a `value`. Complete the function using the rules below to modify the object passed to the function.
|
||||
|
||||
- Your function must always return the entire object.
|
||||
- If `prop` isn't `tracks` and `value` isn't an empty string, update or set that album's `prop` to `value`.
|
||||
- If `prop` is `tracks` but the album doesn't have a `tracks` property, create an empty array and add `value` to it.
|
||||
- If `prop` is `tracks` and `value` isn't an empty string, add `value` to the end of the album's existing `tracks` array.
|
||||
- If `value` is an empty string, delete the given `prop` property from the album.
|
||||
- Your function must always return the entire object.
|
||||
- If `prop` isn't `tracks` and `value` isn't an empty string, update or set that album's `prop` to `value`.
|
||||
- If `prop` is `tracks` but the album doesn't have a `tracks` property, create an empty array and add `value` to it.
|
||||
- If `prop` is `tracks` and `value` isn't an empty string, add `value` to the end of the album's existing `tracks` array.
|
||||
- If `value` is an empty string, delete the given `prop` property from the album.
|
||||
|
||||
**Note:** A copy of the `collection` object is used for the tests.
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
# --hints--
|
||||
|
||||
<section id='instructions'>
|
||||
After `updateRecords(collection, 5439, "artist", "ABBA")`, `artist` should be `ABBA`
|
||||
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: After <code>updateRecords(collection, 5439, "artist", "ABBA")</code>, <code>artist</code> should be <code>ABBA</code>
|
||||
testString: assert(updateRecords(_recordCollection, 5439, "artist", "ABBA")[5439]["artist"] === "ABBA");
|
||||
- text: After <code>updateRecords(collection, 5439, "tracks", "Take a Chance on Me")</code>, <code>tracks</code> should have <code>Take a Chance on Me</code> as the last element.
|
||||
testString: assert(updateRecords(_recordCollection, 5439, "tracks", "Take a Chance on Me")[5439]["tracks"].pop() === "Take a Chance on Me");
|
||||
- text: After <code>updateRecords(collection, 2548, "artist", "")</code>, <code>artist</code> should not be set
|
||||
testString: updateRecords(_recordCollection, 2548, "artist", ""); assert(!_recordCollection[2548].hasOwnProperty("artist"));
|
||||
- text: After <code>updateRecords(collection, 1245, "tracks", "Addicted to Love")</code>, <code>tracks</code> should have <code>Addicted to Love</code> as the last element.
|
||||
testString: assert(updateRecords(_recordCollection, 1245, "tracks", "Addicted to Love")[1245]["tracks"].pop() === "Addicted to Love");
|
||||
- text: After <code>updateRecords(collection, 2468, "tracks", "Free")</code>, <code>tracks</code> should have <code>1999</code> as the first element.
|
||||
testString: assert(updateRecords(_recordCollection, 2468, "tracks", "Free")[2468]["tracks"][0] === "1999");
|
||||
- text: After <code>updateRecords(collection, 2548, "tracks", "")</code>, <code>tracks</code> should not be set
|
||||
testString: updateRecords(_recordCollection, 2548, "tracks", ""); assert(!_recordCollection[2548].hasOwnProperty("tracks"));
|
||||
- text: After <code>updateRecords(collection, 1245, "albumTitle", "Riptide")</code>, <code>albumTitle</code> should be <code>Riptide</code>
|
||||
testString: assert(updateRecords(_recordCollection, 1245, "albumTitle", "Riptide")[1245]["albumTitle"] === "Riptide");
|
||||
```js
|
||||
assert(
|
||||
updateRecords(_recordCollection, 5439, 'artist', 'ABBA')[5439]['artist'] ===
|
||||
'ABBA'
|
||||
);
|
||||
```
|
||||
|
||||
</section>
|
||||
After `updateRecords(collection, 5439, "tracks", "Take a Chance on Me")`, `tracks` should have `Take a Chance on Me` as the last element.
|
||||
|
||||
## Challenge Seed
|
||||
```js
|
||||
assert(
|
||||
updateRecords(_recordCollection, 5439, 'tracks', 'Take a Chance on Me')[5439][
|
||||
'tracks'
|
||||
].pop() === 'Take a Chance on Me'
|
||||
);
|
||||
```
|
||||
|
||||
<section id='challengeSeed'>
|
||||
<div id='js-seed'>
|
||||
After `updateRecords(collection, 2548, "artist", "")`, `artist` should not be set
|
||||
|
||||
```js
|
||||
updateRecords(_recordCollection, 2548, 'artist', '');
|
||||
assert(!_recordCollection[2548].hasOwnProperty('artist'));
|
||||
```
|
||||
|
||||
After `updateRecords(collection, 1245, "tracks", "Addicted to Love")`, `tracks` should have `Addicted to Love` as the last element.
|
||||
|
||||
```js
|
||||
assert(
|
||||
updateRecords(_recordCollection, 1245, 'tracks', 'Addicted to Love')[1245][
|
||||
'tracks'
|
||||
].pop() === 'Addicted to Love'
|
||||
);
|
||||
```
|
||||
|
||||
After `updateRecords(collection, 2468, "tracks", "Free")`, `tracks` should have `1999` as the first element.
|
||||
|
||||
```js
|
||||
assert(
|
||||
updateRecords(_recordCollection, 2468, 'tracks', 'Free')[2468][
|
||||
'tracks'
|
||||
][0] === '1999'
|
||||
);
|
||||
```
|
||||
|
||||
After `updateRecords(collection, 2548, "tracks", "")`, `tracks` should not be set
|
||||
|
||||
```js
|
||||
updateRecords(_recordCollection, 2548, 'tracks', '');
|
||||
assert(!_recordCollection[2548].hasOwnProperty('tracks'));
|
||||
```
|
||||
|
||||
After `updateRecords(collection, 1245, "albumTitle", "Riptide")`, `albumTitle` should be `Riptide`
|
||||
|
||||
```js
|
||||
assert(
|
||||
updateRecords(_recordCollection, 1245, 'albumTitle', 'Riptide')[1245][
|
||||
'albumTitle'
|
||||
] === 'Riptide'
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --before-user-code--
|
||||
|
||||
```js
|
||||
const _recordCollection = {
|
||||
2548: {
|
||||
albumTitle: 'Slippery When Wet',
|
||||
artist: 'Bon Jovi',
|
||||
tracks: ['Let It Rock', 'You Give Love a Bad Name']
|
||||
},
|
||||
2468: {
|
||||
albumTitle: '1999',
|
||||
artist: 'Prince',
|
||||
tracks: ['1999', 'Little Red Corvette']
|
||||
},
|
||||
1245: {
|
||||
artist: 'Robert Palmer',
|
||||
tracks: []
|
||||
},
|
||||
5439: {
|
||||
albumTitle: 'ABBA Gold'
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
// Setup
|
||||
@@ -87,41 +142,7 @@ function updateRecords(object, id, prop, value) {
|
||||
updateRecords(collection, 5439, 'artist', 'ABBA');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
## Before Test
|
||||
|
||||
<div id="js-setup">
|
||||
|
||||
```js
|
||||
const _recordCollection = {
|
||||
2548: {
|
||||
albumTitle: 'Slippery When Wet',
|
||||
artist: 'Bon Jovi',
|
||||
tracks: ['Let It Rock', 'You Give Love a Bad Name']
|
||||
},
|
||||
2468: {
|
||||
albumTitle: '1999',
|
||||
artist: 'Prince',
|
||||
tracks: ['1999', 'Little Red Corvette']
|
||||
},
|
||||
1245: {
|
||||
artist: 'Robert Palmer',
|
||||
tracks: []
|
||||
},
|
||||
5439: {
|
||||
albumTitle: 'ABBA Gold'
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
|
||||
<section id='solution'>
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
var collection = {
|
||||
@@ -157,5 +178,3 @@ function updateRecords(object, id, prop, value) {
|
||||
return object;
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@@ -2,13 +2,14 @@
|
||||
id: 5cfa3679138e7d9595b9d9d4
|
||||
title: Replace Loops using Recursion
|
||||
challengeType: 1
|
||||
videoUrl: 'https://www.freecodecamp.org/news/how-recursion-works-explained-with-flowcharts-and-a-video-de61f40cb7f9/'
|
||||
videoUrl: >-
|
||||
https://www.freecodecamp.org/news/how-recursion-works-explained-with-flowcharts-and-a-video-de61f40cb7f9/
|
||||
forumTopicId: 301175
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
Recursion is the concept that a function can be expressed in terms of itself. To help understand this, start by thinking about the following task: multiply the first <code>n</code> elements of an array to create the product of those elements. Using a <code>for</code> loop, you could do this:
|
||||
# --description--
|
||||
|
||||
Recursion is the concept that a function can be expressed in terms of itself. To help understand this, start by thinking about the following task: multiply the first `n` elements of an array to create the product of those elements. Using a `for` loop, you could do this:
|
||||
|
||||
```js
|
||||
function multiply(arr, n) {
|
||||
@@ -20,7 +21,7 @@ Recursion is the concept that a function can be expressed in terms of itself. To
|
||||
}
|
||||
```
|
||||
|
||||
However, notice that <code>multiply(arr, n) == multiply(arr, n - 1) * arr[n - 1]</code>. That means you can rewrite <code>multiply</code> in terms of itself and never need to use a loop.
|
||||
However, notice that `multiply(arr, n) == multiply(arr, n - 1) * arr[n - 1]`. That means you can rewrite `multiply` in terms of itself and never need to use a loop.
|
||||
|
||||
```js
|
||||
function multiply(arr, n) {
|
||||
@@ -32,42 +33,55 @@ However, notice that <code>multiply(arr, n) == multiply(arr, n - 1) * arr[n - 1]
|
||||
}
|
||||
```
|
||||
|
||||
The recursive version of <code>multiply</code> breaks down like this. In the <dfn>base case</dfn>, where <code>n <= 0</code>, it returns 1. For larger values of <code>n</code>, it calls itself, but with <code>n - 1</code>. That function call is evaluated in the same way, calling <code>multiply</code> again until <code>n <= 0</code>. At this point, all the functions can return and the original <code>multiply</code> returns the answer.
|
||||
The recursive version of `multiply` breaks down like this. In the <dfn>base case</dfn>, where `n <= 0`, it returns 1. For larger values of `n`, it calls itself, but with `n - 1`. That function call is evaluated in the same way, calling `multiply` again until `n <= 0`. At this point, all the functions can return and the original `multiply` returns the answer.
|
||||
|
||||
<strong>Note:</strong> Recursive functions must have a base case when they return without calling the function again (in this example, when <code>n <= 0</code>), otherwise they can never finish executing.
|
||||
**Note:** Recursive functions must have a base case when they return without calling the function again (in this example, when `n <= 0`), otherwise they can never finish executing.
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Write a recursive function, `sum(arr, n)`, that returns the sum of the first `n` elements of an array `arr`.
|
||||
|
||||
Write a recursive function, <code>sum(arr, n)</code>, that returns the sum of the first <code>n</code> elements of an array <code>arr</code>.
|
||||
# --hints--
|
||||
|
||||
</section>
|
||||
`sum([1], 0)` should equal 0.
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
``` yml
|
||||
tests:
|
||||
- text: <code>sum([1], 0)</code> should equal 0.
|
||||
testString: assert.equal(sum([1], 0), 0);
|
||||
- text: <code>sum([2, 3, 4], 1)</code> should equal 2.
|
||||
testString: assert.equal(sum([2, 3, 4], 1), 2);
|
||||
- text: <code>sum([2, 3, 4, 5], 3)</code> should equal 9.
|
||||
testString: assert.equal(sum([2, 3, 4, 5], 3), 9);
|
||||
- text: Your code should not rely on any kind of loops (<code>for</code> or <code>while</code> or higher order functions such as <code>forEach</code>, <code>map</code>, <code>filter</code>, or <code>reduce</code>.).
|
||||
testString: assert(!__helpers.removeJSComments(code).match(/for|while|forEach|map|filter|reduce/g));
|
||||
- text: You should use recursion to solve this problem.
|
||||
testString: assert(__helpers.removeJSComments(sum.toString()).match(/sum\(.*\)/g).length > 1);
|
||||
```js
|
||||
assert.equal(sum([1], 0), 0);
|
||||
```
|
||||
|
||||
</section>
|
||||
`sum([2, 3, 4], 1)` should equal 2.
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
```js
|
||||
assert.equal(sum([2, 3, 4], 1), 2);
|
||||
```
|
||||
|
||||
<div id='js-seed'>
|
||||
`sum([2, 3, 4, 5], 3)` should equal 9.
|
||||
|
||||
```js
|
||||
assert.equal(sum([2, 3, 4, 5], 3), 9);
|
||||
```
|
||||
|
||||
Your code should not rely on any kind of loops (`for` or `while` or higher order functions such as `forEach`, `map`, `filter`, or `reduce`.).
|
||||
|
||||
```js
|
||||
assert(
|
||||
!__helpers
|
||||
.removeJSComments(code)
|
||||
.match(/for|while|forEach|map|filter|reduce/g)
|
||||
);
|
||||
```
|
||||
|
||||
You should use recursion to solve this problem.
|
||||
|
||||
```js
|
||||
assert(
|
||||
__helpers.removeJSComments(sum.toString()).match(/sum\(.*\)/g).length > 1
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function sum(arr, n) {
|
||||
@@ -75,15 +89,9 @@ function sum(arr, n) {
|
||||
|
||||
// Only change code above this line
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function sum(arr, n) {
|
||||
@@ -95,7 +103,4 @@ function sum(arr, n) {
|
||||
}
|
||||
// Only change code above this line
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@@ -6,9 +6,9 @@ videoUrl: 'https://scrimba.com/c/c3JE8fy'
|
||||
forumTopicId: 18266
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
If you have many options to choose from, a <code>switch</code> statement can be easier to write than many chained <code>if</code>/<code>else if</code> statements. The following:
|
||||
# --description--
|
||||
|
||||
If you have many options to choose from, a `switch` statement can be easier to write than many chained `if`/`else if` statements. The following:
|
||||
|
||||
```js
|
||||
if (val === 1) {
|
||||
@@ -35,47 +35,75 @@ switch(val) {
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Change the chained <code>if</code>/<code>else if</code> statements into a <code>switch</code> statement.
|
||||
</section>
|
||||
Change the chained `if`/`else if` statements into a `switch` statement.
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: You should not use any <code>else</code> statements anywhere in the editor
|
||||
testString: assert(!/else/g.test(code));
|
||||
- text: You should not use any <code>if</code> statements anywhere in the editor
|
||||
testString: assert(!/if/g.test(code));
|
||||
- text: You should have at least four <code>break</code> statements
|
||||
testString: assert(code.match(/break/g).length >= 4);
|
||||
- text: <code>chainToSwitch("bob")</code> should be "Marley"
|
||||
testString: assert(chainToSwitch("bob") === "Marley");
|
||||
- text: <code>chainToSwitch(42)</code> should be "The Answer"
|
||||
testString: assert(chainToSwitch(42) === "The Answer");
|
||||
- text: "<code>chainToSwitch(1)</code> should be \"There is no #1\""
|
||||
testString: "assert(chainToSwitch(1) === \"There is no #1\");"
|
||||
- text: <code>chainToSwitch(99)</code> should be "Missed me by this much!"
|
||||
testString: assert(chainToSwitch(99) === "Missed me by this much!");
|
||||
- text: <code>chainToSwitch(7)</code> should be "Ate Nine"
|
||||
testString: assert(chainToSwitch(7) === "Ate Nine");
|
||||
- text: <code>chainToSwitch("John")</code> should be "" (empty string)
|
||||
testString: assert(chainToSwitch("John") === "");
|
||||
- text: <code>chainToSwitch(156)</code> should be "" (empty string)
|
||||
testString: assert(chainToSwitch(156) === "");
|
||||
You should not use any `else` statements anywhere in the editor
|
||||
|
||||
```js
|
||||
assert(!/else/g.test(code));
|
||||
```
|
||||
|
||||
</section>
|
||||
You should not use any `if` statements anywhere in the editor
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
```js
|
||||
assert(!/if/g.test(code));
|
||||
```
|
||||
|
||||
<div id='js-seed'>
|
||||
You should have at least four `break` statements
|
||||
|
||||
```js
|
||||
assert(code.match(/break/g).length >= 4);
|
||||
```
|
||||
|
||||
`chainToSwitch("bob")` should be "Marley"
|
||||
|
||||
```js
|
||||
assert(chainToSwitch('bob') === 'Marley');
|
||||
```
|
||||
|
||||
`chainToSwitch(42)` should be "The Answer"
|
||||
|
||||
```js
|
||||
assert(chainToSwitch(42) === 'The Answer');
|
||||
```
|
||||
|
||||
`chainToSwitch(1)` should be "There is no #1"
|
||||
|
||||
```js
|
||||
assert(chainToSwitch(1) === 'There is no #1');
|
||||
```
|
||||
|
||||
`chainToSwitch(99)` should be "Missed me by this much!"
|
||||
|
||||
```js
|
||||
assert(chainToSwitch(99) === 'Missed me by this much!');
|
||||
```
|
||||
|
||||
`chainToSwitch(7)` should be "Ate Nine"
|
||||
|
||||
```js
|
||||
assert(chainToSwitch(7) === 'Ate Nine');
|
||||
```
|
||||
|
||||
`chainToSwitch("John")` should be "" (empty string)
|
||||
|
||||
```js
|
||||
assert(chainToSwitch('John') === '');
|
||||
```
|
||||
|
||||
`chainToSwitch(156)` should be "" (empty string)
|
||||
|
||||
```js
|
||||
assert(chainToSwitch(156) === '');
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function chainToSwitch(val) {
|
||||
@@ -99,18 +127,9 @@ function chainToSwitch(val) {
|
||||
}
|
||||
|
||||
chainToSwitch(7);
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function chainToSwitch(val) {
|
||||
@@ -135,5 +154,3 @@ function chainToSwitch(val) {
|
||||
return answer;
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@@ -6,10 +6,11 @@ videoUrl: 'https://scrimba.com/c/cy87wue'
|
||||
forumTopicId: 18271
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
We can pass values into a function with <dfn>arguments</dfn>. You can use a <code>return</code> statement to send a value back out of a function.
|
||||
<strong>Example</strong>
|
||||
# --description--
|
||||
|
||||
We can pass values into a function with <dfn>arguments</dfn>. You can use a `return` statement to send a value back out of a function.
|
||||
|
||||
**Example**
|
||||
|
||||
```js
|
||||
function plusThree(num) {
|
||||
@@ -18,52 +19,46 @@ function plusThree(num) {
|
||||
var answer = plusThree(5); // 8
|
||||
```
|
||||
|
||||
<code>plusThree</code> takes an <dfn>argument</dfn> for <code>num</code> and returns a value equal to <code>num + 3</code>.
|
||||
</section>
|
||||
`plusThree` takes an <dfn>argument</dfn> for `num` and returns a value equal to `num + 3`.
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Create a function <code>timesFive</code> that accepts one argument, multiplies it by <code>5</code>, and returns the new value. See the last line in the editor for an example of how you can test your <code>timesFive</code> function.
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
Create a function `timesFive` that accepts one argument, multiplies it by `5`, and returns the new value. See the last line in the editor for an example of how you can test your `timesFive` function.
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>timesFive</code> should be a function
|
||||
testString: assert(typeof timesFive === 'function');
|
||||
- text: <code>timesFive(5)</code> should return <code>25</code>
|
||||
testString: assert(timesFive(5) === 25);
|
||||
- text: <code>timesFive(2)</code> should return <code>10</code>
|
||||
testString: assert(timesFive(2) === 10);
|
||||
- text: <code>timesFive(0)</code> should return <code>0</code>
|
||||
testString: assert(timesFive(0) === 0);
|
||||
# --hints--
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`timesFive` should be a function
|
||||
|
||||
```js
|
||||
|
||||
|
||||
|
||||
assert(typeof timesFive === 'function');
|
||||
```
|
||||
|
||||
</div>
|
||||
`timesFive(5)` should return `25`
|
||||
|
||||
```js
|
||||
assert(timesFive(5) === 25);
|
||||
```
|
||||
|
||||
`timesFive(2)` should return `10`
|
||||
|
||||
</section>
|
||||
```js
|
||||
assert(timesFive(2) === 10);
|
||||
```
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
`timesFive(0)` should return `0`
|
||||
|
||||
```js
|
||||
assert(timesFive(0) === 0);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function timesFive(num) {
|
||||
@@ -71,5 +66,3 @@ function timesFive(num) {
|
||||
}
|
||||
timesFive(10);
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@@ -6,10 +6,11 @@ videoUrl: 'https://scrimba.com/c/cQe39Sq'
|
||||
forumTopicId: 18272
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
When a <code>return</code> statement is reached, the execution of the current function stops and control returns to the calling location.
|
||||
<strong>Example</strong>
|
||||
# --description--
|
||||
|
||||
When a `return` statement is reached, the execution of the current function stops and control returns to the calling location.
|
||||
|
||||
**Example**
|
||||
|
||||
```js
|
||||
function myFun() {
|
||||
@@ -20,43 +21,62 @@ function myFun() {
|
||||
myFun();
|
||||
```
|
||||
|
||||
The above outputs "Hello" to the console, returns "World", but <code>"byebye"</code> is never output, because the function exits at the <code>return</code> statement.
|
||||
</section>
|
||||
The above outputs "Hello" to the console, returns "World", but `"byebye"` is never output, because the function exits at the `return` statement.
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Modify the function <code>abTest</code> so that if <code>a</code> or <code>b</code> are less than <code>0</code> the function will immediately exit with a value of <code>undefined</code>.
|
||||
<strong>Hint</strong><br>Remember that <a href='https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/understanding-uninitialized-variables' target='_blank'><code>undefined</code> is a keyword</a>, not a string.
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
Modify the function `abTest` so that if `a` or `b` are less than `0` the function will immediately exit with a value of `undefined`.
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>abTest(2,2)</code> should return a number
|
||||
testString: assert(typeof abTest(2,2) === 'number' );
|
||||
- text: <code>abTest(2,2)</code> should return <code>8</code>
|
||||
testString: assert(abTest(2,2) === 8 );
|
||||
- text: <code>abTest(-2,2)</code> should return <code>undefined</code>
|
||||
testString: assert(abTest(-2,2) === undefined );
|
||||
- text: <code>abTest(2,-2)</code> should return <code>undefined</code>
|
||||
testString: assert(abTest(2,-2) === undefined );
|
||||
- text: <code>abTest(2,8)</code> should return <code>18</code>
|
||||
testString: assert(abTest(2,8) === 18 );
|
||||
- text: <code>abTest(3,3)</code> should return <code>12</code>
|
||||
testString: assert(abTest(3,3) === 12 );
|
||||
- text: <code>abTest(0,0)</code> should return <code>0</code>
|
||||
testString: assert(abTest(0,0) === 0);
|
||||
|
||||
**Hint**
|
||||
Remember that [`undefined` is a keyword](https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/understanding-uninitialized-variables), not a string.
|
||||
|
||||
# --hints--
|
||||
|
||||
`abTest(2,2)` should return a number
|
||||
|
||||
```js
|
||||
assert(typeof abTest(2, 2) === 'number');
|
||||
```
|
||||
|
||||
</section>
|
||||
`abTest(2,2)` should return `8`
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
```js
|
||||
assert(abTest(2, 2) === 8);
|
||||
```
|
||||
|
||||
<div id='js-seed'>
|
||||
`abTest(-2,2)` should return `undefined`
|
||||
|
||||
```js
|
||||
assert(abTest(-2, 2) === undefined);
|
||||
```
|
||||
|
||||
`abTest(2,-2)` should return `undefined`
|
||||
|
||||
```js
|
||||
assert(abTest(2, -2) === undefined);
|
||||
```
|
||||
|
||||
`abTest(2,8)` should return `18`
|
||||
|
||||
```js
|
||||
assert(abTest(2, 8) === 18);
|
||||
```
|
||||
|
||||
`abTest(3,3)` should return `12`
|
||||
|
||||
```js
|
||||
assert(abTest(3, 3) === 12);
|
||||
```
|
||||
|
||||
`abTest(0,0)` should return `0`
|
||||
|
||||
```js
|
||||
assert(abTest(0, 0) === 0);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
// Setup
|
||||
@@ -73,15 +93,7 @@ function abTest(a, b) {
|
||||
abTest(2,2);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function abTest(a, b) {
|
||||
@@ -91,5 +103,3 @@ function abTest(a, b) {
|
||||
return Math.round(Math.pow(Math.sqrt(a) + Math.sqrt(b), 2));
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@@ -6,9 +6,10 @@ videoUrl: 'https://scrimba.com/c/cp62qAQ'
|
||||
forumTopicId: 18273
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
You may recall from <a href="/learn/javascript-algorithms-and-data-structures/basic-javascript/comparison-with-the-equality-operator" target="_blank">Comparison with the Equality Operator</a> that all comparison operators return a boolean <code>true</code> or <code>false</code> value.
|
||||
# --description--
|
||||
|
||||
You may recall from [Comparison with the Equality Operator](/learn/javascript-algorithms-and-data-structures/basic-javascript/comparison-with-the-equality-operator) that all comparison operators return a boolean `true` or `false` value.
|
||||
|
||||
Sometimes people use an if/else statement to do a comparison, like this:
|
||||
|
||||
```js
|
||||
@@ -21,7 +22,7 @@ function isEqual(a,b) {
|
||||
}
|
||||
```
|
||||
|
||||
But there's a better way to do this. Since <code>===</code> returns <code>true</code> or <code>false</code>, we can return the result of the comparison:
|
||||
But there's a better way to do this. Since `===` returns `true` or `false`, we can return the result of the comparison:
|
||||
|
||||
```js
|
||||
function isEqual(a,b) {
|
||||
@@ -29,33 +30,33 @@ function isEqual(a,b) {
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Fix the function <code>isLess</code> to remove the <code>if/else</code> statements.
|
||||
</section>
|
||||
Fix the function `isLess` to remove the `if/else` statements.
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>isLess(10,15)</code> should return <code>true</code>
|
||||
testString: assert(isLess(10,15) === true);
|
||||
- text: <code>isLess(15,10)</code> should return <code>false</code>
|
||||
testString: assert(isLess(15, 10) === false);
|
||||
- text: You should not use any <code>if</code> or <code>else</code> statements
|
||||
testString: assert(!/if|else/g.test(code));
|
||||
`isLess(10,15)` should return `true`
|
||||
|
||||
```js
|
||||
assert(isLess(10, 15) === true);
|
||||
```
|
||||
|
||||
</section>
|
||||
`isLess(15,10)` should return `false`
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
```js
|
||||
assert(isLess(15, 10) === false);
|
||||
```
|
||||
|
||||
<div id='js-seed'>
|
||||
You should not use any `if` or `else` statements
|
||||
|
||||
```js
|
||||
assert(!/if|else/g.test(code));
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function isLess(a, b) {
|
||||
@@ -71,20 +72,10 @@ function isLess(a, b) {
|
||||
isLess(10, 15);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function isLess(a, b) {
|
||||
return a < b;
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@@ -6,10 +6,11 @@ videoUrl: 'https://scrimba.com/c/c4mv4fm'
|
||||
forumTopicId: 18277
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
If you have many options to choose from, use a <dfn>switch</dfn> statement. A <code>switch</code> statement tests a value and can have many <dfn>case</dfn> statements which define various possible values. Statements are executed from the first matched <code>case</code> value until a <code>break</code> is encountered.
|
||||
Here is an example of a <code>switch</code> statement:
|
||||
# --description--
|
||||
|
||||
If you have many options to choose from, use a <dfn>switch</dfn> statement. A `switch` statement tests a value and can have many <dfn>case</dfn> statements which define various possible values. Statements are executed from the first matched `case` value until a `break` is encountered.
|
||||
|
||||
Here is an example of a `switch` statement:
|
||||
|
||||
```js
|
||||
switch(lowercaseLetter) {
|
||||
@@ -22,40 +23,57 @@ switch(lowercaseLetter) {
|
||||
}
|
||||
```
|
||||
|
||||
<code>case</code> values are tested with strict equality (<code>===</code>). The <code>break</code> tells JavaScript to stop executing statements. If the <code>break</code> is omitted, the next statement will be executed.
|
||||
</section>
|
||||
`case` values are tested with strict equality (`===`). The `break` tells JavaScript to stop executing statements. If the `break` is omitted, the next statement will be executed.
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Write a switch statement which tests <code>val</code> and sets <code>answer</code> for the following conditions:<br><code>1</code> - "alpha"<br><code>2</code> - "beta"<br><code>3</code> - "gamma"<br><code>4</code> - "delta"
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
Write a switch statement which tests `val` and sets `answer` for the following conditions:
|
||||
`1` - "alpha"
|
||||
`2` - "beta"
|
||||
`3` - "gamma"
|
||||
`4` - "delta"
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>caseInSwitch(1)</code> should have a value of "alpha"
|
||||
testString: assert(caseInSwitch(1) === "alpha");
|
||||
- text: <code>caseInSwitch(2)</code> should have a value of "beta"
|
||||
testString: assert(caseInSwitch(2) === "beta");
|
||||
- text: <code>caseInSwitch(3)</code> should have a value of "gamma"
|
||||
testString: assert(caseInSwitch(3) === "gamma");
|
||||
- text: <code>caseInSwitch(4)</code> should have a value of "delta"
|
||||
testString: assert(caseInSwitch(4) === "delta");
|
||||
- text: You should not use any <code>if</code> or <code>else</code> statements
|
||||
testString: assert(!/else/g.test(code) || !/if/g.test(code));
|
||||
- text: You should have at least 3 <code>break</code> statements
|
||||
testString: assert(code.match(/break/g).length > 2);
|
||||
# --hints--
|
||||
|
||||
`caseInSwitch(1)` should have a value of "alpha"
|
||||
|
||||
```js
|
||||
assert(caseInSwitch(1) === 'alpha');
|
||||
```
|
||||
|
||||
</section>
|
||||
`caseInSwitch(2)` should have a value of "beta"
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
```js
|
||||
assert(caseInSwitch(2) === 'beta');
|
||||
```
|
||||
|
||||
<div id='js-seed'>
|
||||
`caseInSwitch(3)` should have a value of "gamma"
|
||||
|
||||
```js
|
||||
assert(caseInSwitch(3) === 'gamma');
|
||||
```
|
||||
|
||||
`caseInSwitch(4)` should have a value of "delta"
|
||||
|
||||
```js
|
||||
assert(caseInSwitch(4) === 'delta');
|
||||
```
|
||||
|
||||
You should not use any `if` or `else` statements
|
||||
|
||||
```js
|
||||
assert(!/else/g.test(code) || !/if/g.test(code));
|
||||
```
|
||||
|
||||
You should have at least 3 `break` statements
|
||||
|
||||
```js
|
||||
assert(code.match(/break/g).length > 2);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function caseInSwitch(val) {
|
||||
@@ -69,18 +87,9 @@ function caseInSwitch(val) {
|
||||
}
|
||||
|
||||
caseInSwitch(1);
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function caseInSwitch(val) {
|
||||
@@ -102,5 +111,3 @@ function caseInSwitch(val) {
|
||||
return answer;
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@@ -6,53 +6,45 @@ videoUrl: 'https://scrimba.com/c/c9MEKHZ'
|
||||
forumTopicId: 18280
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
Create a shopping list in the variable <code>myList</code>. The list should be a multi-dimensional array containing several sub-arrays.
|
||||
# --description--
|
||||
|
||||
Create a shopping list in the variable `myList`. The list should be a multi-dimensional array containing several sub-arrays.
|
||||
|
||||
The first element in each sub-array should contain a string with the name of the item. The second element should be a number representing the quantity i.e.
|
||||
<code>["Chocolate Bar", 15]</code>
|
||||
|
||||
`["Chocolate Bar", 15]`
|
||||
|
||||
There should be at least 5 sub-arrays in the list.
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
# --hints--
|
||||
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>myList</code> should be an array.
|
||||
testString: assert(isArray);
|
||||
- text: The first elements in each of your sub-arrays should all be strings.
|
||||
testString: assert(hasString);
|
||||
- text: The second elements in each of your sub-arrays should all be numbers.
|
||||
testString: assert(hasNumber);
|
||||
- text: You should have at least 5 items in your list.
|
||||
testString: assert(count > 4);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`myList` should be an array.
|
||||
|
||||
```js
|
||||
var myList = [];
|
||||
|
||||
|
||||
assert(isArray);
|
||||
```
|
||||
|
||||
</div>
|
||||
The first elements in each of your sub-arrays should all be strings.
|
||||
|
||||
```js
|
||||
assert(hasString);
|
||||
```
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
The second elements in each of your sub-arrays should all be numbers.
|
||||
|
||||
```js
|
||||
assert(hasNumber);
|
||||
```
|
||||
|
||||
You should have at least 5 items in your list.
|
||||
|
||||
```js
|
||||
assert(count > 4);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
var count = 0;
|
||||
@@ -83,13 +75,13 @@ var hasNumber = false;
|
||||
})(myList);
|
||||
```
|
||||
|
||||
</div>
|
||||
## --seed-contents--
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
```js
|
||||
var myList = [];
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
var myList = [
|
||||
@@ -100,5 +92,3 @@ var myList = [
|
||||
["Toads", 9]
|
||||
];
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@@ -6,67 +6,52 @@ videoUrl: 'https://scrimba.com/c/ca8Q8tP'
|
||||
forumTopicId: 18307
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
# --description--
|
||||
|
||||
In Computer Science a <dfn>queue</dfn> is an abstract <dfn>Data Structure</dfn> where items are kept in order. New items can be added at the back of the queue and old items are taken off from the front of the queue.
|
||||
Write a function <code>nextInLine</code> which takes an array (<code>arr</code>) and a number (<code>item</code>) as arguments.
|
||||
|
||||
Write a function `nextInLine` which takes an array (`arr`) and a number (`item`) as arguments.
|
||||
|
||||
Add the number to the end of the array, then remove the first element of the array.
|
||||
The <code>nextInLine</code> function should then return the element that was removed.
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
The `nextInLine` function should then return the element that was removed.
|
||||
|
||||
</section>
|
||||
# --hints--
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>nextInLine([], 5)</code> should return a number.
|
||||
testString: assert.isNumber(nextInLine([],5));
|
||||
- text: <code>nextInLine([], 1)</code> should return <code>1</code>
|
||||
testString: assert(nextInLine([],1) === 1);
|
||||
- text: <code>nextInLine([2], 1)</code> should return <code>2</code>
|
||||
testString: assert(nextInLine([2],1) === 2);
|
||||
- text: <code>nextInLine([5,6,7,8,9], 1)</code> should return <code>5</code>
|
||||
testString: assert(nextInLine([5,6,7,8,9],1) === 5);
|
||||
- text: After <code>nextInLine(testArr, 10)</code>, <code>testArr[4]</code> should be <code>10</code>
|
||||
testString: nextInLine(testArr, 10); assert(testArr[4] === 10);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`nextInLine([], 5)` should return a number.
|
||||
|
||||
```js
|
||||
function nextInLine(arr, item) {
|
||||
// Only change code below this line
|
||||
|
||||
return item;
|
||||
// Only change code above this line
|
||||
|
||||
|
||||
}
|
||||
|
||||
// Setup
|
||||
var testArr = [1,2,3,4,5];
|
||||
|
||||
// Display code
|
||||
console.log("Before: " + JSON.stringify(testArr));
|
||||
console.log(nextInLine(testArr, 6));
|
||||
console.log("After: " + JSON.stringify(testArr));
|
||||
assert.isNumber(nextInLine([], 5));
|
||||
```
|
||||
|
||||
</div>
|
||||
`nextInLine([], 1)` should return `1`
|
||||
|
||||
### Before Test
|
||||
<div id='js-setup'>
|
||||
```js
|
||||
assert(nextInLine([], 1) === 1);
|
||||
```
|
||||
|
||||
`nextInLine([2], 1)` should return `2`
|
||||
|
||||
```js
|
||||
assert(nextInLine([2], 1) === 2);
|
||||
```
|
||||
|
||||
`nextInLine([5,6,7,8,9], 1)` should return `5`
|
||||
|
||||
```js
|
||||
assert(nextInLine([5, 6, 7, 8, 9], 1) === 5);
|
||||
```
|
||||
|
||||
After `nextInLine(testArr, 10)`, `testArr[4]` should be `10`
|
||||
|
||||
```js
|
||||
nextInLine(testArr, 10);
|
||||
assert(testArr[4] === 10);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --before-user-code--
|
||||
|
||||
```js
|
||||
var logOutput = [];
|
||||
@@ -91,10 +76,7 @@ function uncapture() {
|
||||
capture();
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
uncapture();
|
||||
@@ -102,13 +84,28 @@ testArr = [1,2,3,4,5];
|
||||
(function() { return logOutput.join("\n");})();
|
||||
```
|
||||
|
||||
</div>
|
||||
## --seed-contents--
|
||||
|
||||
</section>
|
||||
```js
|
||||
function nextInLine(arr, item) {
|
||||
// Only change code below this line
|
||||
|
||||
return item;
|
||||
// Only change code above this line
|
||||
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
}
|
||||
|
||||
// Setup
|
||||
var testArr = [1,2,3,4,5];
|
||||
|
||||
// Display code
|
||||
console.log("Before: " + JSON.stringify(testArr));
|
||||
console.log(nextInLine(testArr, 6));
|
||||
console.log("After: " + JSON.stringify(testArr));
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
var testArr = [ 1,2,3,4,5];
|
||||
@@ -118,5 +115,3 @@ function nextInLine(arr, item) {
|
||||
return arr.shift();
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@@ -6,66 +6,58 @@ videoUrl: 'https://scrimba.com/c/crZQWAm'
|
||||
forumTopicId: 18309
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
With JavaScript <code>array</code> variables, we can store several pieces of data in one place.
|
||||
# --description--
|
||||
|
||||
With JavaScript `array` variables, we can store several pieces of data in one place.
|
||||
|
||||
You start an array declaration with an opening square bracket, end it with a closing square bracket, and put a comma between each entry, like this:
|
||||
<code>var sandwich = ["peanut butter", "jelly", "bread"]</code>.
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Modify the new array <code>myArray</code> so that it contains both a <code>string</code> and a <code>number</code> (in that order).
|
||||
<strong>Hint</strong><br>Refer to the example code in the text editor if you get stuck.
|
||||
</section>
|
||||
`var sandwich = ["peanut butter", "jelly", "bread"]`.
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --instructions--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>myArray</code> should be an <code>array</code>.
|
||||
testString: assert(typeof myArray == 'object');
|
||||
- text: The first item in <code>myArray</code> should be a <code>string</code>.
|
||||
testString: assert(typeof myArray[0] !== 'undefined' && typeof myArray[0] == 'string');
|
||||
- text: The second item in <code>myArray</code> should be a <code>number</code>.
|
||||
testString: assert(typeof myArray[1] !== 'undefined' && typeof myArray[1] == 'number');
|
||||
Modify the new array `myArray` so that it contains both a `string` and a `number` (in that order).
|
||||
|
||||
```
|
||||
**Hint**
|
||||
Refer to the example code in the text editor if you get stuck.
|
||||
|
||||
</section>
|
||||
# --hints--
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`myArray` should be an `array`.
|
||||
|
||||
```js
|
||||
// Only change code below this line
|
||||
var myArray = [];
|
||||
|
||||
assert(typeof myArray == 'object');
|
||||
```
|
||||
|
||||
</div>
|
||||
The first item in `myArray` should be a `string`.
|
||||
|
||||
```js
|
||||
assert(typeof myArray[0] !== 'undefined' && typeof myArray[0] == 'string');
|
||||
```
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
The second item in `myArray` should be a `number`.
|
||||
|
||||
```js
|
||||
assert(typeof myArray[1] !== 'undefined' && typeof myArray[1] == 'number');
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
(function(z){return z;})(myArray);
|
||||
```
|
||||
|
||||
</div>
|
||||
## --seed-contents--
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
```js
|
||||
// Only change code below this line
|
||||
var myArray = [];
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
var myArray = ["The Answer", 42];
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@@ -6,57 +6,44 @@ videoUrl: 'https://scrimba.com/c/cEanysE'
|
||||
forumTopicId: 18310
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
In JavaScript, you can store a value in a variable with the <dfn>assignment</dfn> operator (<code>=</code>).
|
||||
<code>myVariable = 5;</code>
|
||||
This assigns the <code>Number</code> value <code>5</code> to <code>myVariable</code>.
|
||||
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.
|
||||
# --description--
|
||||
|
||||
In JavaScript, you can store a value in a variable with the <dfn>assignment</dfn> operator (`=`).
|
||||
|
||||
`myVariable = 5;`
|
||||
|
||||
This assigns the `Number` value `5` to `myVariable`.
|
||||
|
||||
If there are any calculations to the right of the `=` operator, those are performed before the value is assigned to the variable on the left of the operator.
|
||||
|
||||
```js
|
||||
var myVar;
|
||||
myVar = 5;
|
||||
```
|
||||
|
||||
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>
|
||||
First, this code creates a variable named `myVar`. Then, the code assigns `5` to `myVar`. Now, if `myVar` appears again in the code, the program will treat it as if it is `5`.
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Assign the value <code>7</code> to variable <code>a</code>.
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
Assign the value `7` to variable `a`.
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: You should not change code above the specified comment.
|
||||
testString: assert(/var a;/.test(code));
|
||||
- text: <code>a</code> should have a value of 7.
|
||||
testString: assert(typeof a === 'number' && a === 7);
|
||||
# --hints--
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
You should not change code above the specified comment.
|
||||
|
||||
```js
|
||||
// Setup
|
||||
var a;
|
||||
|
||||
// Only change code below this line
|
||||
|
||||
assert(/var a;/.test(code));
|
||||
```
|
||||
|
||||
</div>
|
||||
`a` should have a value of 7.
|
||||
|
||||
### Before Test
|
||||
<div id='js-setup'>
|
||||
```js
|
||||
assert(typeof a === 'number' && a === 7);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --before-user-code--
|
||||
|
||||
```js
|
||||
if (typeof a != 'undefined') {
|
||||
@@ -64,26 +51,24 @@ if (typeof a != 'undefined') {
|
||||
}
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
(function(a){return "a = " + a;})(a);
|
||||
```
|
||||
|
||||
</div>
|
||||
## --seed-contents--
|
||||
|
||||
</section>
|
||||
```js
|
||||
// Setup
|
||||
var a;
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
// Only change code below this line
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
var a;
|
||||
a = 7;
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@@ -6,69 +6,52 @@ videoUrl: 'https://scrimba.com/c/cP3yQtk'
|
||||
forumTopicId: 18314
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
We can also subtract one number from another.
|
||||
JavaScript uses the <code>-</code> symbol for subtraction.
|
||||
# --description--
|
||||
|
||||
<strong>Example</strong>
|
||||
We can also subtract one number from another.
|
||||
|
||||
JavaScript uses the `-` symbol for subtraction.
|
||||
|
||||
**Example**
|
||||
|
||||
```js
|
||||
myVar = 12 - 6; // assigned 6
|
||||
```
|
||||
|
||||
# --instructions--
|
||||
|
||||
</section>
|
||||
Change the `0` so the difference is `12`.
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Change the <code>0</code> so the difference is <code>12</code>.
|
||||
</section>
|
||||
# --hints--
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: The variable <code>difference</code> should be equal to 12.
|
||||
testString: assert(difference === 12);
|
||||
- text: You should only subtract one number from 45.
|
||||
testString: assert(/difference=45-33;?/.test(__helpers.removeWhiteSpace(code)));
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
The variable `difference` should be equal to 12.
|
||||
|
||||
```js
|
||||
var difference = 45 - 0;
|
||||
|
||||
|
||||
assert(difference === 12);
|
||||
```
|
||||
|
||||
</div>
|
||||
You should only subtract one number from 45.
|
||||
|
||||
```js
|
||||
assert(/difference=45-33;?/.test(__helpers.removeWhiteSpace(code)));
|
||||
```
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
(function(z){return 'difference = '+z;})(difference);
|
||||
```
|
||||
|
||||
</div>
|
||||
## --seed-contents--
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
```js
|
||||
var difference = 45 - 0;
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
var difference = 45 - 33;
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@@ -6,10 +6,11 @@ videoUrl: 'https://scrimba.com/c/c6Wz4ySr'
|
||||
forumTopicId: 18324
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
Sometimes it is useful to check if the property of a given object exists or not. We can use the <code>.hasOwnProperty(propname)</code> method of objects to determine if that object has the given property name. <code>.hasOwnProperty()</code> returns <code>true</code> or <code>false</code> if the property is found or not.
|
||||
<strong>Example</strong>
|
||||
# --description--
|
||||
|
||||
Sometimes it is useful to check if the property of a given object exists or not. We can use the `.hasOwnProperty(propname)` method of objects to determine if that object has the given property name. `.hasOwnProperty()` returns `true` or `false` if the property is found or not.
|
||||
|
||||
**Example**
|
||||
|
||||
```js
|
||||
var myObj = {
|
||||
@@ -20,38 +21,58 @@ myObj.hasOwnProperty("top"); // true
|
||||
myObj.hasOwnProperty("middle"); // false
|
||||
```
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Modify the function <code>checkObj</code> to test if an object passed to the function (<code>obj</code>) contains a specific property (<code>checkProp</code>). If the property is found, return that property's value. If not, return <code>"Not Found"</code>.
|
||||
</section>
|
||||
Modify the function `checkObj` to test if an object passed to the function (`obj`) contains a specific property (`checkProp`). If the property is found, return that property's value. If not, return `"Not Found"`.
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: '<code>checkObj({gift: "pony", pet: "kitten", bed: "sleigh"}, "gift")</code> should return <code>"pony"</code>.'
|
||||
testString: 'assert(checkObj({gift: "pony", pet: "kitten", bed: "sleigh"}, "gift") === "pony");'
|
||||
- text: '<code>checkObj({gift: "pony", pet: "kitten", bed: "sleigh"}, "pet")</code> should return <code>"kitten"</code>.'
|
||||
testString: 'assert(checkObj({gift: "pony", pet: "kitten", bed: "sleigh"}, "pet") === "kitten");'
|
||||
- text: '<code>checkObj({gift: "pony", pet: "kitten", bed: "sleigh"}, "house")</code> should return <code>"Not Found"</code>.'
|
||||
testString: 'assert(checkObj({gift: "pony", pet: "kitten", bed: "sleigh"}, "house") === "Not Found");'
|
||||
- text: '<code>checkObj({city: "Seattle"}, "city")</code> should return <code>"Seattle"</code>.'
|
||||
testString: 'assert(checkObj({city: "Seattle"}, "city") === "Seattle");'
|
||||
- text: '<code>checkObj({city: "Seattle"}, "district")</code> should return <code>"Not Found"</code>.'
|
||||
testString: 'assert(checkObj({city: "Seattle"}, "district") === "Not Found");'
|
||||
- text: '<code>checkObj({pet: "kitten", bed: "sleigh"}, "gift")</code> should return <code>"Not Found"</code>.'
|
||||
testString: 'assert(checkObj({pet: "kitten", bed: "sleigh"}, "gift") === "Not Found");'
|
||||
`checkObj({gift: "pony", pet: "kitten", bed: "sleigh"}, "gift")` should return `"pony"`.
|
||||
|
||||
```js
|
||||
assert(
|
||||
checkObj({ gift: 'pony', pet: 'kitten', bed: 'sleigh' }, 'gift') === 'pony'
|
||||
);
|
||||
```
|
||||
|
||||
</section>
|
||||
`checkObj({gift: "pony", pet: "kitten", bed: "sleigh"}, "pet")` should return `"kitten"`.
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
```js
|
||||
assert(
|
||||
checkObj({ gift: 'pony', pet: 'kitten', bed: 'sleigh' }, 'pet') === 'kitten'
|
||||
);
|
||||
```
|
||||
|
||||
<div id='js-seed'>
|
||||
`checkObj({gift: "pony", pet: "kitten", bed: "sleigh"}, "house")` should return `"Not Found"`.
|
||||
|
||||
```js
|
||||
assert(
|
||||
checkObj({ gift: 'pony', pet: 'kitten', bed: 'sleigh' }, 'house') ===
|
||||
'Not Found'
|
||||
);
|
||||
```
|
||||
|
||||
`checkObj({city: "Seattle"}, "city")` should return `"Seattle"`.
|
||||
|
||||
```js
|
||||
assert(checkObj({ city: 'Seattle' }, 'city') === 'Seattle');
|
||||
```
|
||||
|
||||
`checkObj({city: "Seattle"}, "district")` should return `"Not Found"`.
|
||||
|
||||
```js
|
||||
assert(checkObj({ city: 'Seattle' }, 'district') === 'Not Found');
|
||||
```
|
||||
|
||||
`checkObj({pet: "kitten", bed: "sleigh"}, "gift")` should return `"Not Found"`.
|
||||
|
||||
```js
|
||||
assert(checkObj({ pet: 'kitten', bed: 'sleigh' }, 'gift') === 'Not Found');
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function checkObj(obj, checkProp) {
|
||||
@@ -59,15 +80,9 @@ function checkObj(obj, checkProp) {
|
||||
return "Change Me!";
|
||||
// Only change code above this line
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function checkObj(obj, checkProp) {
|
||||
@@ -78,5 +93,3 @@ function checkObj(obj, checkProp) {
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@@ -6,9 +6,10 @@ videoUrl: 'https://scrimba.com/c/cWPVaUR'
|
||||
forumTopicId: 18331
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
In JavaScript, <code>String</code> values are <dfn>immutable</dfn>, which means that they cannot be altered once created.
|
||||
# --description--
|
||||
|
||||
In JavaScript, `String` values are <dfn>immutable</dfn>, which means that they cannot be altered once created.
|
||||
|
||||
For example, the following code:
|
||||
|
||||
```js
|
||||
@@ -16,38 +17,40 @@ var myStr = "Bob";
|
||||
myStr[0] = "J";
|
||||
```
|
||||
|
||||
cannot change the value of <code>myStr</code> to "Job", because the contents of <code>myStr</code> cannot be altered. Note that this does <em>not</em> mean that <code>myStr</code> cannot be changed, just that the individual characters of a <dfn>string literal</dfn> cannot be changed. The only way to change <code>myStr</code> would be to assign it with a new string, like this:
|
||||
cannot change the value of `myStr` to "Job", because the contents of `myStr` cannot be altered. Note that this does *not* mean that `myStr` cannot be changed, just that the individual characters of a <dfn>string literal</dfn> cannot be changed. The only way to change `myStr` would be to assign it with a new string, like this:
|
||||
|
||||
```js
|
||||
var myStr = "Bob";
|
||||
myStr = "Job";
|
||||
```
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Correct the assignment to <code>myStr</code> so it contains the string value of <code>Hello World</code> using the approach shown in the example above.
|
||||
</section>
|
||||
Correct the assignment to `myStr` so it contains the string value of `Hello World` using the approach shown in the example above.
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>myStr</code> should have a value of <code>Hello World</code>.
|
||||
testString: assert(myStr === "Hello World");
|
||||
- text: You should not change the code above the specified comment.
|
||||
testString: assert(/myStr = "Jello World"/.test(code));
|
||||
`myStr` should have a value of `Hello World`.
|
||||
|
||||
```js
|
||||
assert(myStr === 'Hello World');
|
||||
```
|
||||
|
||||
</section>
|
||||
You should not change the code above the specified comment.
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
```js
|
||||
assert(/myStr = "Jello World"/.test(code));
|
||||
```
|
||||
|
||||
<div id='js-seed'>
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
(function(v){return "myStr = " + v;})(myStr);
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
// Setup
|
||||
@@ -56,30 +59,11 @@ var myStr = "Jello World";
|
||||
// Only change code below this line
|
||||
myStr[0] = "H"; // Change this line
|
||||
// Only change code above this line
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
(function(v){return "myStr = " + v;})(myStr);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
var myStr = "Jello World";
|
||||
myStr = "Hello World";
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@@ -6,35 +6,40 @@ videoUrl: 'https://scrimba.com/c/c9Me8t4'
|
||||
forumTopicId: 301176
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
Another data type is the <dfn>Boolean</dfn>. <code>Booleans</code> may only be one of two values: <code>true</code> or <code>false</code>. They are basically little on-off switches, where <code>true</code> is "on" and <code>false</code> is "off." These two states are mutually exclusive.
|
||||
<strong>Note</strong><br><code>Boolean</code> values are never written with quotes. The <code>strings</code> <code>"true"</code> and <code>"false"</code> are not <code>Boolean</code> and have no special meaning in JavaScript.
|
||||
</section>
|
||||
# --description--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Modify the <code>welcomeToBooleans</code> function so that it returns <code>true</code> instead of <code>false</code> when the run button is clicked.
|
||||
</section>
|
||||
Another data type is the <dfn>Boolean</dfn>. `Booleans` may only be one of two values: `true` or `false`. They are basically little on-off switches, where `true` is "on" and `false` is "off." These two states are mutually exclusive.
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
**Note**
|
||||
`Boolean` values are never written with quotes. The `strings` `"true"` and `"false"` are not `Boolean` and have no special meaning in JavaScript.
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: The <code>welcomeToBooleans()</code> function should return a boolean (true/false) value.
|
||||
testString: assert(typeof welcomeToBooleans() === 'boolean');
|
||||
- text: <code>welcomeToBooleans()</code> should return true.
|
||||
testString: assert(welcomeToBooleans() === true);
|
||||
# --instructions--
|
||||
|
||||
Modify the `welcomeToBooleans` function so that it returns `true` instead of `false` when the run button is clicked.
|
||||
|
||||
# --hints--
|
||||
|
||||
The `welcomeToBooleans()` function should return a boolean (true/false) value.
|
||||
|
||||
```js
|
||||
assert(typeof welcomeToBooleans() === 'boolean');
|
||||
```
|
||||
|
||||
</section>
|
||||
`welcomeToBooleans()` should return true.
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
```js
|
||||
assert(welcomeToBooleans() === true);
|
||||
```
|
||||
|
||||
<div id='js-seed'>
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
welcomeToBooleans();
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function welcomeToBooleans() {
|
||||
@@ -47,28 +52,10 @@ function welcomeToBooleans() {
|
||||
}
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
welcomeToBooleans();
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function welcomeToBooleans() {
|
||||
return true; // Change this line
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@@ -6,13 +6,17 @@ videoUrl: 'https://scrimba.com/c/cd6GDcD'
|
||||
forumTopicId: 18334
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
# --description--
|
||||
|
||||
In JavaScript all variables and function names are case sensitive. This means that capitalization matters.
|
||||
<code>MYVAR</code> is not the same as <code>MyVar</code> nor <code>myvar</code>. It is possible to have multiple distinct variables with the same name but different casing. It is strongly recommended that for the sake of clarity, you <em>do not</em> use this language feature.
|
||||
|
||||
`MYVAR` is not the same as `MyVar` nor `myvar`. It is possible to have multiple distinct variables with the same name but different casing. It is strongly recommended that for the sake of clarity, you *do not* use this language feature.
|
||||
|
||||
<h4>Best Practice</h4>
|
||||
|
||||
Write variable names in JavaScript in <dfn>camelCase</dfn>. In <dfn>camelCase</dfn>, multi-word variable names have the first word in lowercase and the first letter of each subsequent word is capitalized.
|
||||
<strong>Examples:</strong>
|
||||
|
||||
**Examples:**
|
||||
|
||||
```js
|
||||
var someVariable;
|
||||
@@ -20,39 +24,54 @@ var anotherVariableName;
|
||||
var thisVariableNameIsSoLong;
|
||||
```
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Modify the existing declarations and assignments so their names use <dfn>camelCase</dfn>.<br>Do not create any new variables.
|
||||
</section>
|
||||
Modify the existing declarations and assignments so their names use <dfn>camelCase</dfn>.
|
||||
Do not create any new variables.
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>studlyCapVar</code> should be defined and have a value of <code>10</code>.
|
||||
testString: assert(typeof studlyCapVar !== 'undefined' && studlyCapVar === 10);
|
||||
- text: <code>properCamelCase</code> should be defined and have a value of <code>"A String"</code>.
|
||||
testString: assert(typeof properCamelCase !== 'undefined' && properCamelCase === "A String");
|
||||
- text: <code>titleCaseOver</code> should be defined and have a value of <code>9000</code>.
|
||||
testString: assert(typeof titleCaseOver !== 'undefined' && titleCaseOver === 9000);
|
||||
- text: <code>studlyCapVar</code> should use camelCase in both declaration and assignment sections.
|
||||
testString: assert(code.match(/studlyCapVar/g).length === 2);
|
||||
- text: <code>properCamelCase</code> should use camelCase in both declaration and assignment sections.
|
||||
testString: assert(code.match(/properCamelCase/g).length === 2);
|
||||
- text: <code>titleCaseOver</code> should use camelCase in both declaration and assignment sections.
|
||||
testString: assert(code.match(/titleCaseOver/g).length === 2);
|
||||
`studlyCapVar` should be defined and have a value of `10`.
|
||||
|
||||
```js
|
||||
assert(typeof studlyCapVar !== 'undefined' && studlyCapVar === 10);
|
||||
```
|
||||
|
||||
</section>
|
||||
`properCamelCase` should be defined and have a value of `"A String"`.
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
```js
|
||||
assert(
|
||||
typeof properCamelCase !== 'undefined' && properCamelCase === 'A String'
|
||||
);
|
||||
```
|
||||
|
||||
<div id='js-seed'>
|
||||
`titleCaseOver` should be defined and have a value of `9000`.
|
||||
|
||||
```js
|
||||
assert(typeof titleCaseOver !== 'undefined' && titleCaseOver === 9000);
|
||||
```
|
||||
|
||||
`studlyCapVar` should use camelCase in both declaration and assignment sections.
|
||||
|
||||
```js
|
||||
assert(code.match(/studlyCapVar/g).length === 2);
|
||||
```
|
||||
|
||||
`properCamelCase` should use camelCase in both declaration and assignment sections.
|
||||
|
||||
```js
|
||||
assert(code.match(/properCamelCase/g).length === 2);
|
||||
```
|
||||
|
||||
`titleCaseOver` should use camelCase in both declaration and assignment sections.
|
||||
|
||||
```js
|
||||
assert(code.match(/titleCaseOver/g).length === 2);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
// Variable declarations
|
||||
@@ -66,15 +85,7 @@ PRoperCAmelCAse = "A String";
|
||||
tITLEcASEoVER = 9000;
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
var studlyCapVar;
|
||||
@@ -85,5 +96,3 @@ studlyCapVar = 10;
|
||||
properCamelCase = "A String";
|
||||
titleCaseOver = 9000;
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@@ -6,10 +6,11 @@ videoUrl: 'https://scrimba.com/c/ce2p7cL'
|
||||
forumTopicId: 301177
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
A function can include the <code>return</code> statement but it does not have to. In the case that the function doesn't have a <code>return</code> statement, when you call it, the function processes the inner code but the returned value is <code>undefined</code>.
|
||||
<strong>Example</strong>
|
||||
# --description--
|
||||
|
||||
A function can include the `return` statement but it does not have to. In the case that the function doesn't have a `return` statement, when you call it, the function processes the inner code but the returned value is `undefined`.
|
||||
|
||||
**Example**
|
||||
|
||||
```js
|
||||
var sum = 0;
|
||||
@@ -19,36 +20,43 @@ function addSum(num) {
|
||||
addSum(3); // sum will be modified but returned value is undefined
|
||||
```
|
||||
|
||||
<code>addSum</code> is a function without a <code>return</code> statement. The function will change the global <code>sum</code> variable but the returned value of the function is <code>undefined</code>.
|
||||
</section>
|
||||
`addSum` is a function without a `return` statement. The function will change the global `sum` variable but the returned value of the function is `undefined`.
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Create a function <code>addFive</code> without any arguments. This function adds 5 to the <code>sum</code> variable, but its returned value is <code>undefined</code>.
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
Create a function `addFive` without any arguments. This function adds 5 to the `sum` variable, but its returned value is `undefined`.
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>addFive</code> should be a function.
|
||||
testString: assert(typeof addFive === 'function');
|
||||
- text: Once both functions have ran, the <code>sum</code> should be equal to 8.
|
||||
testString: assert(sum === 8);
|
||||
- text: Returned value from <code>addFive</code> should be <code>undefined</code>.
|
||||
testString: assert(addFive() === undefined);
|
||||
- text: Inside the <code>addFive</code> function, you should add <code>5</code> to the <code>sum</code> variable.
|
||||
testString: assert(__helpers.removeWhiteSpace(addFive.toString()).match(/sum=sum\+5|sum\+=5/));
|
||||
# --hints--
|
||||
|
||||
`addFive` should be a function.
|
||||
|
||||
```js
|
||||
assert(typeof addFive === 'function');
|
||||
```
|
||||
|
||||
</section>
|
||||
Once both functions have ran, the `sum` should be equal to 8.
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
```js
|
||||
assert(sum === 8);
|
||||
```
|
||||
|
||||
<div id='js-seed'>
|
||||
Returned value from `addFive` should be `undefined`.
|
||||
|
||||
```js
|
||||
assert(addFive() === undefined);
|
||||
```
|
||||
|
||||
Inside the `addFive` function, you should add `5` to the `sum` variable.
|
||||
|
||||
```js
|
||||
assert(
|
||||
__helpers.removeWhiteSpace(addFive.toString()).match(/sum=sum\+5|sum\+=5/)
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
// Setup
|
||||
@@ -67,12 +75,7 @@ addThree();
|
||||
addFive();
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
var sum = 0;
|
||||
@@ -88,5 +91,3 @@ function addFive() {
|
||||
addThree();
|
||||
addFive();
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@@ -6,38 +6,53 @@ videoUrl: 'https://scrimba.com/c/cBa2JAL'
|
||||
forumTopicId: 18335
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
When JavaScript variables are declared, they have an initial value of <code>undefined</code>. If you do a mathematical operation on an <code>undefined</code> variable your result will be <code>NaN</code> which means <dfn>"Not a Number"</dfn>. If you concatenate a string with an <code>undefined</code> variable, you will get a literal <dfn>string</dfn> of <code>"undefined"</code>.
|
||||
</section>
|
||||
# --description--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Initialize the three variables <code>a</code>, <code>b</code>, and <code>c</code> with <code>5</code>, <code>10</code>, and <code>"I am a"</code> respectively so that they will not be <code>undefined</code>.
|
||||
</section>
|
||||
When JavaScript variables are declared, they have an initial value of `undefined`. If you do a mathematical operation on an `undefined` variable your result will be `NaN` which means <dfn>"Not a Number"</dfn>. If you concatenate a string with an `undefined` variable, you will get a literal <dfn>string</dfn> of `"undefined"`.
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --instructions--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>a</code> should be defined and evaluated to have the value of <code>6</code>.
|
||||
testString: assert(typeof a === 'number' && a === 6);
|
||||
- text: <code>b</code> should be defined and evaluated to have the value of <code>15</code>.
|
||||
testString: assert(typeof b === 'number' && b === 15);
|
||||
- text: <code>c</code> should not contain <code>undefined</code> and should have a value of "I am a String!"
|
||||
testString: assert(!/undefined/.test(c) && c === "I am a String!");
|
||||
- text: You should not change code below the specified comment.
|
||||
testString: assert(/a = a \+ 1;/.test(code) && /b = b \+ 5;/.test(code) && /c = c \+ " String!";/.test(code));
|
||||
Initialize the three variables `a`, `b`, and `c` with `5`, `10`, and `"I am a"` respectively so that they will not be `undefined`.
|
||||
|
||||
# --hints--
|
||||
|
||||
`a` should be defined and evaluated to have the value of `6`.
|
||||
|
||||
```js
|
||||
assert(typeof a === 'number' && a === 6);
|
||||
```
|
||||
|
||||
</section>
|
||||
`b` should be defined and evaluated to have the value of `15`.
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
```js
|
||||
assert(typeof b === 'number' && b === 15);
|
||||
```
|
||||
|
||||
<div id='js-seed'>
|
||||
`c` should not contain `undefined` and should have a value of "I am a String!"
|
||||
|
||||
```js
|
||||
assert(!/undefined/.test(c) && c === 'I am a String!');
|
||||
```
|
||||
|
||||
You should not change code below the specified comment.
|
||||
|
||||
```js
|
||||
assert(
|
||||
/a = a \+ 1;/.test(code) &&
|
||||
/b = b \+ 5;/.test(code) &&
|
||||
/c = c \+ " String!";/.test(code)
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
(function(a,b,c){ return "a = " + a + ", b = " + b + ", c = '" + c + "'"; })(a,b,c);
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
// Only change code below this line
|
||||
@@ -51,23 +66,7 @@ b = b + 5;
|
||||
c = c + " String!";
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
(function(a,b,c){ return "a = " + a + ", b = " + b + ", c = '" + c + "'"; })(a,b,c);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
var a = 5;
|
||||
@@ -77,5 +76,3 @@ a = a + 1;
|
||||
b = b + 5;
|
||||
c = c + " String!";
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@@ -6,10 +6,11 @@ videoUrl: 'https://scrimba.com/c/c9yEJT4'
|
||||
forumTopicId: 18336
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
# --description--
|
||||
|
||||
After you've created a JavaScript object, you can update its properties at any time just like you would update any other variable. You can use either dot or bracket notation to update.
|
||||
For example, let's look at <code>ourDog</code>:
|
||||
|
||||
For example, let's look at `ourDog`:
|
||||
|
||||
```js
|
||||
var ourDog = {
|
||||
@@ -20,35 +21,35 @@ var ourDog = {
|
||||
};
|
||||
```
|
||||
|
||||
Since he's a particularly happy dog, let's change his name to "Happy Camper". Here's how we update his object's name property:
|
||||
<code>ourDog.name = "Happy Camper";</code> or
|
||||
<code>ourDog["name"] = "Happy Camper";</code>
|
||||
Now when we evaluate <code>ourDog.name</code>, instead of getting "Camper", we'll get his new name, "Happy Camper".
|
||||
</section>
|
||||
Since he's a particularly happy dog, let's change his name to "Happy Camper". Here's how we update his object's name property: `ourDog.name = "Happy Camper";` or `ourDog["name"] = "Happy Camper";` Now when we evaluate `ourDog.name`, instead of getting "Camper", we'll get his new name, "Happy Camper".
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Update the <code>myDog</code> object's name property. Let's change her name from "Coder" to "Happy Coder". You can use either dot or bracket notation.
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
Update the `myDog` object's name property. Let's change her name from "Coder" to "Happy Coder". You can use either dot or bracket notation.
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: You should update <code>myDog</code>'s <code>"name"</code> property to equal "Happy Coder".
|
||||
testString: assert(/happy coder/gi.test(myDog.name));
|
||||
- text: You should not edit the <code>myDog</code> definition.
|
||||
testString: 'assert(/"name": "Coder"/.test(code));'
|
||||
# --hints--
|
||||
|
||||
You should update `myDog`'s `"name"` property to equal "Happy Coder".
|
||||
|
||||
```js
|
||||
assert(/happy coder/gi.test(myDog.name));
|
||||
```
|
||||
|
||||
</section>
|
||||
You should not edit the `myDog` definition.
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
```js
|
||||
assert(/"name": "Coder"/.test(code));
|
||||
```
|
||||
|
||||
<div id='js-seed'>
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
(function(z){return z;})(myDog);
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
// Setup
|
||||
@@ -60,27 +61,9 @@ var myDog = {
|
||||
};
|
||||
|
||||
// Only change code below this line
|
||||
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
(function(z){return z;})(myDog);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
var myDog = {
|
||||
@@ -91,5 +74,3 @@ var myDog = {
|
||||
};
|
||||
myDog.name = "Happy Coder";
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@@ -6,11 +6,13 @@ videoUrl: 'https://scrimba.com/c/ca8JwhW'
|
||||
forumTopicId: 18341
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
<dfn>Bracket notation</dfn> is a way to get a character at a specific <code>index</code> within a string.
|
||||
# --description--
|
||||
|
||||
<dfn>Bracket notation</dfn> is a way to get a character at a specific `index` within a string.
|
||||
|
||||
Most modern programming languages, like JavaScript, don't start counting at 1 like humans do. They start at 0. This is referred to as <dfn>Zero-based</dfn> indexing.
|
||||
For example, the character at index 0 in the word "Charles" is "C". So if <code>var firstName = "Charles"</code>, you can get the value of the first letter of the string by using <code>firstName[0]</code>.
|
||||
|
||||
For example, the character at index 0 in the word "Charles" is "C". So if `var firstName = "Charles"`, you can get the value of the first letter of the string by using `firstName[0]`.
|
||||
|
||||
Example:
|
||||
|
||||
@@ -19,32 +21,35 @@ var firstName = "Charles";
|
||||
var firstLetter = firstName[0]; // firstLetter is "C"
|
||||
```
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Use bracket notation to find the first character in the <code>lastName</code> variable and assign it to <code>firstLetterOfLastName</code>.
|
||||
<strong>Hint: </strong> Try looking at the example above if you get stuck.
|
||||
</section>
|
||||
Use bracket notation to find the first character in the `lastName` variable and assign it to `firstLetterOfLastName`.
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
**Hint:** Try looking at the example above if you get stuck.
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: The <code>firstLetterOfLastName</code> variable should have the value of <code>L</code>.
|
||||
testString: assert(firstLetterOfLastName === 'L');
|
||||
- text: You should use bracket notation.
|
||||
testString: assert(code.match(/firstLetterOfLastName\s*?=\s*?lastName\[.*?\]/));
|
||||
# --hints--
|
||||
|
||||
The `firstLetterOfLastName` variable should have the value of `L`.
|
||||
|
||||
```js
|
||||
assert(firstLetterOfLastName === 'L');
|
||||
```
|
||||
|
||||
</section>
|
||||
You should use bracket notation.
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
```js
|
||||
assert(code.match(/firstLetterOfLastName\s*?=\s*?lastName\[.*?\]/));
|
||||
```
|
||||
|
||||
<div id='js-seed'>
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
(function(v){return v;})(firstLetterOfLastName);
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
// Setup
|
||||
@@ -53,26 +58,9 @@ var lastName = "Lovelace";
|
||||
|
||||
// Only change code below this line
|
||||
firstLetterOfLastName = lastName; // Change this line
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
(function(v){return v;})(firstLetterOfLastName);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
var firstLetterOfLastName = "";
|
||||
@@ -81,5 +69,3 @@ var lastName = "Lovelace";
|
||||
// Only change code below this line
|
||||
firstLetterOfLastName = lastName[0];
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@@ -6,10 +6,11 @@ videoUrl: 'https://scrimba.com/c/cBZQGcv'
|
||||
forumTopicId: 18342
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
# --description--
|
||||
|
||||
In order to get the last letter of a string, you can subtract one from the string's length.
|
||||
For example, if <code>var firstName = "Charles"</code>, you can get the value of the last letter of the string by using <code>firstName[firstName.length - 1]</code>.
|
||||
|
||||
For example, if `var firstName = "Charles"`, you can get the value of the last letter of the string by using `firstName[firstName.length - 1]`.
|
||||
|
||||
Example:
|
||||
|
||||
@@ -18,32 +19,35 @@ var firstName = "Charles";
|
||||
var lastLetter = firstName[firstName.length - 1]; // lastLetter is "s"
|
||||
```
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Use <dfn>bracket notation</dfn> to find the last character in the <code>lastName</code> variable.
|
||||
<strong>Hint: </strong> Try looking at the example above if you get stuck.
|
||||
</section>
|
||||
Use <dfn>bracket notation</dfn> to find the last character in the `lastName` variable.
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
**Hint:** Try looking at the example above if you get stuck.
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>lastLetterOfLastName</code> should be "e".
|
||||
testString: assert(lastLetterOfLastName === "e");
|
||||
- text: You should use <code>.length</code> to get the last letter.
|
||||
testString: assert(code.match(/\.length/g).length > 0);
|
||||
# --hints--
|
||||
|
||||
`lastLetterOfLastName` should be "e".
|
||||
|
||||
```js
|
||||
assert(lastLetterOfLastName === 'e');
|
||||
```
|
||||
|
||||
</section>
|
||||
You should use `.length` to get the last letter.
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
```js
|
||||
assert(code.match(/\.length/g).length > 0);
|
||||
```
|
||||
|
||||
<div id='js-seed'>
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
(function(v){return v;})(lastLetterOfLastName);
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
// Setup
|
||||
@@ -51,31 +55,11 @@ var lastName = "Lovelace";
|
||||
|
||||
// Only change code below this line
|
||||
var lastLetterOfLastName = lastName; // Change this line
|
||||
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
(function(v){return v;})(lastLetterOfLastName);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
var lastName = "Lovelace";
|
||||
var lastLetterOfLastName = lastName[lastName.length - 1];
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@@ -6,10 +6,11 @@ videoUrl: 'https://scrimba.com/c/cWPVJua'
|
||||
forumTopicId: 18343
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
# --description--
|
||||
|
||||
You can also use <dfn>bracket notation</dfn> to get the character at other positions within a string.
|
||||
Remember that computers start counting at <code>0</code>, so the first character is actually the zeroth character.
|
||||
|
||||
Remember that computers start counting at `0`, so the first character is actually the zeroth character.
|
||||
|
||||
Example:
|
||||
|
||||
@@ -18,32 +19,35 @@ var firstName = "Ada";
|
||||
var secondLetterOfFirstName = firstName[1]; // secondLetterOfFirstName is "d"
|
||||
```
|
||||
|
||||
</section>
|
||||
# --instructions--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Let's try to set <code>thirdLetterOfLastName</code> to equal the third letter of the <code>lastName</code> variable using bracket notation.
|
||||
<strong>Hint: </strong> Try looking at the example above if you get stuck.
|
||||
</section>
|
||||
Let's try to set `thirdLetterOfLastName` to equal the third letter of the `lastName` variable using bracket notation.
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
**Hint:** Try looking at the example above if you get stuck.
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: The <code>thirdLetterOfLastName</code> variable should have the value of <code>v</code>.
|
||||
testString: assert(thirdLetterOfLastName === 'v');
|
||||
- text: You should use bracket notation.
|
||||
testString: assert(code.match(/thirdLetterOfLastName\s*?=\s*?lastName\[.*?\]/));
|
||||
# --hints--
|
||||
|
||||
The `thirdLetterOfLastName` variable should have the value of `v`.
|
||||
|
||||
```js
|
||||
assert(thirdLetterOfLastName === 'v');
|
||||
```
|
||||
|
||||
</section>
|
||||
You should use bracket notation.
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
```js
|
||||
assert(code.match(/thirdLetterOfLastName\s*?=\s*?lastName\[.*?\]/));
|
||||
```
|
||||
|
||||
<div id='js-seed'>
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
(function(v){return v;})(thirdLetterOfLastName);
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
// Setup
|
||||
@@ -51,31 +55,11 @@ var lastName = "Lovelace";
|
||||
|
||||
// Only change code below this line
|
||||
var thirdLetterOfLastName = lastName; // Change this line
|
||||
|
||||
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
|
||||
```js
|
||||
(function(v){return v;})(thirdLetterOfLastName);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
var lastName = "Lovelace";
|
||||
var thirdLetterOfLastName = lastName[2];
|
||||
```
|
||||
|
||||
</section>
|
||||
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user