-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
71 lines (58 loc) · 2.59 KB
/
script.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
const N0 = 500; // Initial quantity at n=0
const r = 1.6218; // Growth rate per iteration
let power_Cost = 0;
let power_Strength = N0;
let theme = localStorage.getItem('theme') || (window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light');
document.body.setAttribute('data-theme', theme);
function powerStrength() {
const n = parseInt(document.getElementById('cost').value);
if (isNaN(n) || n < 0 || n > 100) {
alert('Please enter a valid number between 0 and 100.');
return;
}
power_Cost = n;
const Nn = N0 * Math.exp(r * n);
const relativeStrength = Nn / N0;
power_Strength = relativeStrength;
const relativeStrengthFormatted = formatScientific(relativeStrength);
document.getElementById('power-strength').innerHTML = `This is ${relativeStrengthFormatted} times stronger than 0 PP`;
}
function calculatePower() {
const n = parseInt(document.getElementById('iteration').value);
if (isNaN(n) || n < power_Cost || n > 1000000) {
alert('Please enter a valid number between your power cost and 1000000.');
return;
}
const Nn = N0 * Math.exp(r * n);
const relativeStrength = (Nn / N0) / power_Strength;
const relativeStrengthFormatted = formatScientific(relativeStrength);
document.getElementById('relative-strength').innerHTML = `This is ${relativeStrengthFormatted} times stronger than your initial power.`;
}
function formatScientific(number) {
const exponent = Math.floor(Math.log10(number));
const mantissa = number / Math.pow(10, exponent);
return `${mantissa.toFixed(2)} * 10^${exponent}`;
}
function toggleTheme() {
theme = theme === 'light' ? 'dark' : 'light';
document.body.setAttribute('data-theme', theme);
document.getElementById('themeToggle').textContent = theme === 'light' ? 'Switch to Dark Theme' : 'Switch to Light Theme';
// Store the user's preference in local storage
localStorage.setItem('theme', theme);
}
document.addEventListener('DOMContentLoaded', (event) => {
document.body.setAttribute('data-theme', theme);
document.getElementById('themeToggle').textContent = theme === 'light' ? 'Switch to Dark Theme' : 'Switch to Light Theme';
});
// Add event listener for the Enter key on the input field
document.getElementById('cost').addEventListener('keydown', function(event) {
if (event.key === 'Enter') {
powerStrength();
}
});
// Add event listener for the Enter key on the input field
document.getElementById('iteration').addEventListener('keydown', function(event) {
if (event.key === 'Enter') {
calculatePower();
}
});