DEV Community

Code_Regina
Code_Regina

Posted on

Data Structures Introduction

                   -Which Data Structure is The Best?
                   -ES2015 Class Syntax Overview
                   -Data Structure: The Class Keyword
                   -Data Structure: Adding Instance Methods
Enter fullscreen mode Exit fullscreen mode

Which Data Structure is The Best?

Data structures are collections of values, the relationships among them, and the functions or operations that can be applied to the data. Data structures excel at different things. Some are highly specialized, while others are more generally used.

Visualized Data Structures

Array

Alt Text

Singly Linked List

Alt Text

Hash Table

Alt Text

Tree

Alt Text

Binary Search Tree

Alt Text

Undirected Graph

Alt Text

Directed Graph

Alt Text

All data structures store data, however the relationship between the data and the functionality between methods differ.

ES2015 Class Syntax Overview

What is a class?
A class is a blueprint for creating objects with pre-defined properties and methods.

Class example


class Student {
    constructor(firstName, lastName, year){
        this.firstName = firstName;
        this.lastName = lastName;
        this.grade = year;
    }
}

let firstStudent = new Student("Colt", "Steele",1);
let secondStudent = new Student("Blue", "Steele",2);

Enter fullscreen mode Exit fullscreen mode

The method to create new objects must be called constructor.
The class keyword creates a constant that can not be redefined.

Data Structure: Adding Instance Methods


class Student {
    constructor(firstName, lastName, year){
        this.firstName = firstName;
        this.lastName = lastName;
        this.grade = year;
        this.tardies = 0;
        this.scores = [];
    }
    fullName(){
        return `Your full name is ${this.firstName} ${this.lastName}`;
    }
    markLate(){
        this.tardies += 1;
        if(this.tardies >= 3) {
            return "YOU ARE EXPELLED!!!!"
        }
        return `${this.firstName} ${this.lastName} has been late ${this.tardies} times`;
    }
    addScore(score){
        this.scores.push(score);
        return this.scores
    }
    calculateAverage(){
        let sum = this.scores.reduce(function(a,b){return a+b;})
        return sum/this.scores.length;
    }  
}

let firstStudent = new Student("Colt", "Steele",1);
let secondStudent = new Student("Blue", "Steele",2);



Enter fullscreen mode Exit fullscreen mode

Top comments (3)

Collapse
 
ash_bergs profile image
Ash

Nice high-level overview, I can never have enough quick references and cheatsheets.

Collapse
 
im6h profile image
Vu • Edited

Good overview to DataStructures.

Collapse
 
ljcdev profile image
ljc-dev

Great introduction on the DS types. I also needed that refresher on class syntax 😁.