Viewing a response to: @reggaemuffin/introducing-the-coding-challenge
PHP Version: (lazy and dirty) https://pastebin.com/q0GqCaG2 ``` <?php function fizzbuzz($n) { if (!($n % 3)) { $fizzbuzz .= 'Fizz'; } if (!($n % 5)) { $fizzbuzz .= 'Buzz'; } return $fizzbuzz; } for ($n = 0; $n <= 100; $n++) { echo $n . ': '; echo fizzbuzz($n); echo "\n"; } function fizzbuzzTest() { echo (fizzbuzz(0) == 'FizzBuzz' ? 'passed' : 'failed') . "\n"; echo (fizzbuzz(3) == 'Fizz' ? 'passed' : 'failed') . "\n"; echo (fizzbuzz(6) == 'Fizz' ? 'passed' : 'failed') . "\n"; echo (fizzbuzz(5) == 'Buzz' ? 'passed' : 'failed') . "\n"; echo (fizzbuzz(10) == 'Buzz' ? 'passed' : 'failed') . "\n"; echo (fizzbuzz(15) == 'FizzBuzz' ? 'passed' : 'failed') . "\n"; echo (fizzbuzz(30) == 'FizzBuzz' ? 'passed' : 'failed') . "\n"; echo (fizzbuzz(31) == '' ? 'passed' : 'failed') . "\n"; } fizzbuzzTest(); ``` # I think most people will do 3 checks: ``` if %3 AND %5 ... else if %3 ... else if %5 ... ``` But ``` if %3 ... if %5 ... ``` will have the same result when concatenating the strings. You don't even have to handle n = 0 as a special case because strictly speaking 0 is a multiple of 3 and 5 because 3 * 0 is 0 and 5 * 0 is 0. Otherwise there's just another `if ($n)` missing.
author | mkt |
---|---|
permlink | re-reggaemuffin-introducing-the-coding-challenge-20170817t154821852z |
category | coding-challenge |
json_metadata | {"tags":["coding-challenge"],"links":["https://pastebin.com/q0GqCaG2"],"app":"steemit/0.1"} |
created | 2017-08-17 15:48:09 |
last_update | 2017-08-17 15:53:57 |
depth | 1 |
children | 3 |
last_payout | 2017-08-24 15:48:09 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.353 HBD |
curator_payout_value | 0.113 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 1,234 |
author_reputation | 45,513,283,519,678 |
root_title | "Introducing the Coding Challenge" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 12,096,667 |
net_rshares | 134,106,493,332 |
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
reggaemuffin | 0 | 105,617,815,737 | 1% | ||
benniebanana | 0 | 7,135,704,758 | 100% | ||
pilcrow | 0 | 21,352,972,837 | 50.51% |
I think you made a few errors in it ;) You only print the number if it is no fizz and no buzz :) the implicit integer to bool conversion is scary `!($n % 5)` I probably would ask you to switch it if it is code i have to maintain, `($n % 5 === 0)` is a bit more readable in my opinion. But you said it was quick and dirty so I won't complain :) For your tests I would suggest you throw an exception if they fail, as no one will read test logs :)
author | reggaemuffin |
---|---|
permlink | re-mkt-re-reggaemuffin-introducing-the-coding-challenge-20170817t171542809z |
category | coding-challenge |
json_metadata | {"tags":["coding-challenge"],"app":"steemit/0.1"} |
created | 2017-08-17 17:15:45 |
last_update | 2017-08-17 17:15:45 |
depth | 2 |
children | 2 |
last_payout | 2017-08-24 17:15:45 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 446 |
author_reputation | 37,964,839,695,531 |
root_title | "Introducing the Coding Challenge" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 12,103,479 |
net_rshares | 0 |
Oh I really just forgot the "in all other cases" part... :D Actually I never print the number, only in the loop but that was just to check the result. But I have to say that it is hard to write "good" code without a real use case. In my opinion a one-liner (like @benniebanana suggested) is often a perfect copy/paste solution for simple and unshakable logic. I like code that is confident enough to say "Don't f***ing touch me. I just work! Please do the same!" Concerning the if-statements I find my version more readable but that's maybe only because I am so used to do it that way. But I don't see any problems with that anyway. Also the function could return null.... maybe that's an issue. But we're in the PHP world here... :P It's like asking your 8 yo child to look after your 4 yo one while you're at work. You get prettily painted walls but the cake doesn't taste that well. :D Anyway... your competition could become very funny and educational. Keep it on!
author | mkt |
---|---|
permlink | re-reggaemuffin-re-mkt-re-reggaemuffin-introducing-the-coding-challenge-20170817t203611617z |
category | coding-challenge |
json_metadata | {"tags":["coding-challenge"],"users":["benniebanana"],"app":"steemit/0.1"} |
created | 2017-08-17 20:36:00 |
last_update | 2017-08-17 20:40:42 |
depth | 3 |
children | 1 |
last_payout | 2017-08-24 20:36:00 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 968 |
author_reputation | 45,513,283,519,678 |
root_title | "Introducing the Coding Challenge" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 12,118,156 |
net_rshares | 0 |
PHP certainly is like an 8 year old looking after a 4 year old! I'm less a fan of such confident code when it's just there without comment. If it's a one line code like the one above, I'd hope it has 2 lines of comments explaining it so you know not to mess with it.
author | ratticus |
---|---|
permlink | re-mkt-re-reggaemuffin-re-mkt-re-reggaemuffin-introducing-the-coding-challenge-20170818t072901425z |
category | coding-challenge |
json_metadata | {"tags":["coding-challenge"],"app":"steemit/0.1"} |
created | 2017-08-18 07:29:00 |
last_update | 2017-08-18 07:30:00 |
depth | 4 |
children | 0 |
last_payout | 2017-08-25 07:29:00 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.050 HBD |
curator_payout_value | 0.016 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 267 |
author_reputation | 5,793,669,654,286 |
root_title | "Introducing the Coding Challenge" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 12,153,295 |
net_rshares | 19,163,037,586 |
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
mkt | 0 | 19,163,037,586 | 100% |