Extra Credit with JavaScript

In industry, true full-stack developers are highly valued. A full-stack developer is one that can write code both for the back-end (with Python, NodeJS, C#, Java, etc.), as well as write code for the front-end (HTML, CSS, JavaScript). We’ll do some front-end work in this class, but we’ll rarely if ever actually write any JavaScript.

Until now.

Every data structures/algorithms assignment will now have the option for 10 points extra credit if you can implement the same data structure/algorithm in JavaScript. This should be submitted solo.

Conditions

There are, of course, some conditions.

  1. To be eligible for any extra credit, your Python implementation must have received at least an 8/10
  2. Your JavaScript code must be in an installable npm package, complete with package.json. Make sure that the node_modules directory is appropriately placed in your .gitignore
  3. Your implementations must not use constructor functions. Instead, look up the class syntax and use that to build your objects.
  4. Your JavaScript code must be tested. I recommend using Jest as your testing framework. Without tests, you will get no more than 2 of the available 10 points. You can feel free to implement the same tests you wrote for the Python implementation. I should be able to run your tests from the js directory by typing npm test
  5. Your JavaScript code must be in a js directory inside of your data_structures repository. As an example:
/data_structures
    .gitignore
    tox.ini
    setup.py
    linked_list.py
    stack.py
    double_linked_list.py
    queue.py
    tests/
        test_linked_list.py
        test_stack.py
        test_double_linked_list.py
        test_queue.py
    js/
        package.json
        linkedList.js
        stack.js
        doubleLinkedList.js
        queue.js
        __test__/
            linkedList.test.js
            stack.test.js
            doubleLinkedList.test.js
            queue.test.js

Side Notes

Special Methods

Because these implementations are not Python implementations, it doesn’t make sense to try and accommodate assignment task items that require you to implement special methods.

  • Ignore anything asking you to be able to interact with Python’s len() function. JavaScript objects don’t have a length property by default, and there’s no corresponding JS version of len() to interact with. You can still implement your own length property if you’d find it useful.
  • Ignore anything asking you to be able to interact with Python’s print() function. NodeJS’s console.log achieves the same purpose. If you really want to dig into it, you can look into overriding an object’s toString() method to dictate how it should be displayed in the terminal.

Importing, Exporting, and Running from Terminal

You will need to know how to export code from script files and do NodeJS’s version of Python’s package imports in order to run your tests. Check out the MDN documentation on NodeJS imports and NodeJS exports.

For equivalents to Python’s if __name__ == "__main__", check out this StackOverflow answer.

Error Handling

You will want to replicate appropriate error-handling with your functions and methods so that your code only does what you intended for it to do. Check the documentation on JavaScript errors to understand what types of errors can be thrown, how to actually raise your own errors, and how to catch and handle errors that get thrown. Note that Jest can watch for errors to be thrown just like Pytest can using with pytest.raises(SomeException). Use wisely.

Remember to always try out the code you write to make sure that it does what you think it does. Read the outputs. Understand the errors that get raised, where they happen, and why they happen.

Semicolons

Semicolons. No one wants to see that shit, full-stop. Learn how automatic semicolon-insertion works and where it’s appropriate to not use them vs where you have to. If you’re coming from 301 you’ll still be throwing semicolons into your code out of habit. When you submit this assignment, there should be no more semicolons than are necessary.

Default Parameters

It may help to use default parameters for your functions/methods. Check the MDN documentation on default parameters for info on how to do that.

Advanced Operators

If you ever use *args, you may find JavaScript ES6’s spread operator useful. Similarly, **kwargs-like behavior can be mimicked using ES6’s destructuring

Submission

For each assignment calling for a Python implementation of a data structure or algorithm, there is a corresponding a separate assignment for the JS submission in the “Extras”. For ease of grading, after your Python implementation receives an 8/10 or above (or after you submit code that you suspect will meet this requirement), create a <data-structure>-js branch (e.g. linked-list-js) and do your JavaScript implementation on that branch.

When you have finished your work and all your tests pass, create a new pull request from your <data-structure>-js branch to master. Copy the URL of your pull request and submit it using the URL input. When you’re done submitting, you may merge your branch into master.

As usual, use the comment feature in Canvas to add questions, comments, or reflections on the work you did for this assignment.