Variables not global even though being globaly defined [closed] - javascript

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);

Related

Javascript Couldn't Pickup InnerHTML [closed]

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 months ago.
Improve this question
Here is my page element:
Then I tried to pick up the value demo#.... from the element class name user-name using these code:
var x = document.getElementsByClassName("user-name")[0].innerHTML;
console.log("x is: " + x);
It would return me:
x is:
Any idea how to fix this?
Looking at your approach, the code innerHTML should have worked, there might be some other issue which might be occurring.
Like 1) there might be other tag with same class name, 2) the username value might not be updated when you called the function.

arithmetic operation using JavaScript [closed]

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 3 years ago.
Improve this question
i am trying to perform addition in javascript. below is my code :
function myfunc(size){
var m = parseInt(size)+5;
alert(m );
When i use 'alert',it works perfectly.. but i want to use sweet alert and it is not display anything via sweet alert. when i do:
function myfunc(size){
sweetAlert(size );
it does display the size using the code above. the problem is, it doesn't work when i try to perform the addition. why is it so?
try something like this
import swal from "sweetalert";
function myfunc(size) {
var m = parseInt(size) + 5;
swal(String(m));
}
myfunc("6");

javascript function BiggerOf(array,value) [closed]

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 have a function called BiggerOf(array,value) i need a javascript code.the function calculate how many array element Bigger than that value and print these element?
-Array=[2,3,5,7,9,11,13,15,17,19].
-value=8
Try this
var noArray = [2,3,5,7,9,11,13,15,17,19];
function BiggerOf(arrayVal, val){
for (i = 0; i < arrayVal.length; i++) {
if(arrayVal[i] > val ){
alert(arrayVal[i]);
}
}
}
BiggerOf(noArray, 8);
Is this what you want?
function BiggerOf( array, value ){
var result = 0; // a variable to store the result, it starts at 0
for ( var i in array ){ // iterates the array
if ( array[i] > value ) { // checks if the current array item is bigger then value
result++; // if so, result is incremented
}
}
return result; // returns the result
}
Note: for future questions, please provide the code of what you tried, what problem did you encounter and state your problem as clearly as you can. We are not here to do your homework or guess your problem.

JavaScript check variable globally [closed]

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.

Javascript, adding to a variable not setting [closed]

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.

Categories