how to make function with undefined amount of inputs code example
Example 1: javascript x number of parameters
function display_numbers(...numbers) {
colors.forEach(function(x) {
console.log(x);
});
}
// call it like:
display_numbers(1)
display_numbers(1, 2, 3, 4)
display_numbers(1, -1, 9, 0, 2)
Example 2: javascript unknown number of parameters
function add (...numbers) {
let sum = 0;
for (let i = 0; i < numbers.length; i++) {
sum += numbers[i]
};
console.log(sum);
};
add(2, 3);
add(5, 3, 7, 2);