Website lesson 7: understanding js
Tags: html, css, javascript, productivity
Published: 2021-06-14 06:38:15

Welcome back! If you are a new user, I reall advise to read every previous post, starting from ["HTML and CSS worth it?"](https://dev.to/filatovv/html-css-is-it-worth-to-start-4d3c). 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](https://javascript.info), 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:

```
clear(){
        posts.splice(0,posts.length);
    }
```
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. 

Template:
`"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.

```
 removePost(id){
        for (var i = 0; i < posts.length; i++) {
            if (posts[i].id === id) {
                return posts.splice(i,1);
            }
        }
        throw "No object with " + id + " id";
    }
```

####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.

Algorithm:
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. 

```
addPost(Object){
       if(this.validatePost(Object)){
           posts.splice(posts.length,0,Object);
           return true;
       }
       else {
           return false;
       }
    }
```
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](https://dev.to/filatovv/website-lesson-6-js-function-realization-27bk). 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'
}
```
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: ")
console.log(a.addPost(ob))
```
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: ")
console.log(a.removePost("2"))
```
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: ")
console.log(a.clear())
```
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!