Skip to content

Commit 6cef5eb

Browse files
committed
i bEt yoU lIkE jAvAsCrIpT
1 parent c6b9405 commit 6cef5eb

File tree

4 files changed

+161
-0
lines changed

4 files changed

+161
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
<meta charset="UTF-8">
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7+
<title>Text Generator</title>
8+
<link rel="stylesheet" href="../../base.css">
9+
<style>
10+
body {
11+
min-height: 100vh;
12+
display: grid;
13+
align-items: center;
14+
}
15+
16+
.typer {
17+
margin: 0 auto;
18+
background: white;
19+
width: 500px;
20+
padding: 2rem;
21+
padding: 2rem;
22+
border-radius: 3px;
23+
display: grid;
24+
}
25+
26+
textarea {
27+
width: 100%;
28+
}
29+
</style>
30+
</head>
31+
32+
<body>
33+
34+
<div class="typer">
35+
<label for="sarcastic">
36+
<input type="radio" value="sarcastic" id="sarcastic" name="filter" checked>
37+
Sarcastic
38+
</label>
39+
<label for="funky">
40+
<input type="radio" value="funky" id="funky" name="filter">
41+
Funky
42+
</label>
43+
<label for="unable">
44+
<input type="radio" value="unable" id="unable" name="filter">
45+
Unable to Structure a Sentence
46+
</label>
47+
<textarea name="text">so I was thinking about going to the store.</textarea>
48+
<p class="result"></p>
49+
</div>
50+
51+
<script src="./text.js"></script>
52+
</body>
53+
54+
</html>
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// functions as arguments
2+
3+
const textarea = document.querySelector('[name="text"]');
4+
const result = document.querySelector('.result');
5+
const filterInputs = document.querySelectorAll('[name="filter"]');
6+
7+
/* eslint-disable */
8+
const funkyLetters = {
9+
'-': '₋', '!': 'ᵎ', '?': 'ˀ', '(': '⁽', ')': '₎', '+': '⁺', '=': '₌', '0': '⁰', '1': '₁', '2': '²', '4': '₄', '5': '₅', '6': '₆', '7': '⁷', '8': '⁸', '9': '⁹', a: 'ᵃ', A: 'ᴬ', B: 'ᴮ', b: 'ᵦ', C: '𝒸', d: 'ᵈ', D: 'ᴰ', e: 'ₑ', E: 'ᴱ', f: '𝒻', F: 'ᶠ', g: 'ᵍ', G: 'ᴳ', h: 'ʰ', H: 'ₕ', I: 'ᵢ', i: 'ᵢ', j: 'ʲ', J: 'ᴶ', K: 'ₖ', k: 'ₖ', l: 'ˡ', L: 'ᴸ', m: 'ᵐ', M: 'ₘ', n: 'ₙ', N: 'ᴺ', o: 'ᵒ', O: 'ᴼ', p: 'ᵖ', P: 'ᴾ', Q: 'ᵠ', q: 'ᑫ', r: 'ʳ', R: 'ᵣ', S: 'ˢ', s: 'ˢ', t: 'ᵗ', T: 'ₜ', u: 'ᵘ', U: 'ᵤ', v: 'ᵛ', V: 'ᵥ', w: '𝓌', W: 'ʷ', x: 'ˣ', X: 'ˣ', y: 'y', Y: 'Y', z: '𝓏', Z: 'ᶻ' };
10+
/* eslint-enable */
11+
12+
const filters = {
13+
sarcastic(letter, index) {
14+
if (index % 2) {
15+
return letter.toUpperCase();
16+
}
17+
return letter.toLowerCase();
18+
},
19+
funky(letter, index) {
20+
// first check if there is a letter in this case
21+
let funkyLetter = funkyLetters[letter];
22+
console.log(funkyLetter);
23+
if (!funkyLetter) {
24+
// then check for a lowercase version
25+
funkyLetter = funkyLetters[letter.toLowerCase()];
26+
}
27+
// if we still don't have something, just use the regular letter
28+
if (!funkyLetter) {
29+
funkyLetter = letter;
30+
}
31+
return funkyLetter;
32+
},
33+
unable(letter) {
34+
const random = Math.floor(Math.random() * 3);
35+
if (letter === ' ' && random === 2) {
36+
return '...';
37+
}
38+
return letter;
39+
},
40+
};
41+
42+
function handleInput(text) {
43+
const filter = document.querySelector('[name="filter"]:checked').value;
44+
const mod = Array.from(text)
45+
.map(filters[filter])
46+
.join('');
47+
result.textContent = mod;
48+
}
49+
50+
textarea.addEventListener('input', e => handleInput(e.target.value));
51+
52+
filterInputs.forEach(input =>
53+
input.addEventListener('input', () => handleInput(textarea.value))
54+
);
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
const textarea = document.querySelector('[name="text"]');
2+
const result = document.querySelector('.result');
3+
const filterInputs = Array.from(document.querySelectorAll('[name="filter"]'));
4+
5+
/* eslint-disable */
6+
const funkyLetters = {
7+
'-': '₋', '!': 'ᵎ', '?': 'ˀ', '(': '⁽', ')': '₎', '+': '⁺', '=': '₌', '0': '⁰', '1': '₁', '2': '²', '4': '₄', '5': '₅', '6': '₆', '7': '⁷', '8': '⁸', '9': '⁹', a: 'ᵃ', A: 'ᴬ', B: 'ᴮ', b: 'ᵦ', C: '𝒸', d: 'ᵈ', D: 'ᴰ', e: 'ₑ', E: 'ᴱ', f: '𝒻', F: 'ᶠ', g: 'ᵍ', G: 'ᴳ', h: 'ʰ', H: 'ₕ', I: 'ᵢ', i: 'ᵢ', j: 'ʲ', J: 'ᴶ', K: 'ₖ', k: 'ₖ', l: 'ˡ', L: 'ᴸ', m: 'ᵐ', M: 'ₘ', n: 'ₙ', N: 'ᴺ', o: 'ᵒ', O: 'ᴼ', p: 'ᵖ', P: 'ᴾ', Q: 'ᵠ', q: 'ᑫ', r: 'ʳ', R: 'ᵣ', S: 'ˢ', s: 'ˢ', t: 'ᵗ', T: 'ₜ', u: 'ᵘ', U: 'ᵤ', v: 'ᵛ', V: 'ᵥ', w: '𝓌', W: 'ʷ', x: 'ˣ', X: 'ˣ', y: 'y', Y: 'Y', z: '𝓏', Z: 'ᶻ'
8+
};
9+
/* eslint-enable */
10+
11+
const filters = {
12+
sarcastic(letter, index) {
13+
// if it is odd, it will return 1, and that is truthy, so this if statement will trip
14+
if (index % 2) {
15+
return letter.toUpperCase();
16+
}
17+
// if it is even, it will return zero and we will lowercase it
18+
return letter.toLowerCase();
19+
},
20+
funky(letter) {
21+
// first check if there is a funky letter for this case
22+
let funkyLetter = funkyLetters[letter];
23+
if (funkyLetter) return funkyLetter;
24+
// if there is not, check if there is a lowercase version
25+
funkyLetter = funkyLetters[letter.toLowerCase()];
26+
if (funkyLetter) return funkyLetter;
27+
// if there is nothing, return the regular letter
28+
return letter;
29+
},
30+
unable(letter) {
31+
const random = Math.floor(Math.random() * 3);
32+
if (letter === ' ' && random === 2) {
33+
return '...';
34+
}
35+
return letter;
36+
},
37+
};
38+
39+
function transformText(text) {
40+
// const filter = document.querySelector('[name="filter"]:checked').value;
41+
const filter = filterInputs.find(input => input.checked).value;
42+
// take the text, and loop over each letter.
43+
const mod = Array.from(text).map(filters[filter]);
44+
result.textContent = mod.join('');
45+
}
46+
47+
textarea.addEventListener('input', e => transformText(e.target.value));
48+
49+
filterInputs.forEach(input =>
50+
input.addEventListener('input', () => {
51+
transformText(textarea.value);
52+
})
53+
);

exercises/55 - Sarcastic Text/text.js

Whitespace-only changes.

0 commit comments

Comments
 (0)