-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumber-exercises.js
More file actions
39 lines (26 loc) · 866 Bytes
/
number-exercises.js
File metadata and controls
39 lines (26 loc) · 866 Bytes
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
//Currency formatter
const formatCurrency = amount => {
if (!Number.isFinite(amount)) return "Invalid Amount";
return `${amount.toFixed(2)}`
}
console.log(formatCurrency(12.3456)) //1234
console.log(formatCurrency(Infinity))
//Safe division
const safeDivide = (a, b) => {
a = a ?? 0; // nullish coalescing operator
b = b ?? 1;
return (b == 0) ? Infinity : a / b;
}
console.log(safeDivide(10, 2));
console.log(safeDivide(10, 0));
console.log(safeDivide(null, 2));
//Random Hex Color Generator
const getRandomHexColor = () => {
const color = Math.floor(Math.random() * (0xFFFFFF + 1)).toString(16).padStart(6, '0')
return `#${color}`
}
console.log(getRandomHexColor())
console.log(getRandomHexColor())
console.log(getRandomHexColor())
console.log(getRandomHexColor())
console.log(getRandomHexColor())