programming-examples/js/Math/Subtract elements from one another in array.js
2019-11-15 12:59:38 +01:00

22 lines
527 B
JavaScript

function subtraction(arr)
{
if (Object.prototype.toString.call(arr) === '[object Array]') {
var total = arr[0];
if (typeof (total) !== 'number') {
return false;
}
for (var i = 1, length = arr.length; i < length; i++)
{
if (typeof (arr[i]) === 'number')
{
total -= arr[i];
}
else
return false;
}
return total;
}
else
return false;
}
console.log(subtraction([7,3, 2,-1]));