I receive different strings as arguments, there may be several of them.
I check in the if block if there are more than 1 argument, then I need to return the sum of the lengths of the lines that were passed in the function arguments. How can i do this?
const strSum = (...args) => {
let sum = 0;
if (args.length > 1) {
args.forEach((item) => {
});
}
return sum;
};
console.log(strSum('hello', 'hi', 'my name', 'is')); //16
>Solution :
You can add item.length to the sum. item.length will be equals to the length of the string.
const strSum = (...args) => {
let sum = 0;
if (args.length > 1) {
args.forEach((item) => {
sum += item.length
});
}
return sum;
};
console.log(strSum('hello', 'hi', 'my name', 'is')); //16
console.log(strSum()); //0