Tell us what’s happening:
Hello, I know I should use Chrome for this step, but I’m trying to explain whats happening so that maybe it’ll help you fix the problem:
When I use “currentWeaponIndex” as the index for weapons array in Firefox, the hint and console says:
You should add the result of Math.floor(Math.random() * xp) + 1 to the result of weapons[currentWeapon].power
which is obviously wrong, and if I use “currentWeapon”, the console says both:
You should add to the weapons[currentWeaponIndex].power value.
You should add the result of Math.floor(Math.random() * xp) + 1 to the result of weapons[currentWeapon].power.
And if I don’t place Math.floor(Math.random() * xp) + 1 inside parenthesis, the code passes.
So you should fix it as it throws the wrong error in Firefox and prevents users to pass this step. thanks
Your code so far
<!-- file: index.html -->
/* file: styles.css */
/* file: script.js */
// User Editable Region
monsterHealth -= weapons[currentWeaponIndex].power + (Math.floor(Math.random() * xp) + 1);
// User Editable Region
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:125.0) Gecko/20100101 Firefox/125.0
Challenge Information:
Learn Basic JavaScript by Building a Role Playing Game - Step 124
I’m not sure what do you think is wrong?
Where did you find the instructions for wrapping the additional statement in parentheses?
it says to use currentWeapon instead of currentWeaponIndex
nowhere, but it’s not wrong and the code should pass
I can’t see that. Can you show me the exact code you typed in so I can see the same hint?
the extra parens don’t hurt I think?
monsterHealth -= weapons[currentWeaponIndex].power + (Math.floor(Math.random() * xp) + 1);
I have deleted the post because it gives the final solution. The intention was only to prove that parentheses are a surplus in this case.
yes, see? throws a wrong error, and it only passes if I remove the additional parenthesis
The extra parentheses aren’t doing anything useful so I can see why they weren’t expected by the tests
Yes, of course, but I guess IRL some people prefer to use it as it might help for better readability, not specifically in this code, I’m talking generally, am I right? I’m new btw.
anyways, it should be fixed
In this case, no I wouldn’t say that these unnecessary ()s help readability.
yes I see that now, thanks for posting it.
I think we should report the message on github. Here’s the message for reference:
You should add the result of Math.floor(Math.random() * xp) + 1 to the result of weapons[currentWeapon].power.
And yes the bit about weapons[currentWeapon].power
should be corrected to weapons[currentWeaponIndex].power
Do you want to go ahead and try to report it?
(or shall I? let me know)
Here’s the github link:
1 Like
it turns out that the tests are behaving weirdly because I tried the following two lines of code:
monsterHealth -= weapons[currentWeaponIndex].power + (Math.floor(Math.random() * xp));
The above passes even with the extra paren. (and despite the missing +1, I’ve opened an issue for this one already)
monsterHealth -= weapons[currentWeaponIndex].power + (Math.floor(Math.random() * xp) + 1);
While this one which is functionally correct with extra parens doesn’t pass and shows a hint message that is incorrect:
You should add the result of Math.floor(Math.random() * xp) + 1 to the result of weapons[currentWeapon].power.
(the wrong bit is the one in the square brackets which should have said currentWeaponIndex). I believe this is what the OP was referring to.
1 Like
Ok, yes I agree with you in this case it doesn’t help much for readability, but in step 146 extra parens did help:
(level * 5) - (Math.floor(Math.random() * xp));
Please do if you can as I’m not familiar with Github yet
1 Like
Yes that’s why I said “The console throws a wrong error”, and it actually misleads and confuses people
1 Like
That’s another place where I wouldn’t use superfluous ()s
1 Like