-
Notifications
You must be signed in to change notification settings - Fork 226
Expand file tree
/
Copy pathmain.js
More file actions
129 lines (100 loc) · 3.34 KB
/
Copy pathmain.js
File metadata and controls
129 lines (100 loc) · 3.34 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
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
'use strict';
// brings in the assert module for unit testing
const assert = require('assert');
// brings in the readline module to access the command line
const readline = require('readline');
// use the readline module to print out to the command line
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
const translate = (word) => {
let vowelPosition = positionOfFirstVowel(word);
if (vowelPosition === -1) {
// If the word doesn't have a vowel add ay to the end of it
return noVowels(word);
} else if (vowelPosition === 0) {
// If the first letter is a vowel add yay to the end
return beginsWithVowel(word);
}
// If the first letter is a constanant find the fist vowel.
// split the word at the vowel and swap sides. Add ay to the end of it
return beginsWithConsonant(word, vowelPosition);
}
const positionOfFirstVowel = (word) => {
for (let i = 0; i < word.length; i++) {
const vowels = 'aeiou';
let letter = word[i];
if (vowels.includes(letter)) {
return i;
}
}
return -1;
}
const noVowels = (word) => {
return word += 'ay';
}
const beginsWithVowel = (word) => {
return word += 'yay';
}
const beginsWithConsonant = (word, position) => {
let firstString = word.substr(0, position);
let secondString = word.substr(position);
return secondString += firstString += 'ay';
}
const stringToArrayFormatted = (string) => {
return string.trim().toLowerCase().split(' ');
}
const pigLatin = (string) => {
let wordArray = stringToArrayFormatted(string);
// Check for multiple words in string
if (wordArray.length > 1) {
let newString = '';
for (let i = 0; i < wordArray.length; i++) {
newString += translate(wordArray[i]) + ' ';
}
return newString.trim();
}
return translate(wordArray[0]);
}
// the first function called in the program to get an input from the user
// to run the function use the command: node main.js
// to close it ctrl + C
const getPrompt = () => {
rl.question('word ', (answer) => {
console.log(pigLatin(answer));
getPrompt();
});
}
// Unit Tests
// to use them run the command: npm test main.js
// to close them ctrl + C
if (typeof describe === 'function') {
describe('#pigLatin()', () => {
it('should translate a simple word', () => {
assert.equal(pigLatin('car'), 'arcay');
assert.equal(pigLatin('dog'), 'ogday');
});
it('should translate a complex word', () => {
assert.equal(pigLatin('create'), 'eatecray');
assert.equal(pigLatin('valley'), 'alleyvay');
});
it('should attach "yay" if word begins with vowel', () => {
assert.equal(pigLatin('egg'), 'eggyay');
assert.equal(pigLatin('emission'), 'emissionyay');
});
it('should lowercase and trim word before translation', () => {
assert.equal(pigLatin('HeLlO '), 'ellohay');
assert.equal(pigLatin(' RoCkEt'), 'ocketray');
});
});
} else {
getPrompt();
}
// **********
// HINTS
// **********
// break your code into pieces and focus on one piece at a time...
// 1. if word begins with a vowel send to one function: adds "yay"
// 2. if word begins with a consonant send to another function: splices off beginning, returns word with new ending.
// 3. if multiple words, create array of words, loop over them, sending them to different functions and creating a new array with the new words.