forked from AustinCodingAcademy/JS211_PigLatinProject
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDOM.js
More file actions
55 lines (49 loc) · 1.49 KB
/
DOM.js
File metadata and controls
55 lines (49 loc) · 1.49 KB
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
let value = "";
let pigLatinResult = document.getElementById("pigLatinResult");
let englishString = document.getElementById("stringInput");
//trims value put into the text area
const storeString = (valA) => {
value = valA.trim();
};
//clear the data in both text areas
const clearData = () => {
let clearInput = (englishString.value = "");
let clearResult = (pigLatinResult.value = "");
return clearInput, clearResult;
};
//translate english text and display in pig latin result then replace if new word exists
const displayTranslation = (translatedWord) => {
let newDiv = pigLatinResult;
newDiv.value = translatedWord;
let previousWord = pigLatinResult;
let parentDiv = previousWord.parentNode;
parentDiv.replaceChild(newDiv, previousWord);
};
//split the word and then add pigLatin letters and order to join them
const splitString = () => {
str = value
.split(" ")
.map((word) => {
return pigLatin(word);
})
.join(" ");
return displayTranslation(str);
};
//pigLatin translation
const pigLatin = (word) => {
let vowels = ["a", "e", "i", "o", "u"];
let finalWord = "";
let cleanWord = word.toLowerCase();
if (vowels.indexOf(cleanWord[0]) > -1) {
finalWord = cleanWord + "way";
return finalWord;
} else {
let firstMatch = cleanWord.match(/[aeiou]/g) || 0;
let vowelIndex = cleanWord.indexOf(firstMatch[0]);
finalWord =
cleanWord.substring(vowelIndex) +
cleanWord.substring(0, vowelIndex) +
"ay";
return finalWord;
}
};