DEV Community

loading...

Discussion on: Daily Challenge #40 - Counting Sheep

Collapse
itsdarrylnorris profile image
Darryl Norris • Edited

JavaScript

/**
 * Daily Challenge #40 - Counting Sheep
 *
 * @param  {number}        Number of sheeps.
 * @return {string}
 */
const countingSheep = number => {
  // Checking if positive integer or not.
  if (!(number >>> 0 === parseFloat(number))) {
    return `${number} is not a positive integer`;
  }

  let text = '';

  // Building text.
  for (let i = 1; i <= number; i++) {
    text += `${i} sheep...`;
  }
  return text;
};

console.log(countingSheep(3));
// Output: 1 sheep...2 sheep...3 sheep...