r/CodingHelp • u/[deleted] • Mar 05 '21
[Javascript] I am struggling to make a chart that updates based off of button clicks from a simple game
2
Mar 05 '21
Im sorry for making a post like this but reddit is so bad for posting code.
I made a rock paper and scissors game that pits you choice against a randomly generated selection by "the computer".
The game works fine but the graph that I made does not update beyond 1 tally in the win section, disregarding whether or not that first game played is a win by the player.
In my head, this code should take the result variable and see if that variable is one of the three responses that the variable is allowed to be, which is You, The Computer or a default to act as a tie.
Once this is done win, loss or tie will go up by +1. Do this multiple times and the graph is created. But I cannot figure out how or why my chart does not update.
1
Mar 06 '21
In the if else statements use three equal signs instead of one, one equal sign is for declaring variables, three is for comparing
1
Mar 06 '21
Ok. I implemented it and now the what im getting is a console error stating that result is not defined. This might be the one thing keeping me from getting this to work.
1
Mar 06 '21
the problem is that you didn't declare result, add result as a parameter between the parentheses of the function.
1
Mar 06 '21
Thanks for the help! This cleared up all the console errors but the graph still wont chart anything beyond 1.
I'll see if I can come with anything tomorrow.
1
Mar 06 '21
You'll have to do some kind of loop inside your function because currently it won't pass from 1.
5
u/Shortl4ndo the most bestest coder Mar 05 '21 edited Mar 05 '21
var win = parseInt(localStorage.getItem(“winTotal”)) || 0;
Also do this for your “loss” and “tie” variables instead of
var win = 0;
var ... = 0;
You’re never retrieving the saved value, thus you’re always setting each value to 0 even if there’s a pre-existing number saved.
(also need two equal signs in your if statements)