Website Talk > Web Programming
[Beginner] Programming Challenges I
(1/1)
nukpana:
Cool idea I saw at Ubuntuforums.org where beginner programmers are challenged with some situations.
So, who wants to try the first one?
--- Quote ---The Challenge:
Write a program, in any free language, that prints to the terminal the lyrics to this song, 99 Bottles of Beer.
http://99-bottles-of-beer.net/lyrics.html
If you copy anything from that site, it will be obvious, so don't try it, although you may find that site useful for seeing code of many different languages.
--- End quote ---
Obviously I would expect to see PHP and JS code from here...
nukpana:
Here is my (un-whatever) try:
--- Code: ---<?php
function descCount($num) {
for ($i = $num; $i > 0; $i--) {
$array[] = $i;
}
return $array;
}
$num = descCount(99);
array_push($num, 'no more');
foreach($num as $n) :
// Decending number
$d = $n != 0
? $n - 1
: 99;
// Last descender
$d = $d == 0
? 'no more'
: $d;
// Plural Code
$npl = $n == 1
? ''
: 's';
$dpl = $d == 1
? ''
: 's';
$secndStmnt = $n == 0
? 'Go to the store and buy some more'
: 'Take one down and pass it around';
?>
<?php echo ucfirst($n); ?> bottle<?php echo $npl; ?> of beer on the wall, <?php echo $n; ?> bottle<?php echo $npl; ?> of beer.
<?php echo $secndStmnt; ?>, <?php echo $d; ?> bottle<?php echo $dpl; ?> of beer on the wall.
<br />
<?php endforeach; ?>
--- End code ---
Navigation
[0] Message Index
Go to full version