7.3.12--Write a function called sumOfArrayItems, it takes an array parameter and return the sum of all the items. Check if all the array items are number types. If not give return reasonable feedback. #72
7.3.12--Write a function called sumOfArrayItems, it takes an array parameter and return the sum of all the items. Check if all the array items are number types. If not give return reasonable feedback.
function sumOfArrayItems(arr)
{
var sum=0;
var notN=0;
for(let i=0;i<arr.length;i++)
{
if(typeof(arr[i])!=typeof(1)){
notN=1;
}
var p=parseInt(arr[i]);
sum+=p;
}
if(notN==1)console.log(`type of array item is not number type`)
return parseInt(sum);
}
console.log(sumOfArrayItems([1,2,2,9]));
`
7.3.12--Write a function called sumOfArrayItems, it takes an array parameter and return the sum of all the items. Check if all the array items are number types. If not give return reasonable feedback.
output
input
console.log(sumOfArrayItems([1,2,2,"7",9]));
output