/    Sign up×
Community /Pin to ProfileBookmark

I dont understand javascript array sum

I dont understand what happend in this code.

<!DOCTYPE html>
<html>
<body>

<p>Click the button to get the sum of the numbers in the array.</p>
<button onclick=”myFunction()”>Try it</button>

<p>Sum of numbers in array: <span id=”demo”></span></p>

<script>
var numbers = [65, 44, 12, 4];

function getSum(total, num) {
return total + num;
}
function myFunction(item) {
document.getElementById(“demo”).innerHTML = numbers.reduce(getSum);
}
</script>

</body>
</html>

What happend in function getSum and what in myFunction? Source is w3schools

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@rootJan 09.2018 — My suggestion is to read up what the function method reduce() does...

In simple terms, you use the .reduce() on an array and its parameter is a function that is used against the array of element

You could look at it like this, it [B]reduces[/B] the array length by 1 each iteration until it is zero length and quits the function call.

Each time the array is encountered, the array[0] and array[1] are added together (if it exists) and the result is stored in the 1st array element for the next iteration.

Eventually the function completes and out pops your answer.
Copy linkTweet thisAlerts:
@rootJan 09.2018 — Also, when posting code, please use the forum BB Code tags, see my signature for a link and for examples of types.
Copy linkTweet thisAlerts:
@Ashlesha_mishraJan 12.2018 — hi,

reduce() is used for execute each and every value of array from left-to-right.

For sum of array elements, you have to call getSum() until last value of array that is why reduce() is used.

Thanks!
Copy linkTweet thisAlerts:
@Ashlesha_mishraJan 12.2018 — hi,

reduce() is used for execute each and every value of array from left-to-right.

For sum of array elements, you have to call getSum() until last value of array that is why reduce() is used.

You can use it like
[code=html]
var sum = numbers.reduce( function(total, num){
return total + num;
});
[/code]

Thanks!
Copy linkTweet thisAlerts:
@Ashlesha_mishraJan 12.2018 — hi,

reduce() is used for execute each and every value of array from left-to-right.

For sum of array elements, you have to call getSum() until last value of array that is why reduce() is used.

You can use it like
[code=html]
var sum = numbers.reduce( function(total, num){
return total + num;
});
[/code]

Thanks!
×

Success!

Help @Duritop 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.28,
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,
)...