Search code examples
javascriptnumberstransfer

Can I transfer the value of one variable to another in Javascript


I'm making a simple game to help learn the basics of Javascript. During the game the player increases "Grain" every time a button is pressed. This value is displayed to the player by a counter named "grainTotal".

What I'd like to do is for the player to be able to press another button and reset the "grainTotal" counter to 0 whilst transferring the original value to another counter - named "storeTotal".

This is what I've tried so far, and whilst it does reset the "grainTotal" counter, all the "storeTotal" counter does is return "[object Object]"

function buildStore() { /*transfers the value of grainCount to storeCount and resets grainCount to 0 */

storeCount = {grainCount};
grainCount = 0;
grainTotal = document.getElementById('grain').innerHTML = grainCount;
storeTotal = document.getElementById('storeTotal').innerHTML = storeCount;   
}

Is what I'm trying to do even possible? Can anyone help me see what I'm missing?


Solution

  • Why are you wrapping grainCount in brackets? Just assign the value to storeCount like so:

    storeCount = grainCount;