-
Notifications
You must be signed in to change notification settings - Fork 226
Expand file tree
/
Copy pathmain.js
More file actions
108 lines (76 loc) · 2.55 KB
/
Copy pathmain.js
File metadata and controls
108 lines (76 loc) · 2.55 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
'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 pigLatin = (word) => {
word = word.toLowerCase().trim()
console.log("Your first letter is " + word [0])
let firstletter = word[0]
let vowels = ["a", "e", "i", "o", "u"]
let vowelPosition = 0
//If it starts with a vowel just add "yay"
if(vowels.includes( firstletter ) == true)
{
return word + "yay"
//it is a vowel
}
else{
//not a vowel
for (let index = 0; index < word.length; index = index + 1) {
if(vowels.includes(word[index])){
vowelPosition = index
break;
}
//check each letter to see how many consonants there are in a row
}
let finalAnswer = word.slice(vowelPosition) + word.slice(0, vowelPosition) + "ay"
return finalAnswer
}
}
// 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.