let arr = [2, 4, 1, 3, 5];
let result = 0;
for (let i = 0; i < arr.length; i++) {
result += arr[i];
}
console.log(result); // 15
const sum = [1, 2, 3].reduce((partialSum, a) => partialSum + a, 0);
console.log(sum); // 6
Run code snippet
const sum = (...args) => args.reduce((a, b) => a + b, 0);
console.log(sum(1,2,3)); // returns 6
int sumofarr(int *a, int n) {
if (n>1)
return a[n-1]+sumofarr(a, n-1);
return a[0];
}
$total = 0;
foreach ($products as $product) {
$subtotal = $product['price']*$product['quantity'];
$total += $subtotal;
}
echo $total;