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


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

Variable issues and storing cookies

Started byk <kalezwe@gmail.com>
First post2013-09-30 20:04 -0700
Last post2013-10-03 16:17 +0200
Articles 8 — 6 participants

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


Contents

  Variable issues and storing cookies k <kalezwe@gmail.com> - 2013-09-30 20:04 -0700
    Re: Variable issues and storing cookies Stefan Weiss <krewecherl@gmail.com> - 2013-10-01 07:01 +0200
    Re: Variable issues and storing cookies JJ <duh@nah.meh> - 2013-10-01 17:40 +0700
    Re: Variable issues and storing cookies Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-10-01 14:04 +0200
      Re: Variable issues and storing cookies John Harris <niam@jghnorth.org.uk.invalid> - 2013-10-02 14:55 +0100
        Re: Variable issues and storing cookies Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-10-02 16:09 +0200
          Re: Variable issues and storing cookies John Harris <niam@jghnorth.org.uk.invalid> - 2013-10-03 15:00 +0100
            Re: Variable issues and storing cookies "ViLco" <villiber@tin.it> - 2013-10-03 16:17 +0200

#21479 — Variable issues and storing cookies

Fromk <kalezwe@gmail.com>
Date2013-09-30 20:04 -0700
SubjectVariable issues and storing cookies
Message-ID<e58dd859-2b9b-4a10-9e82-069004635056@googlegroups.com>
I was wondering, for my cookie clicker program, if it would be easier to use HTML Session storage or store cookies. Also I have some idea how to do both but I need some help storing multiple variables in cookies and a template for how that would be done. I would also need a template for Session storage number variable storage. The main reason I made this post is the fact that I have an error in my code that has to do with variables and getting elements by Ids. The area with the problem is marked below with PROBLEM HERE. Thankyou for helping.

<!DOCTYPE html>
<html>
<head>
<script>
// vars
var clicked=0;
var priceten = 15;
var manyten = 0;
var pricefive = 100;
var manyten = 0;
//clicked function
function ninja(){
		clicked ++;
	document.getElementById("d").innerHTML=clicked + " cookies";
};
// cursor function
function ten(){
if(clicked<priceten){
alert('Not enough clicks');
}else{
 clicked = clicked - priceten;
 priceten + 2;
 manyten++;
document.getElementById("cursorb").innerHTML="Cursor Cost: " + priceten + " Quantity Owned:" + manyten;
document.getElementById("d").innerHTML=clicked + " cookies";
setInterval(function() { clicked++; document.getElementById("d").innerHTML=clicked + " cookies"; }, 10000)
}}
// grandma function PROBLEM HERE
function five(){
if(clicked<pricefive){
alert('Not enough clicks');
}else{
 clicked = clicked - pricefive; 
 pricefive + 2;
 manyfive++;
document.getElementById("gmab").innerHTML="Cursor Cost: " + pricefive + " Quantity Owned:" + manyfive;
 document.getElementById("d").innerHTML=clicked + " cookies";
setInterval(function() { clicked++; document.getElementById("d").innerHTML=clicked + " cookies"; }, 1000)
}}
//add in when at 1,000,000,000 you win
</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}
button{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="cursorb" onclick="ten()">Cursor Cost: 15 Quantity: 0</button></li>
<li><button type="button" id="gmab" onclick="five()">Grandma Cost: 100 Quantity: 0</button></li>
<ul>

</body>	
</html>


[toc] | [next] | [standalone]


#21480

FromStefan Weiss <krewecherl@gmail.com>
Date2013-10-01 07:01 +0200
Message-ID<l2dkv9$dp6$1@news.albasani.net>
In reply to#21479
On 2013-10-01 05:04, k wrote:
> I was wondering, for my cookie clicker program, if it would be
> easier to use HTML Session storage or store cookies.

There's a difference between cookies (the kind you eat) and cookies (the
kind you store in your browser). Currently, your game only employs the
former kind. When you're ready for persistance (= you have some relevant
code), we can talk about cookies vs session storage.

> The main reason I made this post is the fact that I have an error in
> my code that has to do with variables and getting elements by Ids.
> The area with the problem is marked below with PROBLEM HERE.

This really doesn't have anything to do with browser cookies or session
storage. I don't have time to test and debug all of your code, but this
part looks dodgy:

> // grandma function PROBLEM HERE
> function five(){
[...]
>  pricefive + 2;
>  manyfive++;

The value of "pricefive" doesn't change in this statement.
Did you mean "pricefive += 2"?
Also, "manyfive" is not declared or initialized anywhere.

- stefan

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


#21483

FromJJ <duh@nah.meh>
Date2013-10-01 17:40 +0700
Message-ID<1djsh4yygry1k.scve7ge0nbpk$.dlg@40tude.net>
In reply to#21479
On Mon, 30 Sep 2013 20:04:48 -0700 (PDT), k wrote:
> // vars
> var clicked=0;
> var priceten = 15;
> var manyten = 0;
> var pricefive = 100;
> var manyten = 0;

"manyten" is declared twice.

>  priceten + 2;

See Stefan Weiss' solution.

> setInterval(function() { clicked++; document.getElementById("d").innerHTML=clicked + " cookies"; }, 10000)

Don't do this unconditionally since a new interval timer will be created
each time Cursor or Grandma is bought. It's better to move setInterval out
of both functions and call in only once after the document is loaded. e.g.:

function init() {
  setInterval(ninja, 10000);
}
addEventListener("DOMContentLoaded", init, false);

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


#21484

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2013-10-01 14:04 +0200
Message-ID<197245300.YLI4vQOGbu@PointedEars.de>
In reply to#21479
k wrote:

> I was wondering, for my cookie clicker program, if it would be easier to
> use HTML Session storage or store cookies.

HTML5 Session Storage is the API that is easier to handle in raw code:

  /* store */
  window.sessionStorage["foo"] = "bar";
  window.sessionStorage["baz"] = "bla";

  /* retrieve */
  var x = window.sessionStorage["foo"];
  var y = window.sessionStorage["baz"];

vs. the conventional

  /* store */
  document.cookies = "foo=bar";
  document.cookies = "baz=bla";

  /* retrieve */
  var cookies = document.cookies;
  var x = (cookies.match(/(^|;\s*)foo=([^;]+)/) || [])[2];
  var y = (cookies.match(/(^|;\s*)baz=([^;]+)/) || [])[2];

However, because the former is part of the comparably new HTML*5* API, it is 
also the least compatible of the two.  You should make design decisions 
based on compatibility above easy coding.  The latter can always be 
achieved – and has been several times before – by an easy-to-use wrapper 
API.

> Also I have some idea how to do both but I need some help storing multiple
> variables in cookies and a template for how that would be done.

What is your idea, then?

> I would also need a template for Session storage number variable storage.

RTFM: <https://developer.mozilla.org/>

> The main reason I made this post is the fact that I have an error in my
> code that has to do with variables and getting elements by Ids.

Yes, you really should learn the basics first.  You can start by retrieving 
the reference to the same element object only one time, and storing it in 
and reusing a variable.  Also, “I have an error in my code” is not a problem 
*description*: 

<http://jibbering.com/faq/>
<http://PointedEars.de/faq>
<http://devtoolsecrets.com/>

> The area with the problem is marked below with PROBLEM HERE.

Still, your code is hardly readable.  In addition to getting a minimum clue, 
you need to get used to a consistent, easily readable code style if you want 
others to invest their time reviewing your code. 

By not using the spammed and buggy Google Groups for posting to Usenet, and 
using a newsreader like SeaMonkey or KNode instead, you will increase your 
potential audience.  Also, you should post using your real name.

> Thankyou for helping.

You are welcome.

-- 
PointedEars

Twitter: @PointedEars2
Please do not Cc: me. / Bitte keine Kopien per E-Mail.

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


#21499

FromJohn Harris <niam@jghnorth.org.uk.invalid>
Date2013-10-02 14:55 +0100
Message-ID<cd9o4990o7phoeo3q3qpm8k474klkm7qoi@4ax.com>
In reply to#21484
On Tue, 01 Oct 2013 14:04:34 +0200, Thomas 'PointedEars' Lahn
<PointedEars@web.de> wrote:


  <snip>
>Also, you should post using your real name.

Why ?

  John

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


#21501

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2013-10-02 16:09 +0200
Message-ID<2098158.DKSfp5TNBH@PointedEars.de>
In reply to#21499
John Harris wrote:

> Thomas 'PointedEars' Lahn wrote:
>> Also, you should post using your real name.
> 
> Why ?

Because “K” might be appropriate with the MIB, but not in Usenet.
Strange of you to ask that.

BTW, AFAIK in English a question mark is not preceded by white-space. 
Omitting it will avoid problems later. Did you just ask which problems
? There you are.

-- 
PointedEars

Twitter: @PointedEars2
Please do not Cc: me. / Bitte keine Kopien per E-Mail.

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


#21527

FromJohn Harris <niam@jghnorth.org.uk.invalid>
Date2013-10-03 15:00 +0100
Message-ID<n1uq491efjjfr25gblnnl6qce685cidv2n@4ax.com>
In reply to#21501
On Wed, 02 Oct 2013 16:09:53 +0200, Thomas 'PointedEars' Lahn
<PointedEars@web.de> wrote:

>John Harris wrote:
>
>> Thomas 'PointedEars' Lahn wrote:
>>> Also, you should post using your real name.
>> 
>> Why ?
>
>Because “K” might be appropriate with the MIB, but not in Usenet.
>Strange of you to ask that.

Who said it is inappropriate in Usenet, and what on earth is 'MIB'.

Stalin, John le Carre, Sting, Marilyn Monroe, Student, and Mark Twain
all used pseudonyms. Who are you to say they're wrong ?


>BTW, AFAIK in English a question mark is not preceded by white-space. 

I have books by English authors with English publishers and English
printers where every question mark and colon is preceded by an
n-space. It makes them easier to see.


>Omitting it will avoid problems later. Did you just ask which problems
>? There you are.

Arguing from the quirks of text processing programs is futile. (I
expect there's a Latin name for that.)

  John

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


#21528

From"ViLco" <villiber@tin.it>
Date2013-10-03 16:17 +0200
Message-ID<l2ju9c$qfv$1@dont-email.me>
In reply to#21527
John Harris wrote:

> Arguing from the quirks of text processing programs is futile. (I
> expect there's a Latin name for that.)

Just it: "futile", in current italian :)
-- 
"Un pasto senza vino e' come un giorno senza sole"
Anthelme Brillat Savarin 

[toc] | [prev] | [standalone]


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


csiph-web