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 |
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
'use strict'; | |
// Data | |
// forEach | |
const info1 = { | |
owner: 'Sita Troy', | |
movements: [200, 450, -400, 3000, -650, -130, 70, 1300], | |
interestRate: 1.2, // % | |
pin: 1111, |
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
// 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 |
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
// 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); |
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
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); |
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
// Data | |
const account1 = { | |
owner: 'Bobby Khan', | |
movements: [2030, 450, -400, 3000, -650, -130, 70, 1300], | |
interestRate: 1.2, // % | |
pin: 11411, | |
}; | |
const account2 = { |
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
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); |
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
// 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 |
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
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); |
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
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); |