Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #21468 > unrolled thread

Variable subtraction

Started byk <kalezwe@gmail.com>
First post2013-09-29 16:34 -0700
Last post2013-09-29 20:06 -0700
Articles 3 — 2 participants

Back to article view | Back to comp.lang.javascript


Contents

  Variable subtraction k <kalezwe@gmail.com> - 2013-09-29 16:34 -0700
    Re: Variable subtraction Stefan Weiss <krewecherl@gmail.com> - 2013-09-30 02:07 +0200
      Re: Variable subtraction k <kalezwe@gmail.com> - 2013-09-29 20:06 -0700

#21468 — Variable subtraction

Fromk <kalezwe@gmail.com>
Date2013-09-29 16:34 -0700
SubjectVariable subtraction
Message-ID<7c29d7c6-fd63-4a14-9172-86e51b0d65e3@googlegroups.com>
So I was trying to make a copy of the game cookie clicker because it is very simple and I cant subtract from one of my variables. Here is the code I have marked with comments where the problems are:

<!DOCTYPE html>
<html>
<head>
<script>
var clicked=98;
var tenv=100;
function ninja(){
		clicked ++;
	document.getElementById("d").innerHTML=clicked + " cookies";
};
function ten(){
if(clicked<100){
alert('Not enough clicks');
}else{
var clicked = - 100;
// line above doesn't work
setInterval(function() { clicked++; document.getElementById("d").innerHTML=clicked + " cookies"; }, 10000)
}}
function five(){
if(clicked<200){
alert('Not enough clicks');
}else{
var clicked = - 200;
// line above doesn't work
setInterval(function() { clicked++; document.getElementById("d").innerHTML=clicked + " cookies"; }, 5000)
}}
</script>
<style type="text/css">
body{background-image:url(http://3.bp.blogspot.com/_flVW4vXmccQ/S8tdQq6zZtI/AAAAAAAAAUQ/A41qGqvAxsE/s320/chocolate_chip.jpg); }
#d{width:auto; height:60px; color:white; background-color: rgba(1,1,1,0.5); font-size:55px; font-weight:bold}
#cook{width:380px; height:370px; background-image:url("cookie.png")}
ul{position:absolute; right:10px; top: 65px}
#ten{list-style:none; background-color:grey; color:white; font-size:18px; font-weight:bold; height:25px;}
</style>
<title>Cookie Clicker</title>
</head>	
<body style="cursor:wait">	
<div id="d">0</div>
<button type="button" id="cook" onclick="ninja()"></button>
<ul>
<li><button type="button" id="ten" onclick="ten()">Spend 100 clicks to get one free click every 10 seconds</button></li>
<li><button type="button" id="ten" onclick="five()">Spend 200 clicks to get one free click every 5 seconds</button></li>
<ul>

</body>	
</html>


[toc] | [next] | [standalone]


#21469

FromStefan Weiss <krewecherl@gmail.com>
Date2013-09-30 02:07 +0200
Message-ID<l2afcq$mon$1@news.albasani.net>
In reply to#21468
On 2013-09-30 01:34, k wrote:
> <script>
> var clicked=98;

This "clicked" is a global variable.

> function ten(){
> if(clicked<100){
> alert('Not enough clicks');
> }else{
> var clicked = - 100;
> // line above doesn't work

This "clicked" variable is local to the ten() function, because you used
"var". It has nothing to do with the global one. If you leave out the
"var", you'll address the global variable, as you probably intended.

By the way, this line would set "clicked" to -100, not subtract 100 from
it. To do substraction, you can use

  clicked = clicked - 100;

or the short form

  clicked -= 100;  // not "= -"

> function five(){
> if(clicked<200){
> alert('Not enough clicks');
> }else{
> var clicked = - 200;
> // line above doesn't work

Same reason as before.


- stefan

[toc] | [prev] | [next] | [standalone]


#21471

Fromk <kalezwe@gmail.com>
Date2013-09-29 20:06 -0700
Message-ID<7a083b5a-8e4f-4985-acab-8b12efbef6d3@googlegroups.com>
In reply to#21469
On Sunday, September 29, 2013 5:07:54 PM UTC-7, Stefan Weiss wrote:
> On 2013-09-30 01:34, k wrote:
> 
> > <script>
> 
> > var clicked=98;
> 
> 
> 
> This "clicked" is a global variable.
> 
> Thanks so much that worked perfectly
> 
> > function ten(){
> 
> > if(clicked<100){
> 
> > alert('Not enough clicks');
> 
> > }else{
> 
> > var clicked = - 100;
> 
> > // line above doesn't work
> 
> 
> 
> This "clicked" variable is local to the ten() function, because you used
> 
> "var". It has nothing to do with the global one. If you leave out the
> 
> "var", you'll address the global variable, as you probably intended.
> 
> 
> 
> By the way, this line would set "clicked" to -100, not subtract 100 from
> 
> it. To do substraction, you can use
> 
> 
> 
>   clicked = clicked - 100;
> 
> 
> 
> or the short form
> 
> 
> 
>   clicked -= 100;  // not "= -"
> 
> 
> 
> > function five(){
> 
> > if(clicked<200){
> 
> > alert('Not enough clicks');
> 
> > }else{
> 
> > var clicked = - 200;
> 
> > // line above doesn't work
> 
> 
> 
> Same reason as before.
> 
> 
> 
> 
> 
> - stefan

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web