so am doing this exercise where it asks me to return a list of strings for each counted number.
I am aware that the function will end as soon as it return a value. I tried couple of other variations such as continue and break, but i cant seem to figure it out.
Thank you in advance
var countSheep = function (num) {
for (let i = 1; i <= num; i++) {
return `${i}sheep...`;
}
}
>Solution :
As mentioned in the comments by Barmar, you can add the values to an array and return the list after the for loop completes. That would look like:
var countSheep = function (num) {
var list = [];
for (let i = 1; i <= num; i++) {
list.push(`${i}sheep...`);
}
return list
}
console.log(countSheep(3))