/    Sign up×
Community /Pin to ProfileBookmark

Help with javascript tutorial

Going through a javascript tutorial and am stumped on how to make an example work. What is missing from this example?

Example: Using for each…in
The following snippet iterates over an array’s items, calculating their sum:

var sum = 0;
var arr = [5,7,9];
for each (var item in arr) {
sum += item;
}
print(sum);

//Source: [url]http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Statements:for_each…in[/url]

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@felgallOct 13.2006 — nothing missing, you just have one word too many. The word [b]each[/b] is the cause of the problem, get rid of that word and you will have an inefficient but workable code. A more efficient way to do it would be:

var sum = 0;
var arr = [5,7,9];
for(var i = arr.length-1;i>=0;i--) {
sum += arr[i];
}
print(sum);


Of course how it displays the result depends on what you have in your print() function.
Copy linkTweet thisAlerts:
@JMRKEROct 13.2006 — Fire this up:
[code=php]
<html>
<head>
<title>for each example</title>
</head>
<body>
<script type="text/javascript">
var sum = 0;
var arr = [5,7,9];
for (item=0; item<arr.length; item++) { sum += arr[item]; }
alert(sum);
</script>
</body>
</html>
[/code]

As far as I know there is not a 'for each' or 'foreach' command in javascript

and you would need to write a 'print()' function as well to make it work.

You may want to contact the tutorial people to see if they have an errata page (?)
Copy linkTweet thisAlerts:
@jwgrafflinauthorOct 13.2006 — Thanks! With that information I was able to get this to work:

<script type="text/javascript">

var sum = 0;

var arr = [5,7,9];

for(var i = arr.length-1;i>=0;i--) {

sum += arr[i];

}

document.write(sum);

</script>



Guess I need to find a more current (or accurate) tutorial, huh?
Copy linkTweet thisAlerts:
@JMRKEROct 13.2006 — With further testing, this seems to work as well:
[code=php]
<html>
<head>
<title>for each example</title>
</head>
<body>
<script type="text/javascript">
var sum = 0;
var arr = [5,7,9];
// for (item=0; item<arr.length; item++) { sum += arr[item]; }
msg = ''; for (item in arr) { sum += arr[item]; msg += item+' '; }
alert(sum+"n"+msg);
document.write(sum+"<br>"+msg);
</script>
</body>
</html>
[/code]

Interesting. I did not know you could use elements in array with (item in arr) construct.
Copy linkTweet thisAlerts:
@jwgrafflinauthorOct 13.2006 — Thanks to you both for your prompt response. Been waiting over a week for a response from a similar forum.
×

Success!

Help @jwgrafflin spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...