create account

RE: Introducing the Coding Challenge by pilcrow

View this thread on: hive.blogpeakd.comecency.com

Viewing a response to: @reggaemuffin/introducing-the-coding-challenge

· @pilcrow · (edited)
$1.32
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 ;)
👍  , , ,
properties (23)
authorpilcrow
permlinkre-reggaemuffin-introducing-the-coding-challenge-20170818t085726321z
categorycoding-challenge
json_metadata{"tags":["coding-challenge"],"links":["https://jsfiddle.net/jf68mfj1/2/"],"app":"steemit/0.1"}
created2017-08-18 08:57:27
last_update2017-08-18 08:58:15
depth1
children5
last_payout2017-08-25 08:57:27
cashout_time1969-12-31 23:59:59
total_payout_value0.991 HBD
curator_payout_value0.326 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length1,635
author_reputation2,531,070,549,481
root_title"Introducing the Coding Challenge"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id12,157,695
net_rshares377,412,334,263
author_curate_reward""
vote details (4)
@benniebanana ·
$0.10
This is really neat. Well done
👍  
properties (23)
authorbenniebanana
permlinkre-pilcrow-re-reggaemuffin-introducing-the-coding-challenge-20170819t122006588z
categorycoding-challenge
json_metadata{"tags":["coding-challenge"],"app":"steemit/0.1"}
created2017-08-19 12:20:06
last_update2017-08-19 12:20:06
depth2
children0
last_payout2017-08-26 12:20:06
cashout_time1969-12-31 23:59:59
total_payout_value0.074 HBD
curator_payout_value0.024 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length30
author_reputation875,530,017,816
root_title"Introducing the Coding Challenge"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id12,258,140
net_rshares27,329,218,811
author_curate_reward""
vote details (1)
@reggaemuffin ·
+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 :)
properties (22)
authorreggaemuffin
permlinkre-pilcrow-re-reggaemuffin-introducing-the-coding-challenge-20170818t102036456z
categorycoding-challenge
json_metadata{"tags":["coding-challenge"],"app":"steemit/0.1"}
created2017-08-18 10:20:39
last_update2017-08-18 10:20:39
depth2
children2
last_payout2017-08-25 10:20:39
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length564
author_reputation37,964,839,695,531
root_title"Introducing the Coding Challenge"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id12,162,209
net_rshares0
@pilcrow ·
Thanks! In that case I'll wait for the next one. This is fun :)
properties (22)
authorpilcrow
permlinkre-reggaemuffin-2017818t124833597z
categorycoding-challenge
json_metadata{"tags":"coding-challenge","app":"esteem/1.4.6","format":"markdown+html","community":"esteem"}
created2017-08-18 10:48:36
last_update2017-08-18 10:48:36
depth3
children0
last_payout2017-08-25 10:48:36
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length63
author_reputation2,531,070,549,481
root_title"Introducing the Coding Challenge"
beneficiaries
0.
accountesteemapp
weight500
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id12,163,717
net_rshares0
@zuricher ·
 
properties (22)
authorzuricher
permlinkre-re-pilcrow-re-reggaemuffin-introducing-the-coding-challenge-20170818t102036456z-20200829t032329z
categorycoding-challenge
json_metadata"{"app": "beem/0.23.9"}"
created2020-08-29 03:23:30
last_update2020-08-29 03:23:30
depth3
children0
last_payout2020-09-05 03:23:30
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length1
author_reputation-39,571,640,904
root_title"Introducing the Coding Challenge"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id99,332,904
net_rshares0
@tipu ·
<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>
properties (22)
authortipu
permlinkre-re-reggaemuffin-introducing-the-coding-challenge-20170818t085726321z-20170818t102050
categorycoding-challenge
json_metadata""
created2017-08-18 10:20:51
last_update2017-08-18 10:20:51
depth2
children0
last_payout2017-08-25 10:20:51
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length430
author_reputation55,914,851,917,880
root_title"Introducing the Coding Challenge"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id12,162,224
net_rshares0