Declared and undeclared variables in functions

ref – https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/var

Declared variables are constrained in the execution context in which they are declared.

Undeclared variables are always global

…even in inner functions

Let’s declare an inner function called ‘inner’. We have an undeclared variable ‘name’.
That name will be declared as a global. It will be accessible by all scopes.

Chat Room using Node

ref – https://socket.io/get-started/chat

setup

Assuming you have node, npm, nodemon/gulp installed.

First, make a directory called DesktopApp. Then start the process of creating a node project.


npm init

Enter your information for the package.json file.

Install Express:

npm install –save express@4.15.2

Server can push messages to clients. Whenever you write a chat message, the idea is that the server will get it and push it to all other connected clients.

Installing mongodb on Ubuntu

ref – https://www.digitalocean.com/community/tutorials/how-to-install-mongodb-on-ubuntu-18-04

Step 1 — Installing MongoDB

Ubuntu’s official package repositories include an up-to-date version of MongoDB, which means we can install the necessary packages using apt.

First, update the packages list to have the most recent version of the repository listings:

sudo apt update
Now install the MongoDB package itself:

sudo apt install -y mongodb
This command installs several packages containing the latest stable version of MongoDB, along with helpful management tools for the MongoDB server. The database server is automatically started after installation.

Next, let’s verify that the server is running and works correctly.

Step 2 — Checking the Service and Database

The installation process started MongoDB automatically, but let’s verify that the service is started and that the database is working.

First, check the service’s status:

sudo systemctl status mongodb

sending off questions and answers data

Regular Expressions (js)

new style

literal style

test

replace

Found “ryu” at position 6 in string “shooo ryu ken ryu Ryu rYu ryU!”
Found “ryu” at position 14 in string “shooo ryu ken ryu Ryu rYu ryU!”
Found “Ryu” at position 18 in string “shooo ryu ken ryu Ryu rYu ryU!”
Found “rYu” at position 22 in string “shooo ryu ken ryu Ryu rYu ryU!”
Found “ryU” at position 26 in string “shooo ryu ken ryu Ryu rYu ryU!”

After replacement: shooo ryu ken ryu ryu ryu ryu!

wait for async code in a callback function

Situation

We have a function that does some processing. Then it executes a callback function.

So say we call function A, and provide an anonymous callback function.

Under this case, we will get the true in function A’s definition.
Everything is synchronous so we go step by step.

The Problem

The problem happens when we have asynchronous code in our callback

Given definition A:

In our callback definition, we do something asynchrnous. It returns true after 2 seconds.

This is a problem because while the execution is happening for the setTimeOut of waiting 2 seconds, our main execution
continues and our reply will be undefined.

The Solution

The solution is to use Promises. In this particular case, I will use jQuery’s Deferred, which is exactly the same as Promise, except its packaged in jQuery’s API.

1) chain the ‘then’

In order to wait for the async to finish, we call the callback function, and then chain the API call ‘then’ in order to continue with the results. This is assuming we declared a Promise object and returned it in cbConfirmed(). (we’ll get to this in the next step)

When you chain the then call, it will wait for async code to finish, then you simply provide an anonymous callback function inside to get the results.

2) declare and return the Promise object

3) call resolve and use it where the async code has finished

The main execution will wait for the callback to finish. After 2 seconds, the callback returns and gives the result to function A.

quotes on love and relationship

每一个好的爱情,基于在尊重上。如果没有尊重,这个爱活不下去的。
每天要跟新你的爱。讲个笑话。。。带她出去玩。。。给他推拿。。。
结婚的目的,是教你怎么去做多情的朋友们。
爱情不会要求占有。爱情是给自由的。
你爱一个人,因为只有你能听到他生活上唱的歌。
微笑开始一个爱情。亲吻把爱情增长。拥抱让爱情持久。
爱是什么? 当两端有足够的空间时,爱就会让两个人坐在长凳的中间。

Wait for end of resize event (jQuery)

ref – https://stackoverflow.com/questions/5489946/jquery-how-to-wait-for-the-end-of-resize-event-and-only-then-perform-an-ac?noredirect=1

num of lines v2

todo –

given a that a previous space exists:

usually when we pass the limit with a letter, we shift down to the next line, and keep going with the word after the ‘previous space’.

“abcde fghijk”(‘l’ passed limit)

becomes

“abcde
fghijklmn”

However, there is a case where if the offending letter that went passed the limit has a space for the next value….

for example, given “abc def ghi jkl mno”…

abc def ghi (‘i’ passed limit)

then in this particular situation..

1) we include the offending letter as the previous string
2) break line
3) start next line with space

“abc def ghi”
” jkl mno”

js

html