fix: replace inline code blocks (#41576)
This commit is contained in:
@ -13,11 +13,15 @@ You can add new properties to existing JavaScript objects the same way you would
|
||||
|
||||
Here's how we would add a `bark` property to `ourDog`:
|
||||
|
||||
`ourDog.bark = "bow-wow";`
|
||||
```js
|
||||
ourDog.bark = "bow-wow";
|
||||
```
|
||||
|
||||
or
|
||||
|
||||
`ourDog["bark"] = "bow-wow";`
|
||||
```js
|
||||
ourDog["bark"] = "bow-wow";
|
||||
```
|
||||
|
||||
Now when we evaluate `ourDog.bark`, we'll get his bark, `bow-wow`.
|
||||
|
||||
|
@ -13,7 +13,9 @@ If you'll recall from our discussion of [Storing Values with the Assignment Oper
|
||||
|
||||
Assume we have pre-defined a function `sum` which adds two numbers together, then:
|
||||
|
||||
`ourSum = sum(5, 12);`
|
||||
```js
|
||||
ourSum = sum(5, 12);
|
||||
```
|
||||
|
||||
will call `sum` function, which returns a value of `17` and assigns it to `ourSum` variable.
|
||||
|
||||
|
@ -11,7 +11,9 @@ dashedName: compound-assignment-with-augmented-addition
|
||||
|
||||
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:
|
||||
|
||||
`myVar = myVar + 5;`
|
||||
```js
|
||||
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.
|
||||
|
||||
|
@ -11,11 +11,15 @@ dashedName: compound-assignment-with-augmented-division
|
||||
|
||||
The `/=` operator divides a variable by another number.
|
||||
|
||||
`myVar = myVar / 5;`
|
||||
```js
|
||||
myVar = myVar / 5;
|
||||
```
|
||||
|
||||
Will divide `myVar` by `5`. This can be rewritten as:
|
||||
|
||||
`myVar /= 5;`
|
||||
```js
|
||||
myVar /= 5;
|
||||
```
|
||||
|
||||
# --instructions--
|
||||
|
||||
|
@ -11,11 +11,15 @@ dashedName: compound-assignment-with-augmented-multiplication
|
||||
|
||||
The `*=` operator multiplies a variable by a number.
|
||||
|
||||
`myVar = myVar * 5;`
|
||||
```js
|
||||
myVar = myVar * 5;
|
||||
```
|
||||
|
||||
will multiply `myVar` by `5`. This can be rewritten as:
|
||||
|
||||
`myVar *= 5;`
|
||||
```js
|
||||
myVar *= 5;
|
||||
```
|
||||
|
||||
# --instructions--
|
||||
|
||||
|
@ -11,11 +11,15 @@ dashedName: compound-assignment-with-augmented-subtraction
|
||||
|
||||
Like the `+=` operator, `-=` subtracts a number from a variable.
|
||||
|
||||
`myVar = myVar - 5;`
|
||||
```js
|
||||
myVar = myVar - 5;
|
||||
```
|
||||
|
||||
will subtract `5` from `myVar`. This can be rewritten as:
|
||||
|
||||
`myVar -= 5;`
|
||||
```js
|
||||
myVar -= 5;
|
||||
```
|
||||
|
||||
# --instructions--
|
||||
|
||||
|
@ -17,9 +17,7 @@ Having more high cards remaining in the deck favors the player. Each card is ass
|
||||
|
||||
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.
|
||||
|
||||
**Example Output**
|
||||
`-3 Hold`
|
||||
`5 Bet`
|
||||
**Example Outputs:** `-3 Hold` or `5 Bet`
|
||||
|
||||
**Hint**
|
||||
Do NOT reset `count` to 0 when value is 7, 8, or 9.
|
||||
|
@ -11,7 +11,9 @@ dashedName: declare-string-variables
|
||||
|
||||
Previously we have used the code
|
||||
|
||||
`var myName = "your name";`
|
||||
```js
|
||||
var myName = "your name";
|
||||
```
|
||||
|
||||
`"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.
|
||||
|
||||
|
@ -11,11 +11,15 @@ dashedName: decrement-a-number-with-javascript
|
||||
|
||||
You can easily <dfn>decrement</dfn> or decrease a variable by one with the `--` operator.
|
||||
|
||||
`i--;`
|
||||
```js
|
||||
i--;
|
||||
```
|
||||
|
||||
is the equivalent of
|
||||
|
||||
`i = i - 1;`
|
||||
```js
|
||||
i = i - 1;
|
||||
```
|
||||
|
||||
**Note:** The entire line becomes `i--;`, eliminating the need for the equal sign.
|
||||
|
||||
|
@ -11,7 +11,9 @@ dashedName: delete-properties-from-a-javascript-object
|
||||
|
||||
We can also delete properties from objects like this:
|
||||
|
||||
`delete ourDog.bark;`
|
||||
```js
|
||||
delete ourDog.bark;
|
||||
```
|
||||
|
||||
Example:
|
||||
|
||||
|
@ -13,17 +13,23 @@ When you are defining a string you must start and end with a single or double qu
|
||||
|
||||
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\".";`
|
||||
```js
|
||||
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:
|
||||
|
||||
`Alan said, "Peter is learning JavaScript".`
|
||||
```js
|
||||
Alan said, "Peter is learning JavaScript".
|
||||
```
|
||||
|
||||
# --instructions--
|
||||
|
||||
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".`
|
||||
```js
|
||||
I am a "double quoted" string inside "double quotes".
|
||||
```
|
||||
|
||||
# --hints--
|
||||
|
||||
|
@ -17,7 +17,9 @@ Remember that `Math.random()` can never quite return a `1` and, because we're ro
|
||||
|
||||
Putting everything together, this is what our code looks like:
|
||||
|
||||
`Math.floor(Math.random() * 20);`
|
||||
```js
|
||||
Math.floor(Math.random() * 20);
|
||||
```
|
||||
|
||||
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.
|
||||
|
||||
|
@ -15,7 +15,9 @@ 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:
|
||||
|
||||
`Math.floor(Math.random() * (max - min + 1)) + min`
|
||||
```js
|
||||
Math.floor(Math.random() * (max - min + 1)) + min
|
||||
```
|
||||
|
||||
# --instructions--
|
||||
|
||||
|
@ -11,11 +11,15 @@ dashedName: increment-a-number-with-javascript
|
||||
|
||||
You can easily <dfn>increment</dfn> or add one to a variable with the `++` operator.
|
||||
|
||||
`i++;`
|
||||
```js
|
||||
i++;
|
||||
```
|
||||
|
||||
is the equivalent of
|
||||
|
||||
`i = i + 1;`
|
||||
```js
|
||||
i = i + 1;
|
||||
```
|
||||
|
||||
**Note:** The entire line becomes `i++;`, eliminating the need for the equal sign.
|
||||
|
||||
|
@ -11,7 +11,9 @@ dashedName: initializing-variables-with-the-assignment-operator
|
||||
|
||||
It is common to <dfn>initialize</dfn> a variable to an initial value in the same line as it is declared.
|
||||
|
||||
`var myVar = 0;`
|
||||
```js
|
||||
var myVar = 0;
|
||||
```
|
||||
|
||||
Creates a new variable called `myVar` and assigns it an initial value of `0`.
|
||||
|
||||
|
@ -13,7 +13,9 @@ Create a shopping list in the variable `myList`. The list should be a multi-dime
|
||||
|
||||
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.
|
||||
|
||||
`["Chocolate Bar", 15]`
|
||||
```js
|
||||
["Chocolate Bar", 15]
|
||||
```
|
||||
|
||||
There should be at least 5 sub-arrays in the list.
|
||||
|
||||
|
@ -13,7 +13,9 @@ With JavaScript `array` variables, we can store several pieces of data in one pl
|
||||
|
||||
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:
|
||||
|
||||
`var sandwich = ["peanut butter", "jelly", "bread"]`
|
||||
```js
|
||||
var sandwich = ["peanut butter", "jelly", "bread"]
|
||||
```
|
||||
|
||||
# --instructions--
|
||||
|
||||
|
@ -11,7 +11,9 @@ dashedName: storing-values-with-the-assignment-operator
|
||||
|
||||
In JavaScript, you can store a value in a variable with the <dfn>assignment</dfn> operator (`=`).
|
||||
|
||||
`myVariable = 5;`
|
||||
```js
|
||||
myVariable = 5;
|
||||
```
|
||||
|
||||
This assigns the `Number` value `5` to `myVariable`.
|
||||
|
||||
|
@ -13,11 +13,15 @@ The `parseInt()` function parses a string and returns an integer. It takes a sec
|
||||
|
||||
The function call looks like:
|
||||
|
||||
`parseInt(string, radix);`
|
||||
```js
|
||||
parseInt(string, radix);
|
||||
```
|
||||
|
||||
And here's an example:
|
||||
|
||||
`var a = parseInt("11", 2);`
|
||||
```js
|
||||
var a = parseInt("11", 2);
|
||||
```
|
||||
|
||||
The radix variable says that `11` is in the binary system, or base 2. This example converts the string `11` to an integer `3`.
|
||||
|
||||
|
@ -11,7 +11,9 @@ dashedName: use-the-parseint-function
|
||||
|
||||
The `parseInt()` function parses a string and returns an integer. Here's an example:
|
||||
|
||||
`var a = parseInt("007");`
|
||||
```js
|
||||
var a = parseInt("007");
|
||||
```
|
||||
|
||||
The above function converts the string `007` to the integer `7`. If the first character in the string can't be converted into a number, then it returns `NaN`.
|
||||
|
||||
|
Reference in New Issue
Block a user