-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpali4.html
74 lines (70 loc) · 2.17 KB
/
pali4.html
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
<!DOCTYPE html>
<html lang="fi">
<head>
<meta charset="UTF-8">
<title>Palindromi</title>
<script>
if (window.self === window.top) { // lokaali ajo
window.jsframedata = { params: {
inputChars: 8,
minChars: 6,
labelText: 'Anna palindromi, jossa vähintään ${count} kirjainta:'
} };
document.addEventListener('DOMContentLoaded', () => onInit() );
}
</script>
</head>
<body>
<div id="container">
<p>
<label id="label" for="word">Anna sana:</label>
<input id="word" type="text" name="word">
<span id="icon"></span>
</p>
</div>
<script>
function onInit() {
const container = document.getElementById('container');
new Pali(container, window.jsframedata);
}
class Pali {
constructor(container, data) {
this.settings = {
inputChars: 10, // input alueen leveys
minChars: 0, // sanassa tarvittavien merkkien minimimäärä
labelText: "Anna sana:", // labelin teksti
}
if (data) Object.assign(this.settings, data.params);
this.container = container;
this.createContent();
}
createContent() {
const s = this.settings;
this.input = this.container.querySelector("#word");
this.icon = this.container.querySelector("#icon");
const label = this.container.querySelector("#label");
label.textContent = s.labelText.replace('${count}', s.minChars);
this.input.style.width = s.inputChars + 'ch';
this.input.addEventListener('input', () => this.checkPalindrome());
}
checkPalindrome() {
const word = this.input.value;
let isPalindrome = true;
for (let i = 0, j = word.length - 1; i < j; i++, j--) {
if (word[i] !== word[j]) {
isPalindrome = false;
break;
}
}
if (isPalindrome && this.settings.minChars <= word.length) {
this.icon.textContent = '✔';
this.icon.style.color = 'green';
} else {
this.icon.textContent = '✘';
this.icon.style.color = 'red';
}
}
}
</script>
</body>
</html>