> For the complete documentation index, see [llms.txt](https://dtian.gitbook.io/daily-report-1/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://dtian.gitbook.io/daily-report-1/day-35-10-10.md).

# Day 35 - 10/10

Plan: Learning Js&#x20;

* Array
* Date
* Random
* Booleans
* Comparison
* If else

Progress:&#x20;

#### <mark style="color:blue;">Number object methods</mark>&#x20;

* `Number.isInteger(x);` : return true if x is an integer
* `Number.isSafeInteger(x); :` check if a number safe

*<mark style="background-color:yellow;">Safe integer are all integers from (2^53 - 1) to -(2^53 - 1).</mark>*

* `Number.parseFloat("x");` : parse a string and return the first number
* `Number.parseInt("x");` : parses a string and returns a whole number

### <mark style="color:blue;background-color:green;">Number Properties</mark>

* `let var = Number.EPSILON;` : the difference between the smallest floating point number greater than 1 and 1
* `let var = Number.MAX_VALUE;` : the largest possible number in JavaScript
* let var = Number.MIN\_VALUE; : the lowest possible number in JavaScript
* `let var = Number.MAX_SAFE_INTEGER;` : is (2^53 - 1)
* `let var = Number.MIN_SAFE_INTEGER;` : is -(2^53 - 1)
* `let var = Number.POSITIVE_INFINITY;` : is infinity
* `let var = Number.NEGATIVE_INFINITY;` : is -infinity
* `let var = Number.NaN;` : check if a var is a number or not

### <mark style="color:blue;background-color:green;">Arrays</mark>

<mark style="color:orange;">`const`</mark><mark style="color:orange;">` `</mark>*<mark style="color:orange;">`array_name`</mark>*<mark style="color:orange;">` `</mark><mark style="color:orange;">`= [`</mark>*<mark style="color:orange;">`item1`</mark>*<mark style="color:orange;">`,`</mark><mark style="color:orange;">` `</mark>*<mark style="color:orange;">`item2`</mark>*<mark style="color:orange;">`, ...];`</mark>

or

<mark style="color:orange;">`const array_name = [];`</mark>&#x20;

<mark style="color:orange;">`array_name[0]= "`</mark>*<mark style="color:orange;">`item1`</mark>*<mark style="color:orange;">`";`</mark>&#x20;

<mark style="color:orange;">`array_name[1]= "`</mark>*<mark style="color:orange;">`item2`</mark>*<mark style="color:orange;">`";`</mark>&#x20;

<mark style="color:orange;">`array_name[2]= "`</mark>*<mark style="color:orange;">`item3`</mark>*<mark style="color:orange;">`";`</mark>

* can use `array_name[n] = "itemN";` to change the value in specified position
* `.toString()` : convert array to string (comma separated)
* array is object
* array elements can be objects: can have variables of different types in the same Array (have objects in array; have functions in array; have arrays in array

#### <mark style="color:blue;">Array properties</mark>

* array.length : number of elements
* loop

`for (let i = 0; i < length; i++) {  }`

or

`array.forEach(function);`

* adding: `array.push(" ");`
* in Js, arrays always use number index
* Create an array with one element: `const points = [40];`
* Create an array with 40 undefined elements: `const points = new Array(40);`
* Array.isArray(x) : check if a var is an array or not
* `object instanceof Array;` check if an object is created by a given constructor

#### <mark style="color:blue;">Array methods</mark>

* `.length`
* `.toString()` : array to string with comma seperated                                                                                                                                                                                                                         &#x20;
* `.pop()` : remove the last item
* `.push()` : add an item to tail
* `shift()` : remove the first item and shift the rest to a lower index&#x20;
* `unshift()` : add an item to head and un-shift the rest
* `join()`
* `delete ;` : delete the item and leave undefined hole
* `.concat();` : merge existing arrays
* `.flat();` : reduce the dimension of an array
* `.splice(x, y, "item");` : add item to position x, with y items removed from that position toward.
* `.slice(x, y);` : remove y items from position x without leaving undefined holes; if y is omitted, it will remove every item before x

#### <mark style="color:blue;">Array sort</mark>

* `.sort();` : sort an array alphabetically (work well on string, not number)
* `.reverse();` : reverse an array

#### <mark style="color:blue;">Numeric sort</mark>

`array.sort(function(a, b){return a - b});`

compare function `function(a, b)`

When comparing 40 and 100, the `sort()` method calls the compare function(40, 100)

* if return negative, 40 is sorted before 100
* if return positive, 40 is sorted after 100
* if return 0, no change

#### <mark style="color:blue;">Random sort</mark>

`array.sort(function(){return 0.5 - Math.random()});`

#### <mark style="color:blue;">Fisher Yates Method - random sort</mark>

`for (let i = array.length -1; i > 0; i--) {`&#x20;

`let j = Math.floor(Math.random() * (i+1));`&#x20;

`let k = array[i];`&#x20;

`array[i] = array[j];`&#x20;

`array[j] = k; }`

#### <mark style="color:blue;">Math.max() / Math.min()</mark>&#x20;

Find max/min number in an array

`function myArrayMax(arr) {`&#x20;

`return Math.max.apply(null, arr);`&#x20;

`}`

`function myArrayMin(arr) {`&#x20;

`return Math.min.apply(null, arr);`&#x20;

`}`

#### <mark style="color:blue;">Find Min/Max Js method</mark>

`function myArrayMax(arr) {`

`let len = arr.length;`&#x20;

`let max = -Infinity;`&#x20;

`while (len--) {`&#x20;

`if (arr[len] > max) {`&#x20;

`max = arr[len];`&#x20;

`}`

`}`&#x20;

`return max;`&#x20;

`}`

#### <mark style="color:blue;">Sort Object array</mark>

`const cars = [`&#x20;

`{type:"Volvo", year:2016},`&#x20;

`{type:"Saab", year:2001},`&#x20;

`{type:"BMW", year:2010} ];`

* Sort with number:

`cars.sort(function(a, b){return a.year - b.year});`

* Sort with string

`cars.sort(function(a, b){`&#x20;

`let x = a.type.toLowerCase();`&#x20;

`let y = b.type.toLowerCase();`&#x20;

`if (x < y) {return -1;}`

`if (x > y) {return 1;}`&#x20;

`return 0; });`

#### <mark style="color:blue;">Array Iteration</mark>

* `.forEach(function)` : call function once for each item
* `.map(function)` : create a new array by execute the old one with function
* <mark style="color:red;">.flatmap(function) ?</mark>
* `.filter(function)` : create an array with elements passed some conditions
* `.reduce()` : create an array by running a function on each array element from left to right to produce (reduce it to) a single value.
* `.reduceRight()` : create an array by running a function on each array element from right to left to produce (reduce it to) a single value.
* `.every()` : check if every elements pass the conditions
* `.some()` : check if some elements pass the conditions
* `.indexOf(item, start)` : search for an element and return its first position
* `.lastIndexOf(item, start)` : search for an element and return its last position
* `.find(function)` : return the first number that passes conditions
* `.findIndex()` : returns the index of the first array element that passes a test function
* `Array.from()` : returns an Array object from any object with a length property or any iterable object
* <mark style="color:red;">`.keys()`</mark> <mark style="color:red;"></mark><mark style="color:red;">: returns an Array Iterator object with the keys of an array</mark>
* <mark style="color:red;">`.entries()`</mark> <mark style="color:red;"></mark><mark style="color:red;">: Create an Array Iterator, and then iterate over the key/value pairs</mark>
* `.includes()` : check if an element is present in an array
* Spread (...) : expands an iterable (like an array) into more elements

#### <mark style="color:blue;">Array Const</mark>&#x20;

* <mark style="background-color:orange;">An array declared with</mark> <mark style="background-color:orange;"></mark><mark style="background-color:orange;">`const`</mark> <mark style="background-color:orange;"></mark><mark style="background-color:orange;">cannot be reassigned</mark>
* <mark style="background-color:orange;">Element can be reasssigned</mark>
* An array declared with `const` must be initialized when it is declared;&#x20;

~~`const cars;`~~&#x20;

~~`cars = ["Saab", "Volvo", "BMW"];`~~

* Arrays declared with `var` can be initialized at any time

`cars = ["Saab", "Volvo", "BMW"];`&#x20;

`var cars;`

* Const : <mark style="color:green;">Block Scope</mark> - <mark style="color:red;">Redeclaring</mark>&#x20;
* Var : <mark style="color:red;">Block Scope</mark> - <mark style="color:green;">Redeclaring</mark>

### <mark style="color:blue;background-color:green;">Date Object</mark>

Create date object with current date and time

* `new Date()`

Creates a date object from a date string

* `new Date(`*`date string`*`)`

Create date object with a specified date and time

* `new Date(`*`year,month`*`)`
* `new Date(`*`year,month,day`*`)`
* `new Date(`*`year,month,day,hours`*`)`
* `new Date(`*`year,month,day,hours,minutes`*`)`
* `new Date(`*`year,month,day,hours,minutes,seconds`*`)`
* `new Date(`*`year,month,day,hours,minutes,seconds,ms`*`)`
* `new Date(`*`milliseconds`*`)`

Month count from 0 - 11

Day / month overflow will add to the next month / year

One and two digit years will be interpreted as 19xx

<mark style="color:orange;">Dates are saved as number of milliseconds since January 01, 1970.</mark>

#### <mark style="color:blue;">Displaying date</mark>

`const d = new Date();`

Default: `date.toString();`

More readable: `date.toDateString();`

UTC standard: `date.toUTCString();`

ISO standard: `date.toISOString();`

#### <mark style="color:blue;">Date Format</mark>

**Output format**

ISO:&#x20;

* Date: (YYYY-MM-DD)
* Date - Time: (YYYY-MM-DDTHH:MM:SSZ)

Short Date: (MM/DD/YYYY)

Long Date: (MM DD YYYY)

**Input format**

Get the number of milliseconds between the date and Jan 1, 1970 and convert to a date object:

`let msec = Date.parse("March 21, 2012");`&#x20;

`const d = new Date(msec);`

### <mark style="color:blue;background-color:green;">Get Date Methods</mark>

* `d.getFullYear();` : return just the year as 4 digit number
* `d.getMonth();` : return just the month as number (0-11)
* `d.getDate();` : return just the day as number (1-31)
* `d.getHours();` : (0-23)
* `d.getMinutes();` : (0-59)
* `d.getSeconds();` : (0-59)
* `d.getMilliseconds();` : (0-999)
* `d.getDay();` : (0-6)
* `d.getTime();` : return the number of milliseconds since January 1, 1970
* `Date.now();` : returns the number of milliseconds since January 1, 1970
* `.getTimezoneOffset()` : returns the difference (in minutes) between local time an UTC time

### <mark style="color:blue;background-color:green;">Set Date Methods</mark>&#x20;

* `d.setFullYear(x);`
* `d.setMonth(0-11);`
* `d.setDate(1-31);`
* `d.setHours(0-23);`
* `d.setMinutes(0-59);`
* `d.setSeconds(0-59);`

### <mark style="color:blue;background-color:green;">Random</mark>&#x20;

* `Math.random();` : returns a random number between 0 and 1
* `Math.floor(Math.random() * 10^n);` : returns a random number

### <mark style="color:blue;background-color:green;">Boolean</mark>&#x20;

Everything without a value is False
