Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 70 additions & 1 deletion 02week/pigLatin.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,82 @@ const rl = readline.createInterface({
output: process.stdout
});

//planning
//input text
//check to see if valid (make sure string, check if any numbers), if not ask to re enter a word. checkValid()
//check for uppercase, turn all to lowercase. fixCase()
//check to see if start with vowel, if vowel, add yay to end. checkVowel() return it back
//turn entered word to array(splice). turnIntoList()
//go through array until first vowel(loop until vowel). checkConsonent()
//take letters until first vowel and put to the end (pop) and add ay. addVowelAy()

function pigLatin(word) {
var wordArray = turnToList(word);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't use var, should be const

if (checkValid(wordArray)) {
if (checkVowel(wordArray)) {
return wordArray.join("") + "yay";
} else {
var vowelIndex = checkConsonant(wordArray);
pushConsonant(wordArray, vowelIndex);
return wordArray.join("") + "ay";
} //end of checkVowel()
} else {
return "Please remove any numbers and try again"
} //end of checkValid()
}

const turnToList = (word) => {
const lowerCase = word.toLowerCase();
const trimmed = lowerCase.trim();
const toList = trimmed.split("");
return toList;
}

// Your code here
const checkValid = (word) => {
const arrayLength = word.length;
for (var i = 0; i < arrayLength; i++) {
if (isNaN(word[i])) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just return the evaluation

return true;
} else {
return false;
}
}
}

const checkVowel = (word) => {
const vowel = ["a", "e", "i", "o", "u"];
for (var i = 0; i < vowel.length; i++) {
if (word[0] == vowel[i]) {
return true;
}
}
return false;
}

const checkConsonant = (word) => {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do you need a check for constant and vowel?

const vowel = ["a", "e", "i", "o", "u"];
var i = 0;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be let

var found = false;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be let

while (!found && i < word.length) {
for (var j = 0; j < vowel.length; j++) {
if (word[i] == vowel[j]) {
found = true;
break;
}
}
if (!found) {
i++;
}
}
return i;
}

const pushConsonant = (word, firstVowelIndex) => {
for (var i = 0; i < firstVowelIndex; i++) {
const letter = word.shift();
word.push(letter);
}
}

function getPrompt() {
rl.question('word ', (answer) => {
Expand Down
Loading