Constnium/public/js/main.js

292 lines
11 KiB
JavaScript
Raw Permalink Normal View History

2022-06-23 12:40:21 +02:00
window.addEventListener("load", () => main());
// Get input area
const input = document.getElementById("firstname");
2022-06-23 12:40:21 +02:00
const main = () => {
2022-06-23 12:40:21 +02:00
// Reset content of the input
input.value = "";
2022-06-24 10:01:59 +02:00
// Create the paragraph of the first name
const string_firstname = document.createElement("div");
2022-06-23 16:45:25 +02:00
string_firstname.className = "firstname-constant-string";
// Create the paragraph of the constant
2022-06-24 10:01:59 +02:00
const double_firstname = document.createElement("p");
2022-06-23 16:45:25 +02:00
double_firstname.className = "firstname-constant-double";
// Add the elements to the page
2022-06-25 01:38:15 +02:00
const firstname_constant = document.getElementsByClassName("firstname-constant").item(0);
firstname_constant.append(string_firstname);
firstname_constant.append(double_firstname);
2022-06-23 16:45:25 +02:00
// Fill informations with placeholder
2022-06-24 10:01:59 +02:00
updateConst();
2022-06-23 12:40:21 +02:00
// Call callback when editing the input
input.addEventListener("input", updateConst);
2022-06-23 12:40:21 +02:00
};
2022-06-24 10:01:59 +02:00
// Callback: called when first name is changed
const updateConst = () => {
const firstname = input.value.trim().toLowerCase();
2022-06-25 19:23:56 +02:00
if (firstname.length) {
2022-06-25 01:38:15 +02:00
const data = getConst(firstname);
2022-06-24 10:01:59 +02:00
setString(firstname.replace(/^\w/, (c) => c.toUpperCase()), data.infos);
2022-06-23 16:45:25 +02:00
getDouble().textContent = data.const;
} else {
2022-06-24 10:01:59 +02:00
defaultInfos();
2022-06-23 13:56:52 +02:00
}
2022-06-23 12:40:21 +02:00
};
2022-06-24 10:01:59 +02:00
// Define the string who show the firstname
const setString = (firstname = String, data = Object) => {
const string_div = document.getElementsByClassName("firstname-constant-string").item(0);
// Reset the string data
string_div.innerHTML = "";
// For each letter of the first name
2022-06-25 19:54:15 +02:00
[...firstname].forEach(letter_value => {
2022-06-24 10:01:59 +02:00
// Create the letter
const letter = document.createElement("p");
// Fill elements with data
letter.textContent = letter_value;
2022-06-25 19:40:55 +02:00
// Check if the letter is inside part of the constant
if (Object.keys(data).includes(clearText(letter_value))) {
// Info about a specific constant
const infos = document.createElement("span");
Object.keys(data[clearText(letter_value)]).forEach(key => {
const link = document.createElement("a");
link.href = data[clearText(letter_value)][key];
link.innerText = key;
infos.append(link);
});
// Change parameter of the info card
infos.hidden = true;
infos.style.position = "absolute";
// Link info to the letter
letter.append(infos);
// Add behaviour when hovering over the letter
letter.addEventListener("mouseenter", (e) => e.target.firstElementChild.hidden = false);
letter.addEventListener("mouseleave", (e) => e.target.firstElementChild.hidden = true);
}
2022-06-24 10:01:59 +02:00
// Add the letter
string_div.append(letter);
});
2022-06-25 19:23:56 +02:00
};
2022-06-23 16:45:25 +02:00
// Get the paragraph of the constant
const getDouble = () => {
2022-06-24 10:01:59 +02:00
return document.getElementsByClassName("firstname-constant-double").item(0);
2022-06-25 19:23:56 +02:00
};
// Set the paragraph of the name to the placeholder
2022-06-24 10:01:59 +02:00
const defaultInfos = () => {
// Retrieve the constant from the default first name
2022-06-25 01:38:15 +02:00
const data = getConst(input.placeholder);
2022-06-24 10:01:59 +02:00
// Define the string
setString(input.placeholder, data.infos);
2022-06-23 16:45:25 +02:00
2022-06-24 10:01:59 +02:00
// Define the constant
getDouble().textContent = data.const;
2022-06-25 19:23:56 +02:00
};
2022-06-25 01:38:15 +02:00
// Remove accent and lower the string
const clearText = (string = String) => {
return string.normalize('NFD').replace(/[\u0300-\u036f]/g, "").toLowerCase();
2022-06-25 19:23:56 +02:00
};
2022-06-25 01:38:15 +02:00
const getConst = (letters = String) => {
// Turn letters to an array
2022-06-25 19:54:15 +02:00
const letters_array = [...clearText(letters)];
2022-06-25 01:38:15 +02:00
2022-06-24 10:01:59 +02:00
// Store constants of each letters of the first name
const const_data = [];
2022-06-23 12:40:21 +02:00
// Store some constants info
2022-06-24 10:01:59 +02:00
const const_infos = {};
2022-06-23 12:40:21 +02:00
// Assign each letter to a constant, fallback to 1 if no one has been found
let position = 0;
2022-06-25 01:38:15 +02:00
letters_array.forEach(letter => {
position++;
switch (letter) {
case "a":
2022-06-25 19:23:56 +02:00
const_data.push(1.2824271291006226 ** position);
const_infos[letter] = {
2022-06-25 01:38:15 +02:00
"Wikipedia": "https://en.wikipedia.org/wiki/Glaisher%E2%80%93Kinkelin_constant"
2022-06-23 13:49:39 +02:00
};
break;
case "b":
const_data.push(1.456074 ** position);
const_infos[letter] = {
2022-06-25 01:38:15 +02:00
"Wikipedia": "https://en.wikipedia.org/wiki/Backhouse%27s_constant"
2022-06-23 13:49:39 +02:00
};
break;
case "c":
const_data.push(299792458 ** position);
const_infos[letter] = {
2022-06-25 01:38:15 +02:00
"Wikipedia": "https://en.wikipedia.org/wiki/Speed_of_light"
2022-06-23 13:49:39 +02:00
};
break;
2022-06-23 13:49:39 +02:00
case "d":
2022-06-25 19:23:56 +02:00
const_data.push(0.7390851332151606 ** position);
const_infos[letter] = {
2022-06-25 01:38:15 +02:00
"Wikipedia": "https://en.wikipedia.org/wiki/Dottie_number"
2022-06-23 13:49:39 +02:00
};
break;
case "e":
2022-06-25 19:23:56 +02:00
const_data.push(2.718281828459045 ** position);
const_infos[letter] = {
2022-06-25 01:38:15 +02:00
"Wikipedia": "https://en.wikipedia.org/wiki/E_(mathematical_constant)"
2022-06-23 13:49:39 +02:00
};
break;
case "f":
2022-06-25 19:23:56 +02:00
const_data.push(2.807770242028519 ** position);
const_infos[letter] = {
2022-06-25 01:38:15 +02:00
"Wikipedia": "https://en.wikipedia.org/wiki/Frans%C3%A9n%E2%80%93Robinson_constant"
2022-06-23 13:49:39 +02:00
};
break;
case "g":
2022-06-25 19:23:56 +02:00
const_data.push(0.8346268416740731 ** position);
const_infos[letter] = {
2022-06-25 01:38:15 +02:00
"Wikipedia": "https://en.wikipedia.org/wiki/Gauss%27s_constant"
2022-06-23 13:49:39 +02:00
};
break;
case "h":
const_data.push(6.62607015e-34 ** position);
const_infos[letter] = {
2022-06-25 01:38:15 +02:00
"Wikipedia": "https://en.wikipedia.org/wiki/Planck_constant"
2022-06-23 13:49:39 +02:00
};
break;
case "i":
2022-06-25 19:23:56 +02:00
const_data.push(0.20787957635076190 ** position);
const_infos[letter] = {
2022-06-25 19:42:10 +02:00
"OEIS": "https://oeis.org/A049006"
2022-06-23 13:49:39 +02:00
};
break;
case "j":
const_data.push(5.5208e27 ** position);
const_infos[letter] = {
2022-06-25 01:38:15 +02:00
"Wikipedia": "https://en.wikipedia.org/wiki/J/psi_meson"
2022-06-23 13:49:39 +02:00
};
break;
case "k":
const_data.push((-273.15) ** position);
const_infos[letter] = {
2022-06-25 01:38:15 +02:00
"Wikipedia": "https://en.wikipedia.org/wiki/Kelvin"
2022-06-23 13:49:39 +02:00
};
break;
case "l":
2022-06-25 19:23:56 +02:00
const_data.push(0.97027011439203392 ** position);
const_infos[letter] = {
2022-06-25 01:38:15 +02:00
"Wikipedia": "https://en.wikipedia.org/wiki/Lochs%27s_theorem"
2022-06-23 13:49:39 +02:00
};
break;
case "m":
2022-06-25 19:23:56 +02:00
const_data.push(0.2614972128476427 ** position);
const_infos[letter] = {
2022-06-25 01:38:15 +02:00
"Wikipedia": "https://en.wikipedia.org/wiki/Meissel%E2%80%93Mertens_constant"
2022-06-23 13:49:39 +02:00
};
break;
case "n":
const_data.push(6.02214076e+23 ** position);
const_infos[letter] = {
2022-06-25 01:38:15 +02:00
"Wikipedia": "https://en.wikipedia.org/wiki/Avogadro_constant"
2022-06-23 13:49:39 +02:00
};
break;
case "o":
2022-06-25 19:23:56 +02:00
const_data.push(1.66168794963359412 ** position);
const_infos[letter] = {
2022-06-25 01:38:15 +02:00
"Wikipedia": "https://en.wikipedia.org/wiki/Somos%27_quadratic_recurrence_constant"
2022-06-23 13:49:39 +02:00
};
break;
case "p":
2022-06-25 19:23:56 +02:00
const_data.push(2.295587149392638 ** position);
const_infos[letter] = {
2022-06-25 01:38:15 +02:00
"Wikipedia": "https://en.wikipedia.org/wiki/Universal_parabolic_constant"
2022-06-23 13:49:39 +02:00
};
break;
2022-06-23 13:49:39 +02:00
case "q":
const_data.push(1.8755459e-18 ** position);
const_infos[letter] = {
2022-06-25 01:38:15 +02:00
"Wikipedia": "https://en.wikipedia.org/wiki/Planck_units#History_and_definition"
2022-06-23 13:49:39 +02:00
};
break;
case "r":
2022-06-25 19:23:56 +02:00
const_data.push(2625374126407687e2 ** position);
const_infos[letter] = {
2022-06-25 01:38:15 +02:00
"Wikipedia": "https://en.wikipedia.org/wiki/Heegner_number#Almost_integers_and_Ramanujan.27s_constant"
2022-06-23 13:49:39 +02:00
};
break;
case "s":
2022-06-25 19:23:56 +02:00
const_data.push(0.1878596424620671 ** position);
const_infos[letter] = {
2022-06-25 01:38:15 +02:00
"Wikipedia": "https://en.wikipedia.org/wiki/MRB_constant"
2022-06-23 13:49:39 +02:00
};
break;
2022-06-23 13:49:39 +02:00
case "t":
2022-06-25 19:23:56 +02:00
const_data.push(1.927561975482925 ** position);
const_infos[letter] = {
2022-06-25 01:38:15 +02:00
"Wikipedia": "https://en.wikipedia.org/wiki/Generalizations_of_Fibonacci_numbers#Tetranacci_numbers"
2022-06-23 13:49:39 +02:00
};
break;
case "u":
2022-06-25 19:23:56 +02:00
const_data.push(1.8477590650225735 ** position);
const_infos[letter] = {
2022-06-25 01:38:15 +02:00
"Wikipedia": "https://en.wikipedia.org/wiki/Self-avoiding_walk"
2022-06-23 13:49:39 +02:00
};
break;
case "v":
2022-06-25 19:23:56 +02:00
const_data.push(0.4221577331158266 ** position);
const_infos[letter] = {
2022-06-25 01:38:15 +02:00
"Wikipedia": "https://en.wikipedia.org/wiki/Reuleaux_tetrahedron"
2022-06-23 13:49:39 +02:00
};
break;
2022-06-23 13:49:39 +02:00
case "w":
2022-06-25 19:23:56 +02:00
const_data.push(2.0945514815423265 ** position);
const_infos[letter] = {
2022-06-25 01:38:15 +02:00
"OEIS": "https://oeis.org/A007493"
2022-06-23 13:49:39 +02:00
};
break;
case "x":
const_data.push(1.0021e-13 ** position);
const_infos[letter] = {
2022-06-25 01:38:15 +02:00
"Wikipedia": "https://en.wikipedia.org/wiki/X_unit"
2022-06-23 13:49:39 +02:00
};
break;
case "y":
2022-06-25 19:23:56 +02:00
const_data.push(0.5772156649015328 ** position);
const_infos[letter] = {
2022-06-25 01:38:15 +02:00
"Wikipedia": "https://en.wikipedia.org/wiki/Euler%27s_constant"
2022-06-23 13:49:39 +02:00
};
break;
case "z":
const_data.push(376.730313668 ** position);
const_infos[letter] = {
2022-06-25 01:38:15 +02:00
"Wikipedia": "https://en.wikipedia.org/wiki/Impedance_of_free_space"
2022-06-23 13:49:39 +02:00
};
break;
2022-06-23 12:40:21 +02:00
default: // By default, equals to 1
const_data.push(1);
2022-06-25 19:23:56 +02:00
}
});
2022-06-23 12:40:21 +02:00
return {
2022-06-24 10:01:59 +02:00
// Multiply all the constants together
"const": const_data.reduce((x, y) => x * y),
"infos": const_infos
};
2022-06-23 12:40:21 +02:00
};