swati-jagtap / javascript-practice

0 stars 0 forks source link

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

Open swati-jagtap opened 3 years ago

swati-jagtap commented 3 years ago

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]));

`

output

image

input

console.log(sumOfArrayItems([1,2,2,"7",9]));

output

image