Daily Report
  • Day 1 - 22/8
  • Day 2 - 23/8
  • Day 3 - 24/8
  • Day 4 - 25/8
  • Day 5 - 26/8
  • Day 6 - 28/8
  • Day 7 - 29/8
  • Day 8 - 30/8
  • Day 9 - 4/9
  • Day 10 - 5/9
  • Day 11 - 6/9
  • Day 12 - 7/9
  • Day 13 - 8/9
  • Day 14 - 9/9
  • Day 15 - 11/9
  • Day 16 - 12/9
  • Day 17 - 14/9
  • Day 18 - 15/9
  • Day 19 - 18/9
  • Day 20 - 19/9
  • Day 21 - 20/9
  • Day 22 - 21/9
  • Day 23 - 22/9
  • Day 24 - 23/9
  • Day 25 - 25/9
  • Day 26 - 26/9
  • Day 27 - 27/9
  • Day 28 - 28/9
  • Day 29 - 29/9
  • Day 30 - 2/10
  • Day 31 - 3/10
  • Day 33 - 7/10
  • Day 32 - 4/10
  • Day 34 - 9/10
  • Day 35 - 10/10
  • Day 36 - 11/10
  • Day 37 - 16/10
  • Day 38 - 17/10
  • Day 39 - 18/10
  • Day 40 - 19/10
  • Day 41 - 20/10
  • Day 42 - 21/10
  • Day 43 - 23/10
  • Day 44 - 25/10
  • Day 45 - 26/10
  • Day 46 - 27/10
  • Day 47 - 30/10
  • Day 48 - 31/10
  • Day 49 - 1/11
  • Day 50 - 2/11
  • Day 51 - 3/11
  • Day 52 - 5/11
  • Day 53 - 6/11
  • Day 54 - 7/11
  • Day 55 - 8/11
  • Day 56 - 9/11
  • Day 57 - 10/11
  • Day 58 - 13/11
  • Day 59 - 14/11
  • Day 60 - 15/11
  • Day 61 - 16/11
  • Day 4/12
  • Day 6/12
  • Day 7/12
  • Day 8/12
  • Day 12/12
  • Day 13/12
  • Day 14/12
  • Day 15/12
  • Day 16/12
  • Day 18/12
  • Day 19/12
  • Day 20/12
  • Day 21/12
  • Day 25/12
  • Day 26/12
  • Day 27/12
  • Day 28/12
  • Day 29/12
  • Day 3/1
  • Day 4/1
  • Day 5/1
  • Day 8/1
  • Day 10/1
  • Day 11/1
  • Day 12/1
  • Day 13/1
  • Day 15/1
  • Day 16/1
  • Day 17/1
  • Day 18/1
  • Day 19/1
  • Day 23/1
  • Day 24/1
  • Day 27/1
  • Day 29/1
  • Day 30/1
  • Day 1/2
  • Day 2/2
  • Day 5/2
  • Day 6/2
  • Day 15/2
  • Day 16/2
  • Day 17/2
  • Day 19/2
  • Day 20/1
  • Day 21/2
  • Day 22/2
  • Day 23/2
  • Day 26/2
  • Day 27/2
  • Day 28/2
  • Day 29/2
  • Day 1/3
  • Day 4/3
  • Day 5/3
  • Day 6/3
  • Day 7/3
  • Day 8/3
  • Day 9/3
  • Day 11/3
Powered by GitBook
On this page
  • Number Properties
  • Arrays
  • Date Object
  • Get Date Methods
  • Set Date Methods
  • Random
  • Boolean

Day 35 - 10/10

Plan: Learning Js

  • Array

  • Date

  • Random

  • Booleans

  • Comparison

  • If else

Progress:

Number object methods

  • Number.isInteger(x); : return true if x is an integer

  • Number.isSafeInteger(x); : check if a number safe

Safe integer are all integers from (2^53 - 1) to -(2^53 - 1).

  • Number.parseFloat("x"); : parse a string and return the first number

  • Number.parseInt("x"); : parses a string and returns a whole number

Number Properties

  • 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

Arrays

const array_name = [item1, item2, ...];

or

const array_name = [];

array_name[0]= "item1";

array_name[1]= "item2";

array_name[2]= "item3";

  • 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

Array properties

  • 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

Array methods

  • .length

  • .toString() : array to string with comma seperated

  • .pop() : remove the last item

  • .push() : add an item to tail

  • shift() : remove the first item and shift the rest to a lower index

  • 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

Array sort

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

  • .reverse(); : reverse an array

Numeric sort

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

Random sort

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

Fisher Yates Method - random sort

for (let i = array.length -1; i > 0; i--) {

let j = Math.floor(Math.random() * (i+1));

let k = array[i];

array[i] = array[j];

array[j] = k; }

Math.max() / Math.min()

Find max/min number in an array

function myArrayMax(arr) {

return Math.max.apply(null, arr);

}

function myArrayMin(arr) {

return Math.min.apply(null, arr);

}

Find Min/Max Js method

function myArrayMax(arr) {

let len = arr.length;

let max = -Infinity;

while (len--) {

if (arr[len] > max) {

max = arr[len];

}

}

return max;

}

Sort Object array

const cars = [

{type:"Volvo", year:2016},

{type:"Saab", year:2001},

{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){

let x = a.type.toLowerCase();

let y = b.type.toLowerCase();

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

if (x > y) {return 1;}

return 0; });

Array Iteration

  • .forEach(function) : call function once for each item

  • .map(function) : create a new array by execute the old one with function

  • .flatmap(function) ?

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

  • .keys() : returns an Array Iterator object with the keys of an array

  • .entries() : Create an Array Iterator, and then iterate over the key/value pairs

  • .includes() : check if an element is present in an array

  • Spread (...) : expands an iterable (like an array) into more elements

Array Const

  • An array declared with const cannot be reassigned

  • Element can be reasssigned

  • An array declared with const must be initialized when it is declared;

const cars;

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

  • Arrays declared with var can be initialized at any time

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

var cars;

  • Const : Block Scope - Redeclaring

  • Var : Block Scope - Redeclaring

Date Object

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

Dates are saved as number of milliseconds since January 01, 1970.

Displaying date

const d = new Date();

Default: date.toString();

More readable: date.toDateString();

UTC standard: date.toUTCString();

ISO standard: date.toISOString();

Date Format

Output format

ISO:

  • 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");

const d = new Date(msec);

Get Date Methods

  • 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

Set Date Methods

  • d.setFullYear(x);

  • d.setMonth(0-11);

  • d.setDate(1-31);

  • d.setHours(0-23);

  • d.setMinutes(0-59);

  • d.setSeconds(0-59);

Random

  • Math.random(); : returns a random number between 0 and 1

  • Math.floor(Math.random() * 10^n); : returns a random number

Boolean

Everything without a value is False

PreviousDay 34 - 9/10NextDay 36 - 11/10

Last updated 1 year ago