-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalgorithm6.html
57 lines (40 loc) · 1.37 KB
/
algorithm6.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
<!DOCTYPE html>
<html>
<body>
<script>
//https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/pig-latin
//Pig Latin is a way of altering English Words. The rules are as follows:
//- If a word begins with a consonant take the first consonant or consonant cluster, move it to the end of the word, and add "ay" to it.
//- If a word begins with a vowel, just add "way" at the end.
function translatePigLatin(str) {
var newStr = str.toLowerCase();
var vowelPos = newStr.search(/[aeiou]/);
// a word begins with a vowel
if (vowelPos == 0) {
return newStr.concat("way");
}
// a word begins with a consonant
var left = newStr;
var right = "";
if (vowelPos > 0) {
left = newStr.substr(0, vowelPos);
right = newStr.substr(vowelPos);
}
// for (var i=1; i<newStr.length; i++) {
// if (vow.indexOf(newStr[i])>=0) {
// firstCons = newStr.slice(0,i);
// last = newStr.slice(i,-1);
// break;
// }
// }
return `${right}${left}ay`;
}
//tests
console.log(translatePigLatin("consonant"));
console.log(translatePigLatin("california"));
console.log(translatePigLatin("schwartz"));
console.log(translatePigLatin("rhythm"));
console.log(translatePigLatin("algorithm"));
</script>
</body>
</html>