How to approach solving a challenge during a coding interview

==> is false(i % 3) ==> type coercion — will check if value is falsy or truthyReplace i by a few numbers to better understand itif (!( 1 % 3))/*becomes if (!( 3 )) /*3 is not false or falsy so check fails*/if (!( 2 % 3))/*becomes if (!( 6 )) /*6 is not false or falsy so check fails*/if (!( 3 % 3))/*becomes if (!( 0 )) /*0 is not false but is falsy so check passes*/I can rewrite now my entire function using the logic abovefunction fizzBuzz( start = 1, end = 100) { for( let i = start; i <= end; i++) { let output = i; if( !(i % 3) ) output = ‘Fizz’; if( !(i % 5) ) output = ‘Buzz’; if( !(i % 3) && !(i % 5) ) output = ‘FizzBuzz’; console.

log(output); }}I was quite ecstatic when I got to this solution, but it did not too long, unfortunately.

The last line was still redundant to me and honestly was bugging me.

How could I combine the checks of 3 and 5 in one pass?And then it hit me, what if I could start with an empty string, attach to it the word ‘Fizz’ if it passes the 3 condition and attach the word ‘Buzz’ if it passes the 5 condition too.

I drew this on a piece of paperi = 1 ==> no Fizz ‘’ ==> no Buzz ‘’ ==> output is 1i = 3 ==> yes ‘Fizz’ ==> no Buzz ‘’ ==> output is ‘Fizz’i = 5 ==> no Fizz ‘’ ==> yes ‘Buzz’ ==> output is ‘Buzz’i = 15 => yes ‘Fizz’ ==> yes ‘Buzz’ ==> output is ‘FizzBuzzThe ternary operator will allow assigning a value if condition checks and an alternate value if it fails in a very terse manner.

Something else became obvious, we are outputting either a string or a number while we cycling through the values of i and as we saw in a previous section an empty string is a falsy value.

So how do we translate all that intelligence into working code?The essential piece to achieve that was that the value of output was either going to be one of the possible strings ‘Fizz’, ‘Buzz’, ‘FizzBuzz’ or be falsy.

In the falsy case i will just be passed as is.

So the final rewrite with more commentsfunction fizzBuzz( start = 1, end = 100) { for( let i = start; i <= end; i++) { // output is assigned a value or empty let output = ( (i % 3) ? ‘’ : ‘Fizz’ ); // output concatenates the next value output += ( (i % 5) ? ‘’ : ‘Buzz’) ; // || or operator if output is falsy will show i value console.

log(output || i); }}fizzBuzz(1,15);Hopefully, you followed all of that 🙂 This was a very satisfying solution to me since I believe it was easy to read, solved the problem and had a touch of eloquent JavaScript in it.

Final wordsThe coding exercise covers only one aspect of the many things that happen during the coding interview.

As I mentioned the steps and being able to deliver, regardless of the complexity of the problem, take a solid amount of practice.

Don’t hesitate to use mock interviews (we will be offering some in Javascript soon but more on that later) to practice the conversational aspect of it.

I hope this was useful, share and live a comment if you please :).

. More details

Leave a Reply