vendredi 17 avril 2020

I need to write a function "checkArray" which receives an array of random numbers and return the sum of the numbers bigger than 5

I need to write a function "checkArray" which receives an array of random numbers (created with giveMeRandom) and prints, for each item, whether it's bigger than 5. The function returns the sum of the numbers bigger than 5.

const checkArray = function(n) {
    let sum = 0;
    let newArr = [];

    for(i = 0; i < n; i++) {
        newArr.push(Math.floor(Math.random() * 10)); 
        if (newArr[i] > 5) {
            sum += newArr[i]
        }
        return sum;
    }

    return newArr;
}

console.log(checkArray(6));

Actually it does not work




Aucun commentaire:

Enregistrer un commentaire