DnD stat rolls | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

DnD stat rolls

https://code.sololearn.com/W4otVCqKrm32/?ref=app The output is fine until it isn't and i don't know what's wrong exactly. In some iterations it works as intended, removing the smallest number and returning the sum of the rest, and then BOOM removes a random number. Also having 4 same numbers also causes problems but having 2 sets of them like 1-1-2-2 works fine.

22nd Feb 2023, 8:52 AM
Unicorn Of Death
Unicorn Of Death - avatar
6 Answers
+ 3
I fixed your code, and edited mine, so check it in comment above. Yes your problem was with splice method, and also you didn't used index, you used value from Math.min, so i found index of smallest number, then I created copy of state array, because splice modify original, and we shouldn't change state in that way, it can trigger rerender and cause many problems. Then I removed smallest item from copyArray(it work even if we have more than one same number, it remove first). And then I do sum of others number from copy array, array with removed lowest number. I also posted comments in my code, so you can see what each line do.
22nd Feb 2023, 8:23 PM
PanicS
PanicS - avatar
+ 2
Your code is not created to support react, please set inviroment (add react via cdn) or post us code from some sandbox what support react, like codesandbox, codepen... Don't just copy/paste code if you use framework, we can't see output, and If i try to create one by myself I may not have same error as you do, for bigger application link to github repo will probably be best option, but for small one you can use cdn or online sandbox.
22nd Feb 2023, 10:32 AM
PanicS
PanicS - avatar
+ 2
I tried but it didn't wanna work at first try so I changed cdn I was using. Here is code so others can see and test it. Also what you expect to happen with stats? To sum every number except lowest? What should happen if there are 2 same low numbers? Please describe us more about expected result, also if you face issue with some numbers please tell us what numbers are making problem and what result you are expecting to get. This may give us more clue. https://code.sololearn.com/W2cAVnV41Dx5/?ref=app
22nd Feb 2023, 1:28 PM
PanicS
PanicS - avatar
0
Can't you just copy the react code and connect it to a div with id="dice"? I'm new to this and am not able to make any alterations right now since i am away at work
22nd Feb 2023, 10:58 AM
Unicorn Of Death
Unicorn Of Death - avatar
0
Thank you for your dedication, yes the purpose of the function is to generate 4 random numbers (1-6) and display them, then to remove the smallest value from the array and to return the sum of the rest, it works fine most of the time and i don't know where it goes wrong as when the numbers are 1-1-2-2 the function works and returns a 5, but another time when it returned 5-3-3-1 it returned a 9, i'm assuming it removed one of the 3s, as stated before it works sometimes and sometimes it doesn't i'm not sure where the problem is, somebody on stack said that i'm using splice wrong so maybe try changing that to work like (...arr).splice, thank you again for your efforts
22nd Feb 2023, 3:17 PM
Unicorn Of Death
Unicorn Of Death - avatar
0
Thank you, the code now works perfectly, i'm gonna have to read on the index methods, they seem pretty useful
23rd Feb 2023, 8:05 AM
Unicorn Of Death
Unicorn Of Death - avatar