Skip to content

Instantly share code, notes, and snippets.

@martinjoiner
Last active December 13, 2025 17:20
Show Gist options
  • Select an option

  • Save martinjoiner/0edae187ab0c530b8b0d29fd21ce6c8a to your computer and use it in GitHub Desktop.

Select an option

Save martinjoiner/0edae187ab0c530b8b0d29fd21ce6c8a to your computer and use it in GitHub Desktop.
Super Tricky Code Challenge
<?php
/**
* Fix the code: Add, Remove or Change 1 character to make this code print 3 pound signs "£££"
* Here's the broken code that will run forever (do NOT uncomment this and run it!)...
*/
//$n = 3;
//for( $i = 0; $i < $n; $i-- ){
// print "&pound;";
//}
/**
* Solution 1: Reduce the qualifyer with each iteration, not the index.
* (I got this one after about half an hour of having a thunk.)
*/
$n = 3;
for( $i = 0; $i < $n; $n-- ){
print "&pound;";
}
/**
* Solution 2: Add a minus character before the first variable in the condition to invert it.
* (Frustratingly I only bloody thought about doing this but didn't run the simulation clearly
* enough inside my bonce to see it working. I thought about negative zero being equal to zero
* and gave up on the idea. If I'd continued I'd have realized it would become negative negative
* 1 (eg positive 1), then negative negative 2, negeative negative 3 and then stop)
*/
$n = 3;
for( $i = 0; -$i < $n; $i-- ){
print "&pound;";
}
/**
* Solution 3: This is actually quite beautiful.
* Change the qualifyer to an addition operation which, when i reaches -3, will evaluate as falsey.
* One of the coders at Softwire explained this to me and I facepalmed because again I'd thought
* of doing it but not for long enough. This whole experience has refreshed my knowledge of the for loop.
*/
$n = 3;
for( $i = 0; $i + $n; $i-- ){
print "&pound;";
}
/**
* Summary: This challenge is not designed to create good code, it's to get coders thinking laterally about
* the for-loop. Everyone I have spoken to agrees these solutions are not good and changing the i-- to i++
* would be the way to professionalise the code.
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment