Hello,
I do not quite understan the insrtructions of step 124.
I have tried several combinations even though it does not seem too complicated.
Instructions:
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 -= weapons[currentWeaponIndex].power
.
One refused solution:
monsterHealth -= weapons[currentWeaponIndex].power + (Math.floor(Math.random() * xp) + 1);
The comment:
You should add the result of Math.floor(Math.random() * xp) + 1
to the result of weapons[currentWeapon].power
.
Another:
monsterHealth -= weapons[currentWeaponIndex].power weapons[currentWeapon].power += (Math.floor(Math.random() * xp) + 1);
Thank you in advance for your help.
hi there!
also post your code and link to the step.
[currentWeapon]
above is the currentWeapon
index.
Thank you so much for your reply.
The link is: https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures-v8/learn-basic-javascript-by-building-a-role-playing-game/step-124
The problem is that with all these micro-instructions, I do not have the overall vision of what needs to be achieved.
monsterHealth -= weapons[currentWeaponIndex].power;
weapons[currentWeapon] += (Math.floor(Math.random() * xp) + 1);
That too is wrong!
this without the parenthesis that are not needed seems a good solution
Thank you so much but… this is the response:
You should add the result of Math.floor(Math.random() * xp) + 1
to the result of weapons[currentWeapon].power
.
Thank you.
Here is the code:
monsterHealth -= weapons[currentWeaponIndex].power + (Math.floor(Math.random() * xp) + 1);
and that’s the response:
You should add the result of Math.floor(Math.random() * xp) + 1
to the result of weapons[currentWeapon].power
.
1 Like
as I said, remove the parenthesis you can do without
1 Like
Thank you, ilenia, that was it.
Grateful for your kind help!
Would it be possible to report this as a bug? Unless I misunderstand, parentheses are mathematically correct and shouldn’t affect the code unless placed incorrectly.
Even if it comes to the same result, I would argue that they shouldn’t be added if not needed.
I can see how it makes the phrases more organized for human reading but that would likely be done with line breaks if it got complex. Something like:
monsterHealth -= weapons[currentWeaponIndex].power +
Math.floor(Math.random() * xp) +
1;
it could be reported as a bug, but to write tests that can catch all the variations, it becomes incresingly complex, and it’s something we tend not to do.
Good style tip!
And, understood @im59138 . I initially thought my logic was wrong, so it’s helpful to learn here that it’s more of a style error than a logic error.
Thanks, both!