Viewing a response to: @reggaemuffin/introducing-the-coding-challenge
Alright, here's my solution: https://jsfiddle.net/jf68mfj1/2/ I went for some solutions that may not be "good coding practices" depending on who you ask, because I wanted to explore JavaScript a bit further than a basic for-loop and if-statement. Here's the main fizzBuzz function: ``` function fizzBuzz(num) { let returnValue = ''; if (num % 3 === 0) returnValue += 'Fizz'; if (num % 5 === 0) returnValue += 'Buzz'; return returnValue || String(num); } ``` Like others I concatenate `Fizz` and `Buzz` so I don't need a third if-statement. As a bonus I use JavaScript's type coercion to check if returnValue is truthy, and if it's not (an empty string `''` is falsy) I return the original number. Initially I returned it as an integer too, but returning mixed types is bad practice and in fact actually broke my tests, so I return it a as string now too. Fizzing from 0 to 100 I do with this method: ``` function fizzTo100() { const allFizzBuzzes = [...Array(100).keys()].map(fizzBuzz); output(allFizzBuzzes.join('\n')); } ``` The first line looks a bit like a hack. What I do is create an array of 100 length and then take the keys from that array. All the values are still undefined, but the keys are 0-100. The result of `keys()` is an iterable though, so I turn it back into an array using the spread operator in a new array `[...x]`. Now I have an array of 1-100 that I can map to fizzBuzz. See and test my code here: https://jsfiddle.net/jf68mfj1/2/ If anyone is interested in more details let me know and I'll write a blog post about it. Not going throught that effort if nobody cares though ;)
author | pilcrow |
---|---|
permlink | re-reggaemuffin-introducing-the-coding-challenge-20170818t085726321z |
category | coding-challenge |
json_metadata | {"tags":["coding-challenge"],"links":["https://jsfiddle.net/jf68mfj1/2/"],"app":"steemit/0.1"} |
created | 2017-08-18 08:57:27 |
last_update | 2017-08-18 08:58:15 |
depth | 1 |
children | 5 |
last_payout | 2017-08-25 08:57:27 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.991 HBD |
curator_payout_value | 0.326 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 1,635 |
author_reputation | 2,531,070,549,481 |
root_title | "Introducing the Coding Challenge" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 12,157,695 |
net_rshares | 377,412,334,263 |
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
reggaemuffin | 0 | 316,922,974,726 | 3% | ||
bulleth | 0 | 58,793,794,330 | 100% | ||
ramzialhaddadtm | 0 | 616,177,987 | 100% | ||
quarterlifehuman | 0 | 1,079,387,220 | 100% |
This is really neat. Well done
author | benniebanana |
---|---|
permlink | re-pilcrow-re-reggaemuffin-introducing-the-coding-challenge-20170819t122006588z |
category | coding-challenge |
json_metadata | {"tags":["coding-challenge"],"app":"steemit/0.1"} |
created | 2017-08-19 12:20:06 |
last_update | 2017-08-19 12:20:06 |
depth | 2 |
children | 0 |
last_payout | 2017-08-26 12:20:06 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.074 HBD |
curator_payout_value | 0.024 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 30 |
author_reputation | 875,530,017,816 |
root_title | "Introducing the Coding Challenge" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 12,258,140 |
net_rshares | 27,329,218,811 |
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
pilcrow | 0 | 27,329,218,811 | 50.51% |
+1 for > Initially I returned it as an integer too, but returning mixed types is bad practice and in fact **actually broke my tests**, so I return it a as string now too. and for having a test that breaks when this is not the case, tip! worthy :) The `||` is a pretty clean way, I like that! Your explanation on why you used the spread operator is glorious! Thank you for giving insight into why you used that :) I would definitely read a blog post about it. But if you want to save your efforts for a harder challenge, a new one will come up pretty soon :)
author | reggaemuffin |
---|---|
permlink | re-pilcrow-re-reggaemuffin-introducing-the-coding-challenge-20170818t102036456z |
category | coding-challenge |
json_metadata | {"tags":["coding-challenge"],"app":"steemit/0.1"} |
created | 2017-08-18 10:20:39 |
last_update | 2017-08-18 10:20:39 |
depth | 2 |
children | 2 |
last_payout | 2017-08-25 10:20:39 |
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 | 564 |
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,162,209 |
net_rshares | 0 |
Thanks! In that case I'll wait for the next one. This is fun :)
author | pilcrow | ||||||
---|---|---|---|---|---|---|---|
permlink | re-reggaemuffin-2017818t124833597z | ||||||
category | coding-challenge | ||||||
json_metadata | {"tags":"coding-challenge","app":"esteem/1.4.6","format":"markdown+html","community":"esteem"} | ||||||
created | 2017-08-18 10:48:36 | ||||||
last_update | 2017-08-18 10:48:36 | ||||||
depth | 3 | ||||||
children | 0 | ||||||
last_payout | 2017-08-25 10:48:36 | ||||||
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 | 63 | ||||||
author_reputation | 2,531,070,549,481 | ||||||
root_title | "Introducing the Coding Challenge" | ||||||
beneficiaries |
| ||||||
max_accepted_payout | 1,000,000.000 HBD | ||||||
percent_hbd | 10,000 | ||||||
post_id | 12,163,717 | ||||||
net_rshares | 0 |
author | zuricher |
---|---|
permlink | re-re-pilcrow-re-reggaemuffin-introducing-the-coding-challenge-20170818t102036456z-20200829t032329z |
category | coding-challenge |
json_metadata | "{"app": "beem/0.23.9"}" |
created | 2020-08-29 03:23:30 |
last_update | 2020-08-29 03:23:30 |
depth | 3 |
children | 0 |
last_payout | 2020-09-05 03:23:30 |
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 | 1 |
author_reputation | -39,571,640,904 |
root_title | "Introducing the Coding Challenge" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 99,332,904 |
net_rshares | 0 |
<table><tr><td>http://i.imgur.com/IFF4CSc.jpg</td><td><p><strong>Hi @pilcrow! You have just received a 0.5 SBD tip from @reggaemuffin!</strong></p><hr><p><a href="https://steemit.com/steemit/@tipu/tipu-quick-guide" rel="noopener">@tipU quick guide</a> | How to check your <a href="https://steemit.com/steemit/@tipu/cool-steem-tools-check-your-pending-payouts-with-charts" rel="noopener">pending payouts.</a></p></td></tr></table>
author | tipu |
---|---|
permlink | re-re-reggaemuffin-introducing-the-coding-challenge-20170818t085726321z-20170818t102050 |
category | coding-challenge |
json_metadata | "" |
created | 2017-08-18 10:20:51 |
last_update | 2017-08-18 10:20:51 |
depth | 2 |
children | 0 |
last_payout | 2017-08-25 10:20:51 |
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 | 430 |
author_reputation | 55,914,851,917,880 |
root_title | "Introducing the Coding Challenge" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 12,162,224 |
net_rshares | 0 |