Javascript, adding to a variable not setting [closed] - javascript

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.

Related

Simple math coming out wrong [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
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.
Improve this question
I've made a super simple script to pop out some hourly rates from a pool of tips. Thing is, this one specific result always comes out wrong. What the heck is going on?
var tips = prompt('Enter final tips after payouts and cleaning');
//Hours worked for both positions
var tendHrsFirst = 11;
var tendHrsSecond = 10;
//Hourly Rate
var barThourly = ((tips/(tendHrsFirst+++tendHrsSecond)));
//This result here always comes out as if tendHrsFirst is 12 and not 11.
var barToneTotal = (tendHrsFirst * barThourly);
//This result is always correct
var barTtwoTotal = (tendHrsSecond * barThourly);
You are incrementing with tendHrsFirst++, so it actually is 12.
I guess those are actually two commands.
tendHrsFirst++ increments tendHrsFrist by 1. Afterwards, you add both numbers. Not sure why you think that's a good idea. Cleaning up your code should help avoiding such mistakes.
here
var barThourly = ((tips/(tendHrsFirst+++tendHrsSecond)));
you are using +++ that means postfix increase of tendHrsFirst and added to tendHrsSecond
or maybe
prefix increase of tendHrsSecond added to tendHrsFirst

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.

Very simple SD function [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 8 years ago.
Improve this question
var orangeCost = function (pricePerOrange, amountOfOranges) {
var totalCost = pricePerOrange * amountOfOranges;
console.log(totalCost + " Dollar total cost");
};
orangeCost(5, 5);
25 Dollar total cost
NaN Dollar total cost
Why does it also log the "NaN" part instead of only the 25 Dollar stuff ?
thank you
The only way a multiplication could result in NaN is if the variables were undefined, meaning you're calling the function without passing pricePerOrange or amountOfOranges, or if one of the variables is a string.
Make sure you're passing variables to orangeCost, and that those variables are in fact Numbers.

Write new line adding +1 to base value until total value = 10? [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 8 years ago.
Improve this question
I'm currently starting to learn Javascript and would be grateful for some help with the following issue:
I'm trying to make a loop that writes a new line with a base value that adds by 1 with each line until the value is equal to 10. Basically, it's supposed to look like this:
Answer: 1
Answer: 2
Answer: 3
... and so on, until it reaches 10. There' the loop should end.
How do I achieve this?
Easy solution. Do not use in anything serious:
for (x=1;x<11;x++)
{
document.write("Answer: "+x+"<br />");
}
More versatile solution with DOM (fiddle):
<p id="numbers">
</p>
<script type="text/javascript">
for (x=1;x<11;x++)
{
document.getElementById("numbers").innerHTML+="Answer: "+x+"<br />"
}
</script>
Use a for loop like so:
for(var x = 1; x <= 10; x++) //start x as equal to one, run the code, and repeat until x is 10
console.log("Answer: " + x);//Print x

jQuery .click +..+ notation [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am trying to understand a codrops tutorial. Its essentially a slider with a thumb scroller.
I reached a point in the code where they were setting variables to represent clicked items in the thumb scroller like this.
var $currentTitle = $pg_title.find('h1:nth-child('+(current+1)+')');
var $nextTitle = $pg_title.find('h1:nth-child('+(idx+1)+')');
var $currentThumb = $pg_preview.find('img.pg_thumb:eq('+current+')');
I have never seen notation like this +....+. I have been digging and found examples where people used it in stack like this but I haven't seen anyone explain it could someone explain how +...+ returns the value of the clicked item?
When used with a string operand, it concatenates the two strings. In this case, to create a selector. When used with a number, it works as an addition operator. It is used in both ways here. So, if current == 1,
h1:nth-child('+(current+1)+')' will evaluate first to h1:nth-child('+2+'), which will ultimately evaluate to h1:nth-child(2)
'+' Use for string concatenation
$pg_title.find('h1:nth-child('+(current+1)+')');
Like:
var b = 'def';
If you want to add some other string in 'b' variable then you can use '+' for concatenation
var addSomotherSting ='abc' + b + 'ghi';
alert(addSomotherSting);
Then browser show a alert box with 'addSomotherSting' the out put is : 'abcdefghi'

Categories