Ask coding questions

← Back to all posts
why does the button say NaN when i click it?
adsarebbbad (146)

im sure i got the number values correct and everything

Commentshotnewtop
Whacko (205)

The "clicks per second" button doesn't work.

Whacko (205)

Maybe try making the 0 a string?

Coder100 (8394)

why would make 0 a string smh @JavaMaster1

Coder100 (8394)

then you would get:

0
01
011
0111

@JavaMaster1

Whacko (205)

@Coder100 I know. JavaScript isn't like HTML where it is just <element></element>, it's more like a bunch of specific complex codes.

Coder100 (8394)

@JavaMaster1 wdym? HTML isn't even a programming language

Coder100 (8394)

what I'm talking about is it's type coercion @JavaMaster1

Coder100 (8394)

It's your pesky line 35:

if(1 == 1) {
  setInterval(incrementcps(), time);
}

thats wrong, you can't call the function, you only give the function:

if(1 == 1) {
  setInterval(incrementcps, time);
}

that's still wrong because you didn't give clickPerSec, so I just removed that for you lol

fuzzyastrocat (646)

@Coder100 @adsarebbbad I wouldn't say that "that's wrong", it could be useful. Consider:

function incrementcps(amount){
    return () => { clickCount += amount }
}

This is a generator that returns a function to increment the cps by some amount. You'd then pass it to setInterval like so:

setInterval(incrementcps(<the_cps_amount>), time);

So no, it's not wrong, and you can call the function (JS lambdas are super useful for exactly this kind of thing), it's just that in this instance the way the code was written did not yield the desired behavior.

However, I feel what I put above is more consistent with the original code intention. It's always good to avoid cluttering the global namespace with unneeded variables, and this method makes the semantics more clear (in setInterval(incrementcps, time), how much are we incrementing the cps?).

EDIT: Realized you can do it even simpler:

function incrementcps (clickPerSec) {
  clickCount += clickPerSec;
}

// ...

setInterval(incrementcps.bind(null, <cps_amount>), time);
adsarebbbad (146)

now thats a lot of errors i don't knwo how to fix @Coder100

lightningrock (101)

its easy to get infinite clicks:
Step one: Do cntrl+shift+j
Step two: Type in clickCount = 100000000000000000000
@Coder100

lightningrock (101)

hmmm...


I think I broke it.
@Coder100

lightningrock (101)

Also btw is it possible to have a number above 1.797693134862315E+308 and not display infinity?
https://www.w3schools.com/JSREF/jsref_infinity.asp
@Coder100

Coder100 (8394)

yes, yes there is, console.log(BigInt('1' + new Array(1000).fill("0").join(""))) @lightningrock

Coder100 (8394)

hmm not tested before maybe will break who knows @lightningrock

realTronsi (175)

clickCount is NaN. Try checking for strings

adsarebbbad (146)

But clickCount isn’t NaN @realTronsi

realTronsi (175)

@adsarebbbad

function clicked(){
	clickCount += clicksPerClick;
  console.log(clickCount)
	document.getElementById("clicks").innerHTML = "Number of clicks: " + clickCount;
}

clickCount shows as NaN. Most likely what happens is you try to do subtraction or something between it and a string in an earlier script

realTronsi (175)

@adsarebbbad Your problem is here:

function incrementcps(clickPerSec) {
	clickCount += clickPerSec;
}
realTronsi (175)

@adsarebbbad Your issue is with your incrementcps function.

adsarebbbad (146)

I don’t see anything wrong with it @realTronsi