Bummer! You must be logged in to access this page.

Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Sorting Question

I'm working on this question for class. I've posted my answer below. Am I on the right track?

// Implement the following functions. Write at least 2
// assertions for each one (the assertions are how you
// will test your code)
//*********************************************************

function lastLetterSort(stringArray) {
  function byLastLetter(a, b) {
    //TODO: implement me. sort the strings in alphabetical
    // order using their last letter
  }
  stringArray.sort(byLastLetter);
}

function sumArray(numberArray) {
  var sum = 0;
  // TODO: implement me using forEach
  return sum;
}

function sumSort(arrayOfArrays) {
  arrayOfArrays.sort(function(item) {
    // TODO: implement me using sumArray
    //  order the arrays based on the sum of the numbers
    //  inside each array
  });
}

MY SOLUTION

if(a < b) {
            return -1;
        } else if(a > b) {
            return 1;
        }

        return 0;

        /* Advanced Solution, inline if */
        // return a < b ? -1 : (a > b ? 1 : 0);
    }
    stringArray.sort(byLastLetter);
}

function sumArray(numberArray) {
    var sum = 0;

    numberArray.forEach(function(number) {
        sum += number;
    });

    return sum;
}

function sumSort(arrayOfArrays) {
    arrayOfArrays.sort(function(a, b) {
        var sumA = sumArray(a),
            sumB = sumArray(b);

        /* Same here like in lastLetterSort */
        /* Basic Solution */
        if(a < b) {
            return -1;
        } else if(a > b) {
            return 1;
        }

        return 0;

        /* Advanced Solution, inline if */
        // return a < b ? -1 : (a > b ? 1 : 0);
    });
}

I added the language after your backticks so we get nice syntax highlighting for JavaScript. :)

2 Answers

You seem to be on the right track. Don't forget to return your sort results, and don't forget to write a test suite at the bottom so you can see that your functions are working.

Remember in your function to sort by the last letter that the whole string is being passed in to the function. Don't forget to compare the last characters only!

Feel free to post updates here if you get stuck anywhere, and I'm sure Jacob Mishkin or I can give you some hints.

yeah man, I think you are on the right track. Looks good from what I can see. If you want to double check you can always go to:

www.stackoverflow.com

and type your issue there. If this is for class, maybe not, but its there just so you know.

Happy coding.