Stuck on using a variable in a simple javascript program - javascript

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

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

Related

How can I add permanent data into an variable

I'm working on a project that plays a game of rock paper scissors
the game works fine but i want to add something into it. a pointing system where it will add 1 each time a player win but i can't seem to make this work i've been trying to find workarounds for almost 2 day now but i can't seem to find any solution. The thing is the code works but after i log the function that adds 1 to a variable but after that it returns to it's original value which is 0.
const one = 1
function addone(test){
test += 1
}
addone(one)
console.log(one)
This print the original value like the function did not do anything to it. im so confused.
I see two problems in your code:
one is a const variable. That means that you can't modify its initial value.
test is a local variable inside your function, that means function won't modify the values of the variable that you pass as parameter (one).
There is an operator(++) that add a unit, I highly recommend you to use it instead of make a function:
var one = 1
one++;
console.log(one)
If you want to use a function you have to do like this:
var one = 1
function addone(test){
return test+1;
}
one = addone(one);
console.log(one)
In terms to store permanent data into a javascript variable, that's not possible. Variable are stored in RAM, that means that when you stop the program, al data of your program are wiped. Permanent store are made in database and other complex data structures.
If you want to play multiple games, you should consider to allow the players to play more games before the program finish.
thank you very much i never think that i could get more dumb so i cant manipulate it because it's just a copy of that variable that im inputing as parameter? so it means that the function did not touch the variable at all. I wish i could go back to my mother's womb.
and the const it's just a typo very sorry for this.
i did not know this website is very active i thought i would wait days just to get a response thank you very much and have a nice day

Implementing Parse.Op.Increment() error

hello I'm very new to javascript. I have only been programming iOS for a year. I am stuck on trying to increment a number in cloud code. I know I need to use Parse.Op.Increment() and amount(). but I must be doing something wrong I have gotten an error since I uploaded this line.
It DID look like this.(and worked)
var sum = Record.get("totalPunches");
Sum += request.params.punches;
Record.save("totalPunches", sum);
The line I am putting on cloud code looks like this.
Parse.Op.Increment("totalPunches", amount(request.params.punches));
any help on what I am doing wrong?
thank you

Why won't my While Loop work?

I am doing the video tutorials from The New Boston and I'm on video 20. Everything has been fine until now but I can't get this one to run. I've checked against the code in the tut and I just can't see what I'm missing. I'm sure it's right in front of my face.
Sorry this is so basic, but I'm a total newbie.
This is the video tutorial: http://www.youtube.com/watch?v=QPFW_0blw9w
This is my code:
var x = 1;
while(x<10){
document.write(x + "how do you like </br>");
x++;
}
Thanks for any help!
Your syntax is correct, as verified by running the JavaScript code (exactly as you wrote it) inside Codecademy Labs. If your while loop isn't working, I suspect it has to do with your environment or the state of the document being written, but it is not the code itself. As a fellow newbie, I try running code in two IDE's (codecademy labs being one) to test a lot of code and confirm against this sort of issue, though codecademy can't do file operations.
In addition, if you're interested in knowing why a for loop is superior to while for this situation, continue reading.
A for loop is a loop that comes in handy when you have a fixed index; When you know how many iterations you need, or know how to find out and can work that into the code.
A while loop is better at handling situations when an unknown (and unpredictable) number of iterations is required, as it will loop through until the conditions are eventually met.
Keep in mind that what makes it strong also makes it weak; for loops are harder to create infinite loops for. Because of the arguments in a for loop (IE: for (index,index<=value,value++)), most for loops self-terminate neatly, or at least it's hard to forget to put the "closing" condition in. while loops, however, don't innately ask for a closing statement and thus you must explicitly state them inside the loop.
I hope this answers your question, and more.
EDIT: Oh yeah, and </br> doesn't work inside the quotes. You can remove it, and it should still do a new line without it; But leaving </br> inside the code just has it literally print </br> in the line.
I don't see anything wrong with your code the way you wrote it, so there must be an error in some of the code that surrounds this. Alternatively, for the same outcome as this, I would suggest using a for loop as such:
for (var x = 1; x < 10; x++){
document.write(x + "how do you like </br>");
}

Javascript declaration issue

//PROBLEM SOLVED//
It turned out that the JQuery Ajax call couldn't reach the URL in certain browsers.
Thanks anyway guys, for your quick responses, definitly helped to work it out.
Sorry for the non-specific title, I don't even think what should be the problem.
There is a JQuery plugin (http://keith-wood.name/countdown.html) which counts down from a specific date or time.
The end time from which the counter should start can be defined in 2 ways: either setting a date either setting the number of seconds left.
My project needs the second one and based on the documentation this option has to be declared like:
$('#digital_hour').countdown({until: +300});
Notice the "+" sign before the number.
It works nice on any OS and device, UNTIL I replace the number 300 with a variable that stores the seconds left until the end of the day on the server. So this version:
$('#digital_hour').countdown({until: +seconds_left_on_server});
works on specific browsers, but on others don't. Strangly enought it works under my Vista/Mozilla20.0 combo, but it doesn't on my Vista/IE6, nor on my friends Ubuntu/Mozilla combo.
I'm not a huge javascript admirer, nor an expert on the subject, but I feel that there is something around the "+" sign.
Can anyone help?
You can try with
$('#digital_hour').countdown({until: new Date(+(new Date()) + 1000 * seconds_left_on_server)});
Have you tried something simple like var seconds_left = 300 and then $('#digital_hour').countdown({until: +seconds_left}); and see what happens?
It sounds like your variable is not storing what it should. The "+" shouldn't be a problem.

How to make a table out of the value of my counter?

I'm making a whack a mole game and I want to make a local high-score table for it. Currently I have a counter that counts the number of successful clicks on the moles. After each session(when the window is closed/stop button is clicked) I would like the value of the counter to get saved in a high score table. It needs to be sorted by score of course. Ex:When the top score is 10 and someone gets 11 the 10 goes to number 2 and the 11 takes 1st. The table only needs to be on the local machine, input for names would be great but not needed. I need an example of this, I've tried fooling around with HTML5 databases to no avail. My counter div is
<div id='counter' data-counter='0'>0</div>
and an example of how the counter goes up one on click of a mole
if ($(this).data("state") == "up") {
var counter = $("#counter");
counter.data("counter", counter.data("counter") + 1);
counter.text(counter.data("counter"));
}
I'm just starting web programming so I need it to be as noob friendly as possible. :P If you need clarification on the question, just comment.
Thanks!
If you want to store in the local machine, you could use cookies. You can save any string with cookies.
An easy way to use cookies with jquery is by using the jquery cookie plugin:
http://plugins.jquery.com/project/Cookie
You could store a serialization of your score object, for example if you have it in an array you could do:
$.cookie("scores", JSON.stringify(scoresArray));
To retrieve it, just do:
scoresArray = JSON.parse($.cookie("scores"));
JSON functions will work in modern browsers. More information on JSON can be found on http://json.org
Hope this helps. Cheers

Categories

Resources