Sunday, 29 November 2015

Kata Day 5: Find the smallest integer in the array


Problem Description: Find the smallest integer in the array.

Given an array of integers your solution should find the smallest integer. For example:
Given [34, 15, 88, 2] your solution will return 2
Given [34, -345, -1, 100] your solution will return -345

You can assume, for the purpose of this Kata, that the supplied array will not be empty.

Level: 7kyu


This Kata was easy to solve if we know how to use sort function in Javascript. Sorting an array using a function is slightly different from the way we used sort function in Python where we can just directly get a sorted array by using a sort function against the given array.

In Javascript we have to use the sort function with a bit of additional code in order to sort an array of given integers. 

Sorting an array in Javascript is done in following manner:

arr.sort(function(a, b){return a-b});

This sorts the array in ascending order. 
To read further on this you can follow this MDN link: Sort in Javascript

Once we have got a sorted array we can return the first element of the array which will give us the smallest integer of that array.

Here is a link to the code: Kata Day 5

Please mention your suggestions or your doubts in the comment below.

Happy coding. :)  

No comments:

Post a Comment