Getting top marks

Sometimes you need a problem which is a bi on the easy side to boost your confidence.  Yes you don’t get as much satisfaction, but you do get a result.  Like in football a tap in goal compared to a lovely set up and build up with a shot on  target will yield the same result but you’ll feel more satisfied with this one.

Quite possibly the simplest kata on code wars is where you take a number as a parameter and square it in a function then return the result.

var square = function(x) {

                  return (x * x);

};

For the first time my result was described as best practice, this was good.  However as we all know it can be improved.  Firstly the brackets surrounding the number parameter squaring are optional, this is fair, for me I just think it looks neater.

A clever option was given as

function square(num){

                   var num = Math.pow(num, 2);

                    return num;

};

This method is good, but on closer inspection using Math.pow is a little more complicated.  I’ll stick with my way.  I was just pleased to be voted up in my solution.

Leave a Reply

Your email address will not be published. Required fields are marked *