-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathperfect-match.js
97 lines (75 loc) · 2.91 KB
/
perfect-match.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
function handleEnterKey(event) {
if (event.key === "Enter") {
document.querySelector(".button").click();
}
}
document.getElementById('name1').addEventListener('keypress', handleEnterKey);
document.getElementById('name2').addEventListener('keypress', handleEnterKey);
function calculateCompatibility() {
const name1 = document.getElementById('name1').value.trim().toLowerCase();
const name2 = document.getElementById('name2').value.trim().toLowerCase();
const warningMessage = document.getElementById('warning-message');
if (!name1 || !name2) {
warningMessage.textContent = 'Enter two names, dummy!';
return;
} else {
warningMessage.textContent = '';
}
const combinedNames = name1 + name2;
const counts = {
L: (combinedNames.match(/l/g) || []).length,
O: (combinedNames.match(/o/g) || []).length,
V: (combinedNames.match(/v/g) || []).length,
E: (combinedNames.match(/e/g) || []).length,
S: (combinedNames.match(/s/g) || []).length
};
const values = [
counts.L,
counts.O,
counts.V,
counts.E,
counts.S
];
let working = values;
let steps = [];
while (working.length > 2) {
const nextStep = [];
for (let i = 0; i < working.length - 1; i++) {
const sum = working[i] + working[i + 1];
const digits = String(sum).split('').map(Number);
nextStep.push(...digits);
}
steps.push(working);
working = nextStep;
if (working.length === 3 && working.join('') === '100') {
break;
}
}
steps.push(working);
const compatibility = parseInt(working.join(''), 10);
document.getElementById('result').textContent = `Compatibility: ${compatibility}%`;
const workingOut = generateWorkingOut(values, steps);
document.getElementById('details').innerHTML = workingOut;
document.getElementById('expandable-section').style.display = 'block';
}
function generateWorkingOut(values, steps) {
let treeHTML = '<div class="working-out">';
treeHTML += '<div class="tree-row">L O V E S</div>';
if (steps.length > 0) {
treeHTML += `<div class="tree-row">${steps[0].join(' ')}</div>`;
}
for (let i = 1; i < steps.length; i++) {
treeHTML += '<div class="tree-line"></div>';
treeHTML += `<div class="tree-row">${steps[i].join(' ')}</div>`;
}
treeHTML += '</div>';
return treeHTML;
}
function toggleDetails() {
const detailsDiv = document.getElementById('details');
const showDetailsSpan = document.querySelector('.show-details');
const isHidden = detailsDiv.style.display === 'none' || !detailsDiv.style.display;
detailsDiv.style.display = isHidden ? 'block' : 'none';
showDetailsSpan.textContent = isHidden ? 'Hide working out' : 'Show working out';
document.body.style.overflowY = isHidden ? 'auto' : 'hidden';
}