Simple Javascript Problem

From Visual Basic to GNU C, this is the place to talk programming.

Moderators: SecretSquirrel, just brew it!

Simple Javascript Problem

Postposted on Thu Jan 09, 2014 5:48 pm

I have a line that looks like this inside a for loop:
Code: Select all
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?
Mothership: Thuban 1055T@3.7GHz, 12GB DDR3, M5A99X EVO, GTX470+Icy Vision Rev.2@840/3800, Vertex 2E 60GB
Supply ship: Sargas@2.8GHz, 12GB DDR3, M4A88TD-V EVO/USB3
Corsair: Macbook Air Ivy Bridge
Crayon Shin Chan
Minister of Gerbil Affairs
 
Posts: 2245
Joined: Fri Sep 06, 2002 11:14 am
Location: Malaysia

Re: Simple Javascript Problem

Postposted on Thu Jan 09, 2014 5:52 pm

Code: Select all
'difficulty-'+(i+1)


I'm not sure sure I understood the question, though?
There is a fixed amount of intelligence on the planet, and the population keeps growing :(
morphine
Gerbil Khan
Silver subscriber
 
 
Posts: 9999
Joined: Fri Dec 27, 2002 8:51 pm
Location: Portugal (that's next to Spain)

Re: Simple Javascript Problem

Postposted on Thu Jan 09, 2014 6:01 pm

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...
Mothership: Thuban 1055T@3.7GHz, 12GB DDR3, M5A99X EVO, GTX470+Icy Vision Rev.2@840/3800, Vertex 2E 60GB
Supply ship: Sargas@2.8GHz, 12GB DDR3, M4A88TD-V EVO/USB3
Corsair: Macbook Air Ivy Bridge
Crayon Shin Chan
Minister of Gerbil Affairs
 
Posts: 2245
Joined: Fri Sep 06, 2002 11:14 am
Location: Malaysia

Re: Simple Javascript Problem

Postposted on Thu Jan 09, 2014 6:06 pm

Then if you add the parenthesis as I described, it works.

I just tested this:
Code: Select all
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.
There is a fixed amount of intelligence on the planet, and the population keeps growing :(
morphine
Gerbil Khan
Silver subscriber
 
 
Posts: 9999
Joined: Fri Dec 27, 2002 8:51 pm
Location: Portugal (that's next to Spain)

Re: Simple Javascript Problem

Postposted on Thu Jan 09, 2014 6:13 pm

Ah ok, I didn't see the parenthesis. Thanks for the tip!
Mothership: Thuban 1055T@3.7GHz, 12GB DDR3, M5A99X EVO, GTX470+Icy Vision Rev.2@840/3800, Vertex 2E 60GB
Supply ship: Sargas@2.8GHz, 12GB DDR3, M4A88TD-V EVO/USB3
Corsair: Macbook Air Ivy Bridge
Crayon Shin Chan
Minister of Gerbil Affairs
 
Posts: 2245
Joined: Fri Sep 06, 2002 11:14 am
Location: Malaysia

Re: Simple Javascript Problem

Postposted on Thu Feb 27, 2014 12:52 am

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?
andrikaa
Gerbil In Training
 
Posts: 1
Joined: Thu Feb 13, 2014 12:59 am

Re: Simple Javascript Problem

Postposted on Thu Feb 27, 2014 7:08 am

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.
(this space intentionally left blank)
just brew it!
Administrator
Gold subscriber
 
 
Posts: 37737
Joined: Tue Aug 20, 2002 10:51 pm
Location: Somewhere, having a beer

Re: Simple Javascript Problem

Postposted on Thu Feb 27, 2014 8:47 am

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.
Flatland_Spider
Gerbil Elite
 
Posts: 852
Joined: Mon Sep 13, 2004 8:33 pm
Location: The 918/539

Re: Simple Javascript Problem

Postposted on Thu Feb 27, 2014 9:21 am

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.
Corsair 600T | ASUS P8P67 PRO | Intel 2500k @ 4.4Ghz | EVGA 560 TI | G.SKILL Ripjaws Series 8GB | Corsair HX650 650W
steelcity_ballin
Gerbilus Supremus
Silver subscriber
 
 
Posts: 11914
Joined: Mon May 26, 2003 5:55 am
Location: Pittsburgh PA

Re: Simple Javascript Problem

Postposted on Thu Feb 27, 2014 12:19 pm

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.
Flatland_Spider
Gerbil Elite
 
Posts: 852
Joined: Mon Sep 13, 2004 8:33 pm
Location: The 918/539

Re: Simple Javascript Problem

Postposted on Thu Feb 27, 2014 1:53 pm

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:
captaintrav
Gerbil
 
Posts: 36
Joined: Thu Dec 12, 2013 12:51 pm
Location: Saskatchewan, Canada

Re: Simple Javascript Problem

Postposted on Thu Feb 27, 2014 2:10 pm

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.
Corsair 600T | ASUS P8P67 PRO | Intel 2500k @ 4.4Ghz | EVGA 560 TI | G.SKILL Ripjaws Series 8GB | Corsair HX650 650W
steelcity_ballin
Gerbilus Supremus
Silver subscriber
 
 
Posts: 11914
Joined: Mon May 26, 2003 5:55 am
Location: Pittsburgh PA

Re: Simple Javascript Problem

Postposted on Fri Feb 28, 2014 7:08 am

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?
Glorious
Darth Gerbil
Gold subscriber
 
 
Posts: 7859
Joined: Tue Aug 27, 2002 6:35 pm

Re: Simple Javascript Problem

Postposted on Fri Feb 28, 2014 9:36 am

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.
There is a fixed amount of intelligence on the planet, and the population keeps growing :(
morphine
Gerbil Khan
Silver subscriber
 
 
Posts: 9999
Joined: Fri Dec 27, 2002 8:51 pm
Location: Portugal (that's next to Spain)

Re: Simple Javascript Problem

Postposted on Fri Feb 28, 2014 10:55 am

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?
Glorious
Darth Gerbil
Gold subscriber
 
 
Posts: 7859
Joined: Tue Aug 27, 2002 6:35 pm

Re: Simple Javascript Problem

Postposted on Fri Feb 28, 2014 12:00 pm

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.
Corsair 600T | ASUS P8P67 PRO | Intel 2500k @ 4.4Ghz | EVGA 560 TI | G.SKILL Ripjaws Series 8GB | Corsair HX650 650W
steelcity_ballin
Gerbilus Supremus
Silver subscriber
 
 
Posts: 11914
Joined: Mon May 26, 2003 5:55 am
Location: Pittsburgh PA

Re: Simple Javascript Problem

Postposted on Fri Feb 28, 2014 12:03 pm

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.
There is a fixed amount of intelligence on the planet, and the population keeps growing :(
morphine
Gerbil Khan
Silver subscriber
 
 
Posts: 9999
Joined: Fri Dec 27, 2002 8:51 pm
Location: Portugal (that's next to Spain)

Re: Simple Javascript Problem

Postposted on Fri Feb 28, 2014 1:12 pm

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:
Glorious
Darth Gerbil
Gold subscriber
 
 
Posts: 7859
Joined: Tue Aug 27, 2002 6:35 pm


Return to Developer's Den

Who is online

Users browsing this forum: No registered users and 1 guest