-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumbers.js
More file actions
136 lines (104 loc) · 4.56 KB
/
Copy pathnumbers.js
File metadata and controls
136 lines (104 loc) · 4.56 KB
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
// ── NUMBERS IN JAVASCRIPT ─────────────────
// Unlike Python → JS has only ONE number type
// integers and decimals are both just "number"!
// ── 1. BASIC NUMBERS ──────────────────────
let integer = 100
let decimal = 9.99
let negative = -50
let big = 10000
console.log(typeof integer) //number
console.log(typeof decimal) //number
// both are just "number" in JS!
// ── 2. BASIC MATH ─────────────────────────
console.log(10 +5) //15 addition
console.log(10 - 5) // 5 subtraction
console.log(10 / 3) //3.333... division
console.log(10 * 5) //50 multiplication
console.log(10 % 3) //1 remainder
console.log(2 ** 10) // 1024 power
// ── 3. MATH OBJECT ────────────────────────
// JS has built in Math toolbox
// like import math in Python!
console.log(Math.PI) //3.14159...
console.log(Math.round(4.6)) // 5 round to nearest
console.log(Math.round(4.4)) // 4
console.log(Math.floor(4.9)) // 4 always round Down
console.log(Math.ceil(4.1)) // 5 always round Up
console.log(Math.abs(-50)) //50 always positive
console.log(Math.sqrt(16)) // 4 squqre root
console.log(Math.pow(2, 10)) //1024 power
console.log(Math.max(5, 10, 3, 8)) // 10 highest
console.log(Math.min(5, 10, 3, 8)) // 3 lowest
// ── 4. RANDOM NUMBERS ─────────────────────
// Math.random() → gives decimal between 0 and 1
console.log(Math.random()) // 0.7823... random!
// random number between 1 and 10
let random = Math.floor(Math.random() * 10) + 1
console.log(random) // random 1-10!
// random number between 1 and 100
let random100 = Math.floor(Math.random() * 100) + 1
console.log(random100) // random 1-100!
// ── 5. NUMBER PROBLEMS ────────────────────
// JS quirk! watch out for this!
console.log(0.1 + 0.2) // 0.30000000000000004 😅
// floating point issue! common in all languages!
// fix it:
console.log((0.1 + 0.2).toFixed(2)) // 0.30 ✅
// ── 6. CONVERTING STRINGS TO NUMBERS ──────
// very useful when taking user input!
let strNumber = "42"
console.log(typeof strNumber) // string
let converted = Number(strNumber)
console.log(typeof converted) // number
console.log(converted + 8) // 50 ✅
// parseInt → convert to integer
console.log(parseInt("42.9")) // 42 (removes decimal!)
// parseFloat → convert to decimal
console.log(parseFloat("42.9")) // 42.9 ✅
// what happens with invalid conversion?
console.log(Number("hello")) // NaN!
console.log(Number("123abc")) // NaN!
// ── 7. NaN (Not a Number) ─────────────────
// NaN = result of invalid math operation
console.log(10 / "hello") // NaN
console.log(Math.sqrt(-1)) // NaN
// check if something is NaN
console.log(isNaN(10)) // false → it IS a number
console.log(isNaN("hello")) // true → NOT a number
console.log(isNaN(NaN)) // true
// ── 8. toFixed() ──────────────────────────
// control decimal places!
// very useful for prices, calculations!
let price = 9.99999
console.log(price.toFixed(2)) // 10.00
console.log(price.toFixed(0)) // 10
let pi = Math.PI
console.log(pi.toFixed(2)) // 3.14
console.log(pi.toFixed(4)) // 3.1416
// ── REAL WORLD USES ───────────────────────
// 1. SHOPPING CART
let itemPrice = 299.99
let quantity = 3
let discount = 0.10 // 10% discount
let total = itemPrice * quantity
let discountAmount = total * discount
let finalPrice = total - discountAmount
console.log(`Item price: Rupee ${itemPrice}`)
console.log(`Quantity: ${quantity}`)
console.log(`Total: Rupee ${total.toFixed(2)}`)
console.log(`Discount: Rupee ${discountAmount.toFixed(2)}`)
console.log(`Final price: Rupee ${finalPrice.toFixed(2)}`)
// 2. RANDOM DICE GAME
let dice1 = Math.floor(Math.random() * 6) + 1
let dice2 = Math.floor(Math.random() * 6) + 1
let diceTotal = dice1 + dice2
console.log(`Dice 1: ${dice1}`)
console.log(`Dice 2: ${dice2}`)
console.log(`Total: ${diceTotal}`)
if (diceTotal === 12) {
console.log("That's a Roll baby! []~( ̄▽ ̄)~*")
} else if (diceTotal >= 10) {
console.log("Good Shot d=====( ̄▽ ̄*)b")
} else {
console.log("try again....(ง •_•)ง")
}