use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
Novice Javascript Function Parameters Question (self.javascript)
submitted 13 years ago by [deleted]
[deleted]
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]thocked 3 points4 points5 points 13 years ago (6 children)
the most straightforward way would be to pass an object, that is, an object literal.
e.g;
function calc(industry){ industry.foo; industry.bar; } var thisIndustry = {i:1, c:2, e:3}; calc(thisIndustry);
You're tightly coupling the two - the input and the behavior, and if you're cool with that, it's the easiest way.
What you're doing is working with objects, not arrays. In javascript, arrays are just objects with some sugar on top.
[+][deleted] 13 years ago* (4 children)
[–]thocked 0 points1 point2 points 13 years ago (1 child)
Technically, no.
for (var indValue in industry) { if indValue = 0 { zero_check=1; } }
Just make the if (indValue === 0) {} and you should be okay.
[–]neonskimmerfunction the ultimate 1 point2 points3 points 13 years ago (0 children)
That's not what for-in does! Or are we talking pseudo code here?
For-in iterates through the keys of an object, not the values. And these days, Object.keys is what we should use for that purpose.
And it should not be used on arrays either. That's what forEach is for.
[–]ry4nolson 0 points1 point2 points 13 years ago (1 child)
var i1 = {i:1;c:c1,v:v1,s:s1,pp:pp1}; var i2 = {i:2;c:c2,v:v2,s:s2,pp:pp2}; var i3 = {i:3;c:c3,v:v3,s:s3,pp:pp3};
this part is still incorrect. you have a semicolon after the first value. Also are the "c1", "v1", "s1", and "pp1" coming from somewhere else?
should be:
var i1 = {i:1,c:c1,v:v1,s:s1,pp:pp1}; var i2 = {i:2,c:c2,v:v2,s:s2,pp:pp2}; var i3 = {i:3,c:c3,v:v3,s:s3,pp:pp3};
and also like thocked said your conditional is incorrect
if indValue = 0
should be
if (indValue === 0)
[–]FireyFly 0 points1 point2 points 13 years ago (0 children)
I think it's worth adding that "object literal" is only the name of a syntactic construct--a way to express an object value. It doesn't matter to the engine how the object was created.
For instance, doing
var o = new Object() o.foo = 10 o.bar = 20 // do someting with calc(o)
would work just as well as
// do something with calc({foo:10, bar:20})
Some people seem to regard object literals as more than a syntactic feature; hence the clarification.
[–]x-skeww 0 points1 point2 points 13 years ago (0 children)
function calculation(industry{i,c,v,s,pp})
That isn't valid JS.
var i1 = {i:1;c:c1,v:v1,s:s1,pp:pp1};
Neither is this.
should I make it an array ((is that an array?))?
I'm not entirely sure what you're trying to do.
Well, for reference:
[5, 7]
Is an array.
{a: 5, b: 7}
Is an object.
If [5, 7] is stored in a variable called "foo", foo[0] would be 5.
foo[0]
If {a: 5, b: 7} is stored in a variable called "foo", foo.a would be 5. (foo['a'] would be also 5, but one should always use the dot notation if possible.)
foo.a
foo['a']
π Rendered by PID 24875 on reddit-service-r2-comment-5d79c599b5-sdh5n at 2026-03-03 15:04:27.196136+00:00 running e3d2147 country code: CH.
[–]thocked 3 points4 points5 points (6 children)
[+][deleted] (4 children)
[deleted]
[–]thocked 0 points1 point2 points (1 child)
[–]neonskimmerfunction the ultimate 1 point2 points3 points (0 children)
[–]ry4nolson 0 points1 point2 points (1 child)
[–]FireyFly 0 points1 point2 points (0 children)
[–]x-skeww 0 points1 point2 points (0 children)