Last active
July 7, 2023 20:32
-
-
Save kironroy/8b662731d0a5d1062ba3b7381e5b9343 to your computer and use it in GitHub Desktop.
numbers, numbers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// All numbers are represented as floating point numbers in JS | |
// Numbers stored in binary format 64 bit | |
// In binary form fractions are hard to represent | |
console.log(23 === 23.0); | |
console.log(0.1 + 0.2 === 0.3); // =( | |
// conversion | |
console.log(Number('23')); | |
console.log(+'23'); // type coercion | |
// parsing, parse number from a string | |
console.log(Number.parseInt(' 30px', 10)); | |
console.log(Number.parseFloat(' 2.5rem')); | |
console.log(Number.isNaN(20)); | |
console.log(Number.isNaN('20')); | |
console.log(Number.isNaN(+'20X')); | |
console.log(Number.isNaN(23 / 0)); | |
// isFinite method should be used to check if any value is a number | |
console.log(Number.isFinite(20)); | |
// math functions | |
console.log(Math.sqrt(25) === 25 ** (1 / 2)); | |
// random integer | |
console.log(Math.trunc(Math.random() * 6) + 1); | |
const randomInt = (min, max) => | |
Math.floor(Math.random() * (max - min) + 1) + min; | |
console.log(randomInt(-10, 20)); | |
// rounding integers | |
console.log(Math.round(23.3)); | |
console.log(Math.round(23.3)); | |
console.log(Math.ceil(23.3)); | |
console.log(Math.ceil(23.3)); | |
console.log(Math.floor(23.3)); | |
console.log(Math.floor(23.3)); | |
console.log(Math.trunc(-23.3)); | |
// floor gets absolute value | |
console.log(Math.floor(-23.3)); | |
// remainder | |
console.log(5 % 2); | |
console.log(8 % 2); | |
// even - odd f(x) | |
const isEven = n => n % 2 === 0; | |
console.log(isEven(8)); | |
console.log(isEven(25)); | |
console.log(isEven(514)); | |
labelBalance.addEventListener('click', function () { | |
[...document.querySelectorAll('.movements__row')].forEach(function (row, i) { | |
if (i % 2 === 0) row.style.backgroundColor = 'orangered'; | |
if (i % 3 === 0) row.style.backgroundColor = 'blue'; | |
}); | |
}); | |
// numeric separators | |
// 287,460,000,000 dia of solar system | |
const diameter = 287_460_000_000; | |
console.log(diameter); | |
const price = 345_99; | |
console.log(price); | |
const transferFee = 15_00; | |
const transferFee2 = 1_500; | |
const PI = 3.14_15; // pointless | |
console.log(PI); | |
console.log(Number('23000')); | |
console.log(Number('23_000')); // NaN | |
// BigInt | |
const biggestNumInJS = 2 ** 53 - 1; | |
console.log(2 ** 53 - 1); // 9007199254740991 | |
console.log(2 ** 53 + 2); // 9007199254740994 // won't calculate! | |
console.log(biggestNumInJS); | |
console.log(Number.MAX_SAFE_INTEGER); | |
// n will make it Big Int | |
console.log(12345678191202839102931341n); | |
console.log(BigInt(12345678191202839102931341)); | |
// Operations | |
console.log(10000n + 10000n); | |
console.log(10000n + 10000n); | |
const huge = 12344567889012334556n; | |
const num = 22; | |
// won't work | |
// console.log(huge * num); | |
// will work | |
console.log(huge * BigInt(num)); | |
// exceptions | |
console.log(20n > 13); | |
console.log(20n === 20); // false, === doesn't do type coercion | |
console.log(typeof 20n); | |
console.log(20n == '20'); | |
console.log(huge + ' is REALLY big'); | |
// divisions | |
console.log(11n / 3n); // cutoff decimal | |
// dates | |
// create a date | |
const now = new Date(); | |
console.log(now); | |
console.log(new Date('Jul 07 2023 13:22:25')); | |
console.log(new Date('December 24, 2015')); | |
console.log(new Date(account1.movementsDates[0])); | |
console.log(new Date(2037, 10, 19, 15, 23, 5)); | |
console.log(new Date(2037, 10, 31)); // auto correct Dec 01 | |
console.log(new Date(0)); | |
// working with dates | |
const future = new Date(2037, 10, 19, 15, 23, 5); | |
console.log(future); | |
console.log(future.getFullYear()); | |
console.log(future.getMonth()); | |
console.log(future.getDay()); | |
console.log(future.getHours()); | |
console.log(future.getMinutes()); | |
console.log(future.getSeconds()); | |
console.log(future.toISOString()); | |
console.log(future.getTime()); | |
console.log(new Date(2142285785000)); | |
console.log(Date.now()); // timestamp |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment