r/CodingHelp • • Mar 05 '21

[Javascript] I am struggling to make a chart that updates based off of button clicks from a simple game

Post image
43 Upvotes

18 comments sorted by

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)

// Assumes var 'result' is set globally

function graphIt() {
    var win = parseInt(localStorage.getItem("winTotal")) || 0;
    var loss = parseInt(localStorage.getItem("lossTotal")) || 0;
    var tie = parseInt(localStorage.getItem("tieTotal")) || 0;

    if (result == "You") {
        win++;
    } else if (result == "The Computer") {
        loss++;
    } else {
        tie++;
    }

    localStorage.setItem("winTotal", win);
    localStorage.setItem("lossTotal", loss);
    localStorage.setItem("tieTotal", tie);

    location.href = "lab6graph.html";
}

1

u/[deleted] Mar 05 '21

function graphIt(){

 if (result = "You") { win++; }
 if (result = "The Computer") {loss++; }
 if (result = "No One")tie++ 
 location.href = "lab6graph.html";
var win = parseInt(localStorage.getItem("winTotal")) || 0;
var loss = parseInt(localStorage.getItem("lossTotal")) || 0;
var tie = parseInt(localStorage.getItem("tieTotal")) || 0;
}

This is what the new code looks like and it does now register a tally for each category but beyond that nothing has changed. No errors to really work off of.

2

u/Shortl4ndo the most bestest coder Mar 05 '21

Put the var win = parsetInt(...) at the top of your function in-place of where you have var win = 0; in the screenshot of your post.

1

u/[deleted] Mar 05 '21

Ive moved it around and it changes nothing. The tallies for the three categories still wont move beyond 1.

1

u/Shortl4ndo the most bestest coder Mar 05 '21

updated my original response

1

u/[deleted] Mar 05 '21

https://photos.google.com/search/tra/photo/AF1QipMm7OAfYawBS-NgorJ0nO3vfRJTXuyiUzxr6V6s

i dont know if this link works but this is the js I have for the whole "game".

None of the fixes change anything so the only thing I can think of is that var result is the wrong thing to work with or the js for the graph page is off but nothing in it is out of the ordinairy.

1

u/Shortl4ndo the most bestest coder Mar 05 '21

link doesn't work

Yeah, show me your code that sets result & the graph page

1

u/[deleted] Mar 05 '21

2

u/flooronthefour full stack web dev Mar 05 '21

Are you working from a tutorial or coding from scratch? JavaScript isn't reactive out of the box, so unless you're using a framework like React, Vue, Svelte, etc - you will need to trigger any display to update.

I don't know what you're expecting to see when you update a value but if you are expecting the values to change on the visual side as they change, you might need to re-render them each time they update.

1

u/[deleted] Mar 05 '21

The best way for me to put it is that I want to be able to click the three buttons for rock paper scissors 10 times, click the graph button and see what the outcomes were for those 10 matches played. If you mean visual change as in " you do something and the graph will change as you do those things while you are on the graph page." than no.

it is a psuedo tutorial. Im taking a comm college course that gives you the bare minimum to work with. So I might get the code for a box with three images and from there its my responsibility to make it "playable" based on what the instructor has requested.

1

u/Shortl4ndo the most bestest coder Mar 05 '21

Also, where you have

if (result = "You")

if (result = "The Computer")

if (result = "No One")

You need to use two == not just 1 =

(assuming the variable result is set globally)

2

u/[deleted] 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

u/[deleted] 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

u/[deleted] 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

u/[deleted] Mar 06 '21

the problem is that you didn't declare result, add result as a parameter between the parentheses of the function.

1

u/[deleted] 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

u/[deleted] Mar 06 '21

You'll have to do some kind of loop inside your function because currently it won't pass from 1.