Implement a function that accepts an array of numbers and returns the sum of all
ID: 3881965 • Letter: I
Question
Implement a function that accepts an array of numbers and returns the sum of all the numbers in the list. Your implementation must use the Array.reduce function internally.sum([-5, 3, -10]); // returns -12 Implement a function that accepts an array of numbers and returns the sum of all the numbers in the list. Your implementation must use the Array.reduce function internally.
sum([-5, 3, -10]); // returns -12 Implement a function that accepts an array of numbers and returns the sum of all the numbers in the list. Your implementation must use the Array.reduce function internally.
sum([-5, 3, -10]); // returns -12 sum([-5, 3, -10]); // returns -12
Explanation / Answer
<html>
<head>
<script>
function sum(total, n)
{
return total + n;
}
var arrayOfNumber=[-5,3,-10] ;
function sumOfNumbers(array)
{
var total = array.reduce(sum);
alert('Input numbers are '+array +' and sum is '+total) ;
}
function test()
{
// passing array to function
sumOfNumbers(arrayOfNumber) ;
}
</script>
</head>
<body>
<button type="button"> Click Me</button>
</body>
</html>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.