-
Notifications
You must be signed in to change notification settings - Fork 0
/
math.js
168 lines (133 loc) · 5.47 KB
/
math.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// Basic Definitions
// Integer: a whole number that is not a fraction
// Float/Floating point: approximate value for a decimal/fraction, as calculated by a computer
// Positive number: greater than zero
// Negative: less than zero
// Non-negative: greater than or equal to zero
// NaN: the only number that is not a number
// console.log(typeof 2) => 'number'
// console.log(typeof NaN) => 'number'
// console.log(typeof 'snake') => 'string'
// console.log('building' / 'house') => NaN
// + is the addition operator but in terms of strings, it is the concatenation operator
// let start = "Hello, ";
// let introduce = "my name is ";
// let name = "E.T.";
// let expression = start + introduce + name;
// console.log(expression);
// Multiplying strings (dividing strings, subtracting strings) will not work!
// These are mathematical operations! and they do not apply to strings
// let manyCats = "cat " * 5;
// console.log(manyCats); => NaN
// let ductTapePrice = 5;
// let amountOfDuctTape = 4;
// let total = ductTapePrice * amountOfDuctTape;
// if (amountOfDuctTape >= 3) {
// total = total - (total * 10) / 100;
// }
// console.log(total)
// console.log(`The price of ${amountOfDuctTape} duct tape rolls is ${total}`);
// PEMDAS
// Easy way to remember how to get a percentage of a number:
// - multiply the number by the percentage(number)
// - divide product by 100 (to get the percentage)
// total = total - (total * 10) / 100;
// console.log(total)
let ductTapePrice = 5;
let amountOfDuctTape = 4;
let percentage = 0.1;
let getDiscountedTotal = 1 - percentage;
let total = ductTapePrice * amountOfDuctTape;
if (amountOfDuctTape >= 3) {
// Multiplication assignment operator:
// - runs calculation and assigns the value at the same time for shorter code
total *= getDiscountedTotal;
// Line below does the same thing as the line above
// total = total * getDiscountedTotal;
}
// console.log(total)
// Math Library
let ourPrice = 5;
let competitorPrices = [2, 3, 4, 6, 7, 8];
let compPrices = [4,5,56,67,8,89]
const comparePriceRange = (prices) => {
let min = Math.min(...prices)
let max = Math.max(...prices)
let priceRange = max - min;
return priceRange;
};
// console.log(comparePriceRange(competitorPrices));
// console.log(comparePriceRange(compPrices));
const upperLimit = 10
// console.log( Math.ceil(Math.random() * 10) )
// Math.floor() => rounds down to the nearest integer
// Math.ceil() => rounds up to the next integer
// Math.round() => rounds to the NEAREST integer(up or down)
// Math.min() => takes numbers in, returns lowest number
// Math.max() => takes numbers in, returns highest number
// Math.random() => returns a float between 0 and 1
// Math.abs() => returns the absolute value of a positive or negative number
// Math.trunc() => returns the integer version of the number (gets rid of everything after the decimal point)
// Math.sqrt() => returns the square root of a number
// Math.pow(x, y) => takes two numbers in, returns the value of the first argument raised to the power of the second argument
// Polya's Problem Solving Methodology: Find the Median
// 1. Understand the Problem
// - what is a median? => middle number
// - is it a clean data set, or are there repeating numbers?
// - if the length of the data set is even, we take the average of the two middle numbers
// 2. Devise a Plan
// 3. Carry out the Plan
// 4. Revise for Edge Cases
const nums = [
14, 11, 16, 15, 13, 16, 15, 17, 19, 11, 12, 14, 19, 11, 15, 17, 11, 18, 12,
17, 12, 71, 18, 15, 12,
];
// 0 1 2 3 4
const five = [11, 12, 13, 14, 17]
const findTheMedian = (arr) => {
let medianIndex = null
// Sort the array from smallest to largest
const sortedArr = arr.sort()
// console.log(sortedArr)
// Remove duplicates to have a clean data set
// use a Set()?
const unique = new Set(sortedArr)
const uniqueArr = [...unique]
// console.log(uniqueArr)
// Check whether the length is odd or even
if(uniqueArr.length % 2 !== 0){
// If it's odd: find the value at the middle index(ex: 5 length) ex: Math.floor(5 / 2)
medianIndex = Math.floor(uniqueArr.length / 2)
// console.log(medianIndex)
} else {
// If it's even: find the middle two numbers and take the average: 6 => get average of: length divided by 2 AND length divided by 2 + 1
// 6 => 3,4 => 3+4=7 => 7/2 => 3.5
const firstMiddleIndex = uniqueArr.length / 2 - 1
const secondMiddleIndex = uniqueArr.length / 2
const realMedian = (uniqueArr[firstMiddleIndex] + uniqueArr[secondMiddleIndex]) / 2
return realMedian
}
// Return that number
return uniqueArr[medianIndex]
}
// console.log(findTheMedian(nums))
// console.log(findTheMedian(five))
const unsortedNums = [735, 123, 23, 87, 3, -2, 0, 546]
// Sort() w no callback function passed to it
// Only looks at the first value/digit in the number/string
// console.log(unsortedNums.sort())
// Function below uses a ternary, which is short hand for an if statement
// In a ternary, we evaulate the expression that comes before the question mark
// If the value is truthy, then we return what comes after the question mark
// If the value i falsy, then we return what comes after the colon
const func = (a, b) => a < b ? -1 : 1
console.log(unsortedNums.sort(func))
console.log(unsortedNums.sort((a, b) => a < b ? -1 : 1))
const func2 = (a, b) => {
if(a < b){
return - 1
} else {
return 1
}
}
console.log(unsortedNums.sort(func2))