DEV Community

Discussion on: Daily Challenge #40 - Counting Sheep

Collapse
 
dak425 profile image
Donald Feury

PHP

I counted bugs because that is more reflective of what I would do while working :)

I also added a message for if you are able to go to sleep immediately

<?php

function countBugs(int $bugs): string {
  if ($bugs === 0) {
    return "sleep tight..." . PHP_EOL;
  }

  $count = 0;
  $text = ++$count . " bug..." . PHP_EOL;

  while ($count < $bugs) {
    $text .= ++$count . " bugs.." . PHP_EOL;
  }

  return $text;
}

echo "Counting three bugs..." . PHP_EOL;
echo countBugs(3);

echo "Counting no bugs..." . PHP_EOL;
echo countBugs(0);