xiqe / code-train

前端算法
0 stars 0 forks source link

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

Open xiqe opened 5 years ago

xiqe commented 5 years ago

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

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

Task:

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.

reply

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

let x = ""; for(let i = 1; i <= num; i++) { x+= ${i} sheep...; } return x;

Eflorscuk commented 2 years ago

const countSheep = num => [...Array(num).keys()].map(element => ${element + 1} sheep...).join('')