Skip to content

Instantly share code, notes, and snippets.

@kironroy
kironroy / numbers.js
Last active July 7, 2023 20:32
numbers, numbers
// 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
@kironroy
kironroy / arr_pracr.js
Created July 4, 2023 18:15
Array practice
'use strict';
// Data
// forEach
const info1 = {
owner: 'Sita Troy',
movements: [200, 450, -400, 3000, -650, -130, 70, 1300],
interestRate: 1.2, // %
pin: 1111,
@kironroy
kironroy / array_methods_list.js
Created July 3, 2023 17:10
Array Methods List
// what do i want from this method?
// mutate an array, create a new array
// array includes
// array transform
// methods that MUTATE original array
// + add to original: .push, ,.unshift (start)
// - remove from original: .pop (end), .shift, .splice
// others: .reverse, .sort, .fill
@kironroy
kironroy / random_arr.js
Created July 3, 2023 16:43
random js array
// try 100 random dice rolls
// create array of 100 numbers (1 to 100)
const diceRollsArr = Array.from({length: 100}, (_, i) => i + 1);
console.log(Array.isArray(diceRollsArr));
// sort array (random)
const rdmDiceRollsArr = diceRollsArr.sort((a, b) => 0.5 - Math.random());
console.log(diceRollsArr);
@kironroy
kironroy / sorting.js
Last active July 3, 2023 15:59
sorting scripts
const movements = [200, 450, -400, 3000, -650, -130, 70, 1300];
// descending
movements.sort((a, b) => {
if (a > b) return 1;
if (b > a) return -1;
});
console.log(movements);
@kironroy
kironroy / map.js
Last active June 29, 2023 01:36
map and multi-dim arrays
// Data
const account1 = {
owner: 'Bobby Khan',
movements: [2030, 450, -400, 3000, -650, -130, 70, 1300],
interestRate: 1.2, // %
pin: 11411,
};
const account2 = {
@kironroy
kironroy / includes_some.js
Last active June 28, 2023 23:57
includes/some
const movements3 = [200, 450, -400, 3000, -650, -130, 70, 1300];
console.log(movements3);
// equality
console.log(movements3.includes(-130));
// some condition
const anyDeposits = movements3.some(mov => mov > 1500);
console.log(anyDeposits);
// https://stackoverflow.com/questions/6866196/javascript-multiply-and-sum-two-arrays
// (4*2+3*3+4*3+5*1).
const arr1 = [2, 3, 4, 5];
const arr2 = [4, 3, 3, 1];
console.log(arr1.reduce((r, a, i) => r + a * arr2[i], 0));
// 34
// https://www.w3schools.com/ai/ai_matrices.asp
@kironroy
kironroy / find.js
Created June 17, 2023 22:40
find method
const movements2 = [200, 450, -400, 3000, -650, -130, 70, 1300];
const firstWithdrawal = movements2.find(mov => mov < 0);
console.log(movements2);
console.log(firstWithdrawal);
// array of objects is a common data structure
console.log(accounts);
@kironroy
kironroy / dogHuman.js
Last active June 16, 2023 18:11
map, filter, reduce in action
const agesOneArr = [5, 2, 4, 1, 15, 8, 3];
const agesTwoArr = [16, 6, 10, 5, 6, 1, 4];
const calcAverageHumanAge = function (ages) {
const dogToHumanYrs = ages.map((age, i) =>
age <= 2 ? 2 * age : 16 + age * 4
);
const eighteenOrOlder = dogToHumanYrs.filter(age => age > 18);
console.log(eighteenOrOlder);