+ 1

Code not working JavaScript

in my function "addIntel()", I created a function that would let me add stat points to a character after the character was created. I have encountered some problems: 1. When there are no stat points available, I am still alerted that a character has not been created yet. I believe this is because the function cannot reference an update in a value from another function. If so, is there a way I can work around it? 2. When I add stats, it will set the stats back to zero and add up from there. I want the stats to add up from the set stats that were made when you created a character. Example: Intelligence stat is 50. I want it to start at fifty and work up. 50 +1, +1, +1, etc. Instead it works like this: Intelligence is 50. stats get set to 0 on click and add up from there: 0 +1, +1, +1. https://code.sololearn.com/WHyfth9gjoer/#js

26th Nov 2018, 8:37 PM
Yosharu
Yosharu - avatar
2 Answers
+ 2
Both of your problems arise from the same mistake: You took the input in distinct, local variables and used them to display the character's stats, but you never update the original variables like 'Magic', etc. to the values that were entered by the user. Thus, Intelligence is set back to 0 because the variable still is 0, and it says you need to create a new character when you have all your points spent because the name is still null. The solution would be to work with the global variables from the top of your code from the start (no local variables for input), or to update them after the input has been done. Either way should suffice. Here is a working version: https://code.sololearn.com/WdsD4Wawa3bD/?ref=app
26th Nov 2018, 10:22 PM
Shadow
Shadow - avatar
+ 1
Shadow Thank you so much for your help!
27th Nov 2018, 3:39 PM
Yosharu
Yosharu - avatar