logo

If you can't sleep, just count sheep!!

Given a non-negative integer, 3 for example, return a string with a murmur: "1 sheep...2 sheep...3 sheep...". Input will always be valid, i.e. no negative integers.


My code:


                        var countSheep = function (num) {
                            if (num <= 0) {
                              return '';
                            }
                            let sheep = [];
                            for (let i = 1; i <= num; i++) {
                              sheep.push(i);
                            }
                            let count = sheep.join(' sheep...') + ' sheep...';
                            return count;
                          } 
                    


And that's the best solution among all users:


                        var countSheep = function (num){
                            let str = "";
                            for(let i = 1; i <= num; i++) { str+= `${i} sheep...`; }
                            return str;
                          }