mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-06-22 21:08:12 +08:00
fix(curriculum): improve regex in rpg project step 123 (#53052)
Co-authored-by: Mama Naomi <nhcarrigan@gmail.com>
This commit is contained in:
parent
c58ace2a80
commit
630e5e8b90
@ -7,11 +7,11 @@ dashedName: step-123
|
||||
|
||||
# --description--
|
||||
|
||||
JavaScript offers the built-in `Math` utility which offers a number of mathematical tools. One of those is `Math.random()`, which generates a random number between 0 and 1. Another is `Math.floor()`, which rounds a given number down to the nearest integer.
|
||||
The `Math` object in JavaScript contains static properties and methods for mathematical constants and functions. One of those is `Math.random()`, which generates a random number from `0` (inclusive) to `1` (exclusive). Another is `Math.floor()`, which rounds a given number down to the nearest integer.
|
||||
|
||||
Using these, you can generate a random number within a range. For example, this generates a random number between 1 and 5: `Math.floor(Math.random() * 5) + 1;`.
|
||||
|
||||
Following this pattern, use the addition operator (`+`) to add a random number between `1` and the value of `xp` to your `monsterHealth` variable change.
|
||||
Following this pattern, use the addition operator (`+`) to add a random number between `1` and the value of `xp` to your `monsterHealth -= weapons[currentWeapon].power`.
|
||||
|
||||
# --hints--
|
||||
|
||||
@ -48,19 +48,19 @@ assert.match(attack.toString(), /Math\.random\(\)\s*\*\s*xp/);
|
||||
You should use `Math.floor()` to round the result of `Math.random() * xp` down.
|
||||
|
||||
```js
|
||||
assert.match(attack.toString(), /Math\.floor\(\s*Math\.random\(\)\s*\*\s*xp\s*\)/);
|
||||
assert.match(attack.toString(), /Math\.floor\(\s*Math\.random\(\)\s*\*\s*xp\s*.*\)/);
|
||||
```
|
||||
|
||||
You should add `1` to the result of `Math.floor()`.
|
||||
|
||||
```js
|
||||
assert.match(attack.toString(), /Math\.floor\(\s*Math\.random\(\)\s*\*\s*xp\s*\)\s*\+\s*1/);
|
||||
assert.match(attack.toString(), /Math\.floor\(Math\.random\(\)\s*\*\s*xp(?:\s*\+\s*1)?\)(?:\s*\+ 1)?/);
|
||||
```
|
||||
|
||||
You should add the result of `Math.floor(Math.random() * xp) + 1` to the result of `weapons[currentWeapon].power`.
|
||||
|
||||
```js
|
||||
assert.match(attack.toString(), /monsterHealth\s*-=\s*weapons\[currentWeapon\]\.power\s*\+\s*Math\.floor\(\s*Math\.random\(\)\s*\*\s*xp\s*\)\s*\+\s*1/);
|
||||
assert.match(attack.toString(), /monsterHealth\s*-=\s*weapons\[currentWeapon\]\.power\s*\+\s*Math\.floor\(Math\.random\(\)\s*\*\s*xp(?:\s*\+\s*1)?\)(?:\s*\+ 1)?/);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
@ -310,14 +310,15 @@ function goFight() {
|
||||
monsterHealthText.innerText = monsterHealth;
|
||||
}
|
||||
|
||||
--fcc-editable-region--
|
||||
|
||||
function attack() {
|
||||
text.innerText = "The " + monsters[fighting].name + " attacks.";
|
||||
text.innerText += " You attack it with your " + weapons[currentWeapon].name + ".";
|
||||
health -= monsters[fighting].level;
|
||||
--fcc-editable-region--
|
||||
monsterHealth -= weapons[currentWeapon].power;
|
||||
--fcc-editable-region--
|
||||
}
|
||||
--fcc-editable-region--
|
||||
|
||||
function dodge() {
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user