DEV Community

Recap of Data Structures with Javascript Part 1

Norbert Braun on October 17, 2018

I decided to write an article about implementing common data structures. The focus is mainly on coding in javascript rather than on theoretical ex...
Collapse
 
bnorbertjs profile image
Norbert Braun • Edited

The easiest way to run the examples is parceljs.

Create an index.html in the same directory. Include your index.js in it like this.

<!DOCTYPE html>
<html>
<head>
</head>
<body>
    <script src="./index.js"></script>
</body>
</html>

Then install parcel with npm globally -> npm i -g parcel-bundler
After that, you can type "parcel index.html" in the console and it will be available on localhost:1234 and you can check the output in the console of the browser.

Parcel is a zero-configuration web application bundler by the way.

If you want to run it with node, you can do node ./index.js in the console, but it will fail because of the "import { blabla } from "something" syntax.

You can change the export statements to the following (example):

module.exports = {
 BinarySearchTree: BinarySearchTree,
 Node: Node
}

and then import it in your index.js
const { BinarySearchTree} = require("./BinarySearchTree")

If you don't want to change the code, you can setup babel with node as well.
(this is babel6, for babel7 please check the official documentation)

step1: npm -i babel-cli babel-preset-env
step2: create a .babelrc file that looks like this:

{
    "presets": ["env"]
}

step3: run your index.js with the following command:
./node_modules/.bin/babel-node index.js

Collapse
 
chenge profile image
chenge

Cool, I use parcel, thanks Braun really.

Collapse
 
nicolasmesa profile image
Nicolas Mesa

Really cool! Thanks for sharing.

I think there's a bug in the setter of the HashTable:

if(isKeyExists){
    //key already exists, overwrite the previous value

    // here, you're overriding the whole array. If there were any collisions, all of them would be deleted.
    this.storage[index] = [[key, value]] 
}else{
    ...
}
Collapse
 
chenge profile image
chenge

Could you tell me how to run? Node version?

Collapse
 
bnorbertjs profile image
Norbert Braun

Hey chenge, please check my comment above. It should help you out.