Page 1 of 1

Simple Javascript Problem

Posted: Thu Jan 09, 2014 5:48 pm
by Crayon Shin Chan
I have a line that looks like this inside a for loop:
coinsarray[i-1].difficulty = document.getElementById('difficulty-'+i).value;

As you can imagine, the for loop starts from 1 instead of 0 precisely because I can't figure out how to temporarily +1 to i when it's inside getElementById(). How does one do that?

Re: Simple Javascript Problem

Posted: Thu Jan 09, 2014 5:52 pm
by morphine
'difficulty-'+(i+1)


I'm not sure sure I understood the question, though?

Re: Simple Javascript Problem

Posted: Thu Jan 09, 2014 6:01 pm
by Crayon Shin Chan
In getElementByID(), I can't temporarily add 1 to i to convert the index from something array friendly to something I'd actually use in the HTML code. Or can I? That's what I'm asking. I can't go document.getElementById('difficulty-'+i+1) because that would just be part of the string, instead of i being temporarily incremented...

Re: Simple Javascript Problem

Posted: Thu Jan 09, 2014 6:06 pm
by morphine
Then if you add the parenthesis as I described, it works.

I just tested this:
var i=1;
'something'+(i+1);


It returns "something2" as expected.

Also, you really shouldn't do plain Javascript anymore. Use JQuery or something similar. Plain Javascript is the work of the devil.

Re: Simple Javascript Problem

Posted: Thu Jan 09, 2014 6:13 pm
by Crayon Shin Chan
Ah ok, I didn't see the parenthesis. Thanks for the tip!

Re: Simple Javascript Problem

Posted: Thu Feb 27, 2014 12:52 am
by andrikaa
Difference between Java and Javascript? Are there any real key differences between the 2, I know Javascript has become a mainsteam thing, But should I really uptake Java if i'm gonna be learning Javascipt? Is Java a prerequist for Javascript?

Re: Simple Javascript Problem

Posted: Thu Feb 27, 2014 7:08 am
by just brew it!
Other than both having some superficial syntactic similarities to C and using similar naming conventions, they are not related. You do not need to learn one to use the other, in either direction.

Re: Simple Javascript Problem

Posted: Thu Feb 27, 2014 8:47 am
by Flatland_Spider
And you don't need to have the Java Runtime Environment (JRE) installed for Javascript to work. This comes from an actual question an actual developer asked.

Re: Simple Javascript Problem

Posted: Thu Feb 27, 2014 9:21 am
by steelcity_ballin
andrikaa wrote:
Difference between Java and Javascript? Are there any real key differences between the 2, I know Javascript has become a mainsteam thing, But should I really uptake Java if i'm gonna be learning Javascipt? Is Java a prerequist for Javascript?


They have nothing to do with each other.

Re: Simple Javascript Problem

Posted: Thu Feb 27, 2014 12:19 pm
by Flatland_Spider
morphine wrote:
Also, you really shouldn't do plain Javascript anymore. Use JQuery or something similar. Plain Javascript is the work of the devil.


It's probably not bad to know some Javascript since jQuery, Coffescript, etc. devolve into Javascript anyway.

Re: Simple Javascript Problem

Posted: Thu Feb 27, 2014 1:53 pm
by captaintrav
steelcity_ballin wrote:
andrikaa wrote:
Difference between Java and Javascript? Are there any real key differences between the 2, I know Javascript has become a mainsteam thing, But should I really uptake Java if i'm gonna be learning Javascipt? Is Java a prerequist for Javascript?


They have nothing to do with each other.


Blame Netscape for that one. The ratified standard is ECMAScript, too bad they didn't come up with a catchier name to distance them from Java. It's surprising how many somewhat technical people complain about how terrible Javascript is. It's not Java! :lol:

Re: Simple Javascript Problem

Posted: Thu Feb 27, 2014 2:10 pm
by steelcity_ballin
captaintrav wrote:
steelcity_ballin wrote:
andrikaa wrote:
Difference between Java and Javascript? Are there any real key differences between the 2, I know Javascript has become a mainsteam thing, But should I really uptake Java if i'm gonna be learning Javascipt? Is Java a prerequist for Javascript?


They have nothing to do with each other.


Blame Netscape for that one. The ratified standard is ECMAScript, too bad they didn't come up with a catchier name to distance them from Java. It's surprising how many somewhat technical people complain about how terrible Javascript is. It's not Java! :lol:


Javascript (eCMA) used to be laughed at by more skilled programmers (classic ASP, then .Net and so on from a MS Stacking frame of reference) because they didn't consider it a 'real' programming language. Prototype gave way to JQUery which has revitalized the entire community around it, and has give us other amazing libraries like Angular. Really good stuff.

Re: Simple Javascript Problem

Posted: Fri Feb 28, 2014 7:08 am
by Glorious
steelcity_ballin wrote:
Javascript (eCMA) used to be laughed at by more skilled programmers (classic ASP, then .Net and so on from a MS Stacking frame of reference) because they didn't consider it a 'real' programming language. Prototype gave way to JQUery which has revitalized the entire community around it, and has give us other amazing libraries like Angular. Really good stuff.


And then some crackhead cooked up node.js, so it's spread its tentacles into server side. :o

It has its place client-side, for lack of anything both better and universal, but I still think the idea is crazy. I understand the libuv part, but the rest of it, whyyyy?

Re: Simple Javascript Problem

Posted: Fri Feb 28, 2014 9:36 am
by morphine
Glorious wrote:
And then some crackhead cooked up node.js, so it's spread its tentacles into server side. :o

Actually, from someone who earns his food doing this sort of thing... I haven't yet begun to you use it, but everything I've seen about node.js makes it look all kinds of cool and I'll dare saying that it has a nice future.

You see, the thing is that basic Javascript is pretty terrible, but not because of a problem with the language. It's mainly a problem with the ever-shifting, unpredictable and inconsistent functionality that the browsers expose to JS. However, node.js sidesteps this - browser issues and API inconsistencies won't (I hope) be a problem on the server-side.

Re: Simple Javascript Problem

Posted: Fri Feb 28, 2014 10:55 am
by Glorious
morphine wrote:
Actually, from someone who earns his food doing this sort of thing... I haven't yet begun to you use it, but everything I've seen about node.js makes it look all kinds of cool and I'll dare saying that it has a nice future.


I don't dispute that, it does cool things and everyone seems to want to use it. But there are lot of competing things that are equally cool and have not only bright futures but bright pasts...

morphine wrote:
You see, the thing is that basic Javascript is pretty terrible, but not because of a problem with the language. It's mainly a problem with the ever-shifting, unpredictable and inconsistent functionality that the browsers expose to JS. However, node.js sidesteps this - browser issues and API inconsistencies won't (I hope) be a problem on the server-side.


Only because google did a ridiculous amount of heavy-lifting to create V8, which node.js uses. It seems to me that something must be wrong with it if it is only usable when other people have had to put massive amounts of effort making it acceptable. It's the same even with the libraries, lots of people use javascript, sure, but only really via jQuery.

My point is that, if you have to abstract/work-around virtually about the language, why choose that language? There are plenty of similar frameworks and stacks that are built around other languages, but yet you can still easily use the native language they were built on. No one seems to actually want to use javascript like that, so why, oh why, choose javascript?

Re: Simple Javascript Problem

Posted: Fri Feb 28, 2014 12:00 pm
by steelcity_ballin
And now this: http://techcrunch.com/2014/02/25/micros ... ual-studio

It was do-able before, but now it's baked in by default. Neat.

Re: Simple Javascript Problem

Posted: Fri Feb 28, 2014 12:03 pm
by morphine
Glorious, like I said, if you decouple Javascript the language from Javascript the browser environment, it's actually fairly good.

I mean, look at it from the inverse angle: if an abstraction of the browser cruft/mess makes JS perfectly fine to work in... :) But to each his own.

Re: Simple Javascript Problem

Posted: Fri Feb 28, 2014 1:12 pm
by Glorious
morphine wrote:
Glorious, like I said, if you decouple Javascript the language from Javascript the browser environment, it's actually fairly good.

I mean, look at it from the inverse angle: if an abstraction of the browser cruft/mess makes JS perfectly fine to work in... :) But to each his own.


Well I certainly use jQuery quite a bit, so it's not like I'm any sort of absolutist. :wink: