DEV Community

Cover image for Website lesson 7: understanding js
Yuri Filatov
Yuri Filatov

Posted on • Originally published at

Website lesson 7: understanding js

Welcome back! If you are a new user, I reall advise to read every previous post, starting from "HTML and CSS worth it?". If you are still reading, it means you are ready to move to harder things.

Summing up

  • Every element shouldn't be somewhere in the space, but in a structure: mass of structures.
  • Every variable name has the most value. If you have to write a line about somebodie's name, then your variable is called "name"
  • Every function is kept in a class. Why? To struct our functions too. Like a mother with all her children - they alll belong to her.

More about js

Hopefully, you already checked the communities, I offered

Clear function

If you are not a beginner, you already know, that in every language we have constructures and clear functions. Let's realize it:

Enter fullscreen mode Exit fullscreen mode

We define this function as a part of class, so it is visible for every element of out class.

What do we sent to the function?

Nothing. We work just inside of our class, we don't need anything outside (user's info) to clear database.

"what we delete".splice(from, how much);

Remove post

What to do, if we want to delete a specific one? Not every, but one with a specific id? The realization is similar to clear: you already know the method to delete smth - splice.

        for (var i = 0; i < posts.length; i++) {
            if (posts[i].id === id) {
                return posts.splice(i,1);
        throw "No object with " + id + " id";
Enter fullscreen mode Exit fullscreen mode

What do we send?

As the user decides, which element to delete, we need a filter - id. We send id as it is the infromationf from outside.

Then we check for id equal to find the object with the right id and then we use our method to delete.

Chech for the information from outside -> Find the object -> Delete it.

Add post

Again you want to add a specific object. First you have to understand, where to add: to the beginning or to the end of your database. Normally, we count from one to ... .Logically, we add to the end.

           return true;
       else {
           return false;
Enter fullscreen mode Exit fullscreen mode

Algorithm of functions always starts from checking the information from outside. We can't add the object, until the user enters its data and sends to our function.

Important thing: we check, if all the fields are correct by using our validating function that I described in the previous lesson. Why? Everybody can make a mistake and put for the name some numbers instead of string value.

If yes, we add object and return true.

Immitating communication

If we will check the adding function we need some object as it's the user's data. Let's initialize:

let ob={
    id: '5',
    destination: 'England',
    createdAt: new Date(2018, 0, 1, 2, 3, 4, 567),
    author: 'ZaicevAnatoliy',
    tag: "spring",
    flug: 'A-737'
Enter fullscreen mode Exit fullscreen mode

To work with class during the communication, we also need an object of class:
let a = new Work(posts);

Testing Add Post

As always to make checking comfortable we output some message.

console.log("test addPost: ")
Enter fullscreen mode Exit fullscreen mode

The second line outputs the result of the addPost function. Now you see, what for do we need an object of our class - to call its function outside the class. And what do we have in the brackets? That user's data (object) with all required fields to add.

Testing Remove Post

The output message + the result of our function

console.log("test removePost: ")
Enter fullscreen mode Exit fullscreen mode

Consider! My functions aren't full correct. You also have to check, if the id is out of the borders or if the user entered not a numeric value.

Testing clear function

Logically, we just call this function, using our object a

console.log("test clear: ")
Enter fullscreen mode Exit fullscreen mode

As you undersand our database will be empty. Then I guess you have a question: FOREVER??
No, you just immitate communication: nothing will be changed after.

Thanks for appretiation!
In 4 days wi will end up js functions, I will share my full code for it. Don't think that the end is near :)
It is not even the half !

More info you can find on my website.
Good luck in your job!

Top comments (0)