previous

page 17

next
My Sort Function

JavaScript has a sort function but it can be tricky to use so I've written a better one for you. Note that mine starts with a capital S. So create a new file Sort.js and paste this code into it.

    function Sort(obj) {
      if (typeof obj == 'object') { // an array
        if (typeof obj[0] == 'number') {
          obj = obj.sort((a, b) => a - b);
        } else {
          obj = obj.sort();
        }
      } else { // a string
        return obj.split('').sort().join('');
      }
    }

It's very simple to use:

Sort(counts);

And don't forget to add this line to the head section of your index.html file.

<script src='Sort.js'></script>

On the next page I'll show you my finished worth function. You should really have a go yourself first. Think of the satisfaction if you get it to work.
Realise that you won't be able to test for 3 and a pair and two pair at this stage.

previous
next