so i want to write a function that accepts any number of numbers and then adds them , but if i enter 0 or the sum exceeds 100 it stops

1 Like

Hi, there.
would you mind try my code?

``````function getArraySum(a){
var total=0;
for(var i in a) {
total += a[i];
}
}
``````

nope , i managed to make the function that accepts any count of numbers but i can;t figure out the rest

``````function add() {
var s = 0;
for (var i=0; i < arguments.length; i++) {
s += arguments[i];
}
return s;
}
``````

what should happen if `arguments[i]` is 0, or if `s` is bigger than 100?

it stops reciieving values, like if its a prompt and he enters 0 , i want the prompt to close

you donâ€™t have a prompt tho

how do you accept values?

yeah i donâ€™t know how to do that with a prompt so thatâ€™s why it is not included in my code

I would put the values in an array and use reduce something like this.

``````var numbers = [50, 50, 25, 10];

console.log(numbers.reduce(myFunc));

function myFunc(total, num) {
if (total > 100 || total < 1) {
} else {
}
}
``````

I am sorry but i am having some trouble understanding what the â€śmyfuncâ€ť do

The code inside myFunc or the whole thing?

the inside myFunc yes

The way I wrote the function, reduce() starts with the first two values in the array as total and num. So total = 50 and num = 50 (the second 50 in the array).
the If statement will check if total (50) is greater than 100, which it is not, so the return will be skipped and that else will be executed which adds total to number. 50+50.
since we are using reduce() the total will now be 100 and the next item in the array will become num (25). The conditions will be checked until total is either greater than 100, less than 1 or all elements of the array have been added together.

Iâ€™m not sure exactly what you are doing so this solution may not work for all conditions.

1 Like

const addNumbers = (arr) => {
let total = 0;
if(arr === 0){
return null;
} else {
arr.map((i) => {