Sum of Numbers in an Array With JavaScript

How to find the sum of an array of numbers

Recommended (reduce with default value)

Array.prototype.reduce can be used to iterate through the array, adding the current element value to the sum of the previous element values.

console.log(  [1, 2, 3, 4].reduce((a, b) => a + b, 0))console.log(  [].reduce((a, b) => a + b, 0))

Simple Array Sum using javascript (single integer)

There are two problems in your code. You need to change

sum += (ar); to sum += (ar[i]);

so as to sum the element at that index and not the ar itself. Also return should be outside the loop and should actually be the return of the function. Otherwise, for..loop will just return after the first execution.

function simpleArraySum(ar) {  var sum = 0;  for (var i = 0; i < ar.length; i++) {    sum += ar[i];  }  return sum;}
console.log(simpleArraySum([1, 2, 3, 4]))

How to sum first n number of entries in an array?

When implementing something recursively, it's a good idea to think about the base condition. It does look like that's where you started, but your logic is flawed. n is the number of elements you would like to sum together. So let's say you want to sum the first n=3 elements of the array [2,3,4,5,6] and get 9. Here are the iterations.

  1. return arr[n-1] + sum(arr, n-1) // arr[2] + sum(arr, 2) === 4 + sum(arr, 2) === 4 + 5
  2. return arr[n-1] + sum(arr, n-1) // arr[1] + sum(arr, 1) === 3 + sum(arr, 2) === 3 + 2
  3. return arr[n-1] // arr[0] === 2 (base case when n is 1)

I didn't solve the problem for you since this is obviously a school exercise, but the point is that the base case and the recursive call that you have are both slightly off.

How to compute the sum and average of elements in an array?

var sum = 0;
for( var i = 0; i < elmt.length; i++ ){
sum += parseInt( elmt[i], 10 ); //don't forget to add the base
}

var avg = sum/elmt.length;

document.write( "The sum of all the elements is: " + sum + " The average is: " + avg );

Just iterate through the array, since your values are strings, they have to be converted to an integer first. And average is just the sum of values divided by the number of values.

sum values in an array based on another array

Sure, filter the services you need -> map service to it's value -> reduce the resulting list to a singe number adding the elements:

const services = [
{
Id: "1",
services: "Early Check-In",
fees: "7500"
},
{
Id: "2",
services: "Late Checkout",
fees: "7500"
},
{
Id: "3",
services: "Airport Chauffeur",
fees: "25000"
}
]
const ids = ["1" , "2"]

console.log(
services
.filter(({Id}) => ids.includes(Id))
.map(({fees}) => parseFloat(fees))
.reduce((acc, fees) => acc + fees, 0)

)

Sum values from an Array, JavaScript

You can use Array#reduce() to get the sum

var magicnumber = [];
mymagicNumber();
function mymagicNumber() { //Specify the size of array var size = parseInt(prompt("How many data values do you need have?")); for (var i = 1; i <= size; i++) { var promptValue = prompt("Enter data value number " + i); magicnumber.push(+promptValue); } var sum = magicnumber.reduce((a,b)=>{return a+b},0); //Display array element document.getElementById("demo1").innerHTML = "Your data : " + magicnumber.join(', '); document.getElementById("sum").innerHTML = "The sum : " + sum;}
<div id="demo1"></div><div id="sum"></div>


Related Topics



Leave a reply



Submit