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
17 changes: 10 additions & 7 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@
<html>
<head>
<meta charset="utf-8">
<title></title>
<script src="main.js"></script>
<title>Pig Latin</title>
<script src="./main.js"></script>
</head>
<body>
<h1>Hello World!</h1>
<hr/>
<div id="display-element"></div>
<button onclick="displayDate()">Click Me</button>
<hr/>
<h1>Pig Latin Converter</h1>
<h2>Engter your word to be translated into Pig Latin</h2>
<p> </p>
<label for="word">Enter a word: </label>
<input type="text" id="word" name="word" />
</br>
<button onclick="getPrompt()">Submit Word</button>
<p id="pigLat"></p>
</body>
</html>
102 changes: 66 additions & 36 deletions main.js
Original file line number Diff line number Diff line change
@@ -1,60 +1,90 @@
'use strict';

// brings in the assert module for unit testing
const assert = require('assert');
//const assert = require('assert');
// brings in the readline module to access the command line
const readline = require('readline');
//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 rl = readline.createInterface({
// input: process.stdin,
// output: process.stdout
// });


const pigLatin = (word) => {

// Your code here

// let word = document.getElementById("word").value;
let vowel1 = 0;
let found = false;
word = word.trim().toLowerCase();
let wordArr = word.split('');

//console.log(word[0]);

if(word[0] === 'a' || word[0] === 'e' || word[0] === 'i' || word[0] === 'o' || word[0] === 'u') {
console.log(word+"yay");
document.getElementById("pigLat").innerHTML = word+"yay";
return word +"yay";
}

else {
for(let i = 1; i < wordArr.length; i++){

if((wordArr[i] === 'a' || wordArr[i] === 'e' || wordArr[i] === 'i' || wordArr[i] === 'o' || wordArr[i] === 'u') && (!found)){
vowel1 = i;
found = true;
}
}

console.log(word.slice(vowel1) + word.slice(0, vowel1) + "ay");
document.getElementById("pigLat").innerHTML = word.slice(vowel1) + word.slice(0, vowel1) + "ay";
return word.substring(vowel1) + word.substring(0, vowel1) + "ay";

}
}

// 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();
});
let answer = document.getElementById("word").value;
pigLatin(answer);
// rl.question('word ', (answer) => {
// console.log( pigLatin(answer) );
// getPrompt();
// });
}

// Unit Tests
// You 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();

}
// 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();

// }



Expand Down