Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I want the global variable exp, when it's 100, to increase the level. I'm using a button to take exp.
It works fine. It gives exp and one other variable. But the level doesn't increment by 1 when exp is 100.
exp = 0;
level = 1;
250 lines of code later, I have this:
if (exp == 100) {
level = level + 1;
exp = 0;
document.getElementById("level").innerHTML = level;
document.getElementById("exp").innerHTML = exp;
}
It doesn't work. After 100, it keeps counting.
What am I doing wrong?
It's difficult to say what's wrong because you haven't included enough code here.
However, it appears that there are a couple of things that could be wrong:
Where have you defined exp and level? You haven't used var to define them above, which means that you have likely defined them somewhere else and are re-defining here, which would reset the count each time it increments, defeating the purpose of the incrementation.
What causes the incrementation to occur? Have you wrapped your if statement in the function you're calling to increment exp? If not, you may only be checking it the first time, which would cause it to never trigger the statement.
I've created a demo here that fills in some of these holes and it works fine in isolation. The question, of course, is how that piece fits within the context of your code. The solution to that requires a bit more information about how you have your project set up.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
A website I am making has a startup function that includes a call to another function to change add to or change a global variable and update an element in the HTML document to the value.
function moneyUpdate (x,z){
var k;
if (z == 1){
k = money + x;
money = k;
document.getElementById("moneyPrint").innerHTML ="$"+ k;
}
if (z == 0){
money = x;
document.getElementById("moneyPrint").innerHTML="$"+ money;
}
I call the function line this
//other code
moneyUpdate(1000,0);
and declare the variable like this
var money=0
I'm new to java script so bare withe my probable incompetence.
It prints'x' fine put it doesn't set the value of 'money' to 1000 and calling it up with y=1 doesn't add to 'money'.
I have no clue what to even try.
In your other code, try;
money = moneyUpdate(1000,0);
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I'm getting this error when using sample code from the up and going book in the ydkjs series. Can anyone help me out? The error is
Uncaught SyntaxError: Identifier 'ACCESSORY_PRICE' has already been declared
at :1:1
const ACCESSORY_PRICE = 9.99;
var bank_balance = 302.13;
var amount = 99.99;
amount = amount * 2;
// can we afford the extra purchase?
if ( amount < bank_balance ) {
console.log( "I'll take the accessory!" );
amount = amount + ACCESSORY_PRICE;
}
// otherwise:
else {
console.log( "No, thanks." );
}
You are using an environment where executing the very same code twice (or more) doesn't allow you to redeclare already declared consts.
I can easily reproduce the code in Chrome's console, in the Sources/Snippets section. Executing the code for the first time works as expected. Executing it again raises the error as the const is already defined by the prior execution.
Assuming the Chrome's console (or similar) is your environment, one of the workarounds is to hit F5 to refresh the page under the console and rerun the script.
Another, disputably cleaner workaround is to switch to another environment where mutliple executions of the same code have no such unexpected effects.
You can't re-declare a const. This error says you already declared ACCESSORY_PRICE. Remove the second const ACCESSORY_PRICE or var ACCESSORY_PRICE or let ACCESSORY_PRICE.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have a situation where I get 4, 5 or 6 images/tiles.
Depending on the number of tiles, I need to format the images on the webpage.
Like this http://prntscr.com/9y75dw
If it's five images, I have to format it in such a way that two images in the first row and three images in the second row. Can you help me with the logic?
Well I don't see a technique, maybe I am missing to do that more appropriately or in a generic way but since the description in less and number of images given are random, I don't know how this will work.
var imageLength = $('img').length;
var newLength = 0, differenceLength=0;
if(imageLength%2==0){
//incase of even number
//Do what you like here eg: $('img').css('width', '50%');
}
else{
// incase of odd number
newLength = Math.round(imageLength/2); //dividing number into two parts.
differenceLength = imageLength - newLength; //difference to put smaller above and greater below.
$('parent-div img:nth-child(1)').nextUntil('img:nth-child('+differenceLength+')').wrapAll('<div></div>') //wraps into a container div
}
Although this is just one way. You might have already realized a lot of logic by now.
PS: I have randomly written this code so take it as a logic for help. Not sure whether this will work.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I don't know how to make a Javascript count up that is related to the real time, which means when you reload the page, the counter won't start over again. Would anybody tell me how to make that happen :) Example like http://www.worldometers.info/ Thanks a lot.
The code they are using is likely pulling from a database with an actual value increasing live.
Check out this js fiddle I made for an example of how a simple timer can work. Notice that if you press "Run" multiple times, the time itself will stay constant.
Using a remote database will cause a lot more work, but for saving values across browser refreshes you should learn about localStorage I'd check out W3 School's article on the subject.
In my implementation I use
localStorage.setItem("time", currentTime); // Note that CurrentTime must be a string!
in each iteration of your code after setting the currentTime var.
When you start up your application, a simple if statement
if (localStorage.getItem("time") {
CurrentTime = localStorage.getItem("time");
} else {
// set default values
}
will work, as localStorage.getItem will return null if the value doesn't exist (or if you set the value to null manually).
(for localStorage, you can also use [bracket notation] and will probably see that in most common examples)
localStorage["foo"] = "value";
// is the same as:
localStorage.setItem("foo", "value")
// and
var bar = localStorage["foo"];
// is the same as:
var bar = localStorage.getItem("foo");
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
I'm having trouble adding to a var at the moment I can only get it to set, I tried a number of operators like "+=" "++" but it doesn't work :(
I have a function that gets called a number of times inside I have this
score = Math.floor((Math.random()*15)*1);
But when it gets activated the function runs and sets the score then when its run again it overwrites the old score with the new random number, I want to add the old score plus whatever is generated together do I need a 2nd var?
When I tried to use score += Math.floor((Math.random()*15)*1); I got the following error:
Uncaught ReferenceError: score is not defined
Let me take a guess, score was not declared to be an Int but a string
try
var score = 0;
score += Math.floor((Math.random()*15)*1);
The issue is that score += Math.floor((Math.random()*15)*1); really means score = score + Math.floor((Math.random()*15)*1);, but, since you haven't defined score anywhere yet, when it tries to use it in the calculation, it is "undefined" (and tells you as much :D ).
Define score before attempting to use the += operator on it and you should be fine.