0

I have been set an assignment to create my own simplified version of the game cookie clicker (http://orteil.dashnet.org/cookieclicker/). I have managed to get almost everything working with the exception of one thing, I'm trying to print an alert to say that in order to buy an upgrade, you first need X amounts more rashers of bacon, (the player increases their amount of rashers through clicking on the image of bacon).

These are my current declared variables:

<script>
var RPS=0;
var RPC=1;
var bacon=0;
var RPCPrice=50;
var RPSPrice=50;
</script>

With RPS meaning the current number of rashers the player is getting per second, RPC meaning the current number of rashers the player is getting per click, and RPC/RPS Price being the cost in rashers of the next upgrade.

I have managed to get an alert to say the correct number of rashers a player is away from the price of the next upgrade by using:

document.getElementById('errors').innerHTML="You can't afford this RPC upgrade, you need " + (RPCPrice-bacon + " more rashers!");

However was wondering if there was a more efficient way to do this by declaring a variable suchas:

var neededRPS=Math.round(RPSPrice-bacon);

and then changing the alert too:

document.getElementById('errors').innerHTML="You can't afford this RPS upgrade, you need " + neededRPS + " more rashers!");

however this does not seem to work.

Any help would be much appreciated, or if anybody knows of any simpler way to do this please enlighten me! I am a javascript newbie so apologies in advance if this has a very simple fix i have missed.

Cheers

Joe Perkins
  • 261
  • 3
  • 9
  • 17

1 Answers1

3

You have a syntax error in your code which is why it likely doesn't work. Remove the end ). The follow is the line with the error removed.

document.getElementById('errors').innerHTML="You can't afford this RPS upgrade, you need " + neededRPS + " more rashers!"; 

For errors like this try looking at your browsers javascript console

Community
  • 1
  • 1
Danny
  • 7,368
  • 8
  • 46
  • 70
  • Thankyou! This managed to print the alert out, however it is now saying i always need 50 more rashers, which is just the current value of RPSPrice. Have i declared var neededRPS=Math.round(RPSPrice-bacon); correctly? – Joe Perkins Jan 06 '14 at 15:14
  • You need to update the innerHtml and variable everytime there is a change to the value of `bacon`. – Danny Jan 06 '14 at 15:17
  • @JoePerkins Does `bacon` get a different value as the user uses the page? Your second way is only calculating the value of `neededRPS` once, when `RPSPrice` is `50` and `bacon` is `0`. Your message will then always print that value. Your original way dynamically computed the value with the current values of `RPSPrice` and `bacon`, so you have probably changed the behavior of your program by making this "optimization" (this is such a small performance gain that you probably don't need to worry about it). – ajp15243 Jan 06 '14 at 15:17
  • Ahhh ok this clears it up. Yeah `bacon` increases every time the user clicks on the image, and `RPSPrice` increases in value every time the user upgrades, (higher upgrades cost more rashers). Will leave it be then, thanks a lot for your help! – Joe Perkins Jan 06 '14 at 15:20