# Day 24 : Become a self-taught blockchain developer with zero knowledge in 365 days

# PREVIOUS: DAY 23

order 8: the fundamentals of Javascrpit

(Technical aspect, Computer Science, Phase 1: a normal technican)

## 5.2 Numbers

## Two types of number in Javascript

There are two types of number in Javascript, and they are stored as different data types. The first is regular numbers we're using, which are stored in 64-bit format. And the other is Bigint numbers, they are needed when we want to represent numbers more than 2^53 or less than -2^53.

## Why the regular number can't represent numbers more than 2⁵³ or less than -2⁵³?

A regular number is represented in the 64-bit format. Theoretically, it is able to store numbers between 2^64 to -2^64, for the computer is only able to recognize 0 and 1, and one digit requires one bit to store. However, among the whole 64-bit, 52 of them are used to store the digits, 11 of them store the position of the decimal point (they are zero for integer numbers), and 1 bit is for the sign. Hence, the regulat number is merely able to represent the number between 2⁵³ to -2⁵³.

## The way to write a number

- style 1: let number = 7300000000
- style 2: let number = 7_300_000_000 //The JavaScript engine ignores _ between digits
- style 3:

(1) let number = 7.3e9 // 7.3bn, `e`

multiplies the number by `1`

with the given zeroes count

(2) let number = 1e-6 // one millionth, a division by 1 with the given number of zeroes

## Hex, binary and octal numbers

Aside from decimal number ,which is widely used for human, there are three numeral system that I have to know. The concept should be known while dealing with the computer.

- Hex: Also called hexidecimal numbers. Their composition of numbers are 0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f. When encountering 16 in decimal number, it will hit the ceiling and become 10
- binary: Their composition of numbers are 0 and 1. When encountering 2 in decimal number, it will hit the ceiling and become 10.
- octal: Their composition of numbers are 0,1,2,3,4,5,6,7. When encountering 8 in decimal number, it will hit the ceiling and become 10

There is still more to learn for the technique of the value changing between these numeral systems. However, I think there is no need for me to do so by now. Just focusing on the concept itself would be fine.

## The way to indicate the use of different numeral system

- Hex:
`0x`

and then the number, and the case of the letter doesn'y matter just like our normal habit. - binary:
`0b`

and then the number - octal:
`0o`

and then the number - other numeral sytem: for other system, there is no such supports. we have to use the function
`parseInt`

## num.toString(the target system) for numeral system changing

- by default for 10
- vary from 2 to 36

## Rounding part 1

built-in function for rounding

- Math.floor: for round down
- Math.ceil: for rounD up
- Math.round: rounds to the nearest integer
- Math.trunc: Removes anything after the decimal point without rounding

## Rounding part 2

run to the appointed layer of digit. E.g., we want to 1.01234 run to 2 digit, which is only 1.01.

- method 1: Multiply-and-divide.

2. method 2: toFixed(n)

(1) This rounds up or down to the nearest value, similar to `Math.round`

(2) that result of `toFixed`

is a string

(3) If the decimal part is shorter than required, zeroes are appended.

## inaccurate caculation for Javascript

- situation 1

2. situation 2

(1) This is the innate limits for the numeral system. For decimal numbers, it can distinguish 1/10 or 1/20 easilym but are the result of 1/3 would be 0.333333...... It also happens the same to binary numbers, it can recognize 1/2 with no problem, but for 1/10 or 1/20, it will become endless fractions like 1/3 is decimal systems.

(2) Many programming languages have this kind of limits as well.

(3) SOLUTION 1

A. the solution can't be solved completely, for it is the innate feature of the language itself, we can merely use some little trick to stop this from happening.

B. toFixed(n)

(4) SOLUTION 2

(5) conclusion

Totally evading fractions is rarely possible.

To avoid the problem completely isn't promissed, just like the example we've seen of the solution 2. Hence, we could just cut them off once and for all, which is the solution 1

3. situation 3

comes from its limis of 64-bit format, and it's unsolvable.

4. situation 4

two zeros can exist at the same time, and the distinct is unnoticeable

## TEST 1: isNaN(value)

1. NaN is one of a numeric value

2. Test if a thing is NaN by using isNaN(value), it would simply convert the value into number and test if it belongs to NaN

3. it is needed because "=== NaN" won't work, for NaN equals to nothing, including itself.

## TEST 2: isFinite(value)

- convert the value to a number, and returns true/false depending on whether it is finite.

2. usage: to see whether the a string value is a regular number

Please note that an empty or a space-only string is treated as `0`

in all numeric functions including `isFinite`

.

## TEST 2: Object.is(a,b)

- built-in for comparing values in === way,
`Object.is(a, b)`

is the same as`a === b`

- NaN works unlike normal ===: Object.is(NaN, NaN) === true
- distinguish the difference of 0 and -0: Object.is(0, -0) === false
- When an internal algorithm needs to compare two values for being exactly the same, it uses
`Object.is`

(internally called SameValue).

## parseInt and parseFloat part 1: powerful functionality for numeric conversion

It is need becuase for using normal numeric conversion, such as +(unary plus) ot Number(), it fails if the value itself can not completely converted to a number. However, in reality, there are many situations that require this kind of "imperfect" numeric conversion. For example, `"100px"`

or `"12pt"`

in CSS, or “19€” for repesenting the amount of the currency.

parseInt and parseFloat are able to read numbers until they can't and return the value it has read. The former is for reading integer, and the latter is able to read the floating numbers.

## parseInt and parseFloat part 2: changing numeric system

Other math functions

- Math.random(): return a random number from 0 to 1.
- Math.max( , , )
- Math.min( , , )
- Math.pow(n, power)

## 5.3 Strings

Three kinds of quotes

- ""
- ''
- ``

(1) able to embed expressions in the string with ${…}

(2) able to support multiple line quotes

(3) able to support "template function" by using func`string` (rarely used)

## special character PART 1: realizing multiline quotes without ``

1. \n means next line

## special character PART 1: OTHERS

- They all start with \, which is also called escape character
- backslash
`\`

serves for the correct reading of the string by JavaScript, then disappears. The in-memory string has no`\`

.

- quotes: in order not to become the string (but if the choice of the quote character is not the same, it can be used safely.)
- unicode: special symbols
- the backlash in the string: \\ (if there is only one, the engine would just ignore it.)

## string length

- str.length (not str.length(), it is a property not a function)
- example

## str[]: get the character at position x

- The first character starts from 0 position

2. an alternative: charAt()

3. if there is no position, it would return undefined for str[]

## overwritte the string

- you can't replace the old string of the variable with new one using normal way

2. you can only create a new one and assign it to the old one and the old one would be replaced

## change the case PART 1: toLowerCase()

"....".toLowerCase()

## change the case PART 2: toUpperCase()

“….”.toUpperCase()

## change the case PART 2: only for single letter

- “….”[0].toLowerCase()
- e.g., alert( ‘Interface’[0].toLowerCase() ); // ‘i’

## Searching for a substring in a string part 1: return the order

- str.indexOf(‘……’)
- in which position the substring is found, and return the order of the position
- return -1 if it can't be found
- It will start from the 0
- the case matters

## Searching for a substring in a string part 2: return the order other than the first substring

- advanced functionality of part 1
- str.indexOf(‘……’, order)
- looking for the occurence other than the first

## Searching for a substring in a string part 3: return all the orders of substring

- finding all occerences

## the trick of the bitwise NOT: +1

- NOT number converts the number to a 32-bit integer
- for 32-bit integers
`~n`

equals`-(n+1)`

.

## Searching for a substring in a string part 4: true/false for the existence of substring

- pratical use of the searching: indexOf in the “if” test, to alert if we found one

(1) the test `if ( ~str.indexOf("...") )`

is truthy only if the result of `indexOf`

is not `-1`

.

(2) it's not recommended to use in this kind of unobvious way, but it is widely used in the old code.

(3) `if (~str.indexOf(...))`

reads as “if found”.

## Searching for a substring in a string part 5: true/false for the existence of substring

- way better than the method of part 4
- "......".includes(...)
- returns true/false

## Searching for a substring in a string part 6: true/false for the existence of substring in the specified order

- advanced functionality of part 5
- “……”.includes(…, order)
- true/false for the position to start searching from

## other funtionality: startsWith/endsWith

## getting the substring: return the part of the substring of the specified interval of order: METHOD 1

- memorizing
**this method would be enough** - Returns the part of the string from
`start`

to (but not including)`end`

- str.slice(start [, end])

4. If there is no second argument, then `slice`

goes till the end of the string:

5. Negative values for `start/end`

are also possible. They mean the position is counted from the string end:

6. The specified order of the interval must be arranged from front to back

## getting the substring: return the part of the substring of the specified interval of order: METHOD 2

- str.substring(start [, end])
- Returns the part of the string
*between*`start`

and`end`

(not included), which is almost the same as`slice`

- it allows
`start`

to be greater than`end`

. (the arrangement of order isn't restricted, the reversed is still the same as the non-reserved one)

3. Negative arguments are (unlike slice) not supported, they are treated as `0`

.

## getting the substring: return the part of the substring of the specified interval of order: METHOD 3

- str.substr(start [, length])
- Returns the part of the string from
`start`

, with the given`length`

instead of the ending position

3. The first argument may be negative, to count from the end

## comparisions for string PART 1: common case

- character-by-character: A lowercase letter is always greater than the uppercase:
- word comparison: Letters with diacritical marks are “out of order”

## comparisions for string PART 2: number for the character

- Each character has a corresponding numeric code, and it is allowed to get the code of the character back
- str.codePointAt(pos)

(1) different case letters have different codes

3. String.fromCodePoint(code)

(1) We can also add Unicode characters by their codes using `\u`

followed by the hex code:

## comparisions for string PART 3: algorithm for string comparison

- due to its nature, the algorithm for string comparison would be more complex.
- the solution: ‘.......’.localeCompare(‘.......’)

(1) a special method to compare strings in different languages, following their rules.

- Returns a negative number if
`str`

is less than`str2`

. - Returns a positive number if
`str`

is greater than`str2`

. - Returns
`0`

if they are equivalent.

#

order (4): Introduction to the blockchain technology by Ethereum, developer perspective

# Fundamental topics

- Blocks
*— The way transactions are batched to ensure state is synchronised across all actors#* - The Ethereum Virtual Machine (EVM)
*— The EVM handles all the computation on the Ethereum network#* - Gas
*— Computational power required to process transactions, paid for in ETH by transaction senders#* - Nodes and clients
*— The individuals participating in the network and the software they run to verify transactions#* - Networks
*— Implementations of Ethereum including test networks* - Block explorers
*— A way to read data about transactions, accounts, blocks and the network* - Mining
*— The way transactions are processed and blocks are created*

###The four parts with # are completed. In the beginning, I thought there is no need for reorganize the learning by writing them down, but now I think it's necessary to do so. I would move to the beginning and redo them again, but just for the topics that are exclusively for Ethereum. There are some key but complex concepts that I have much more to work on.

For the parts I haven't completely grasped, it's better to find more realted materials.###

#