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
12 changes: 9 additions & 3 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,23 @@
<head>
<meta charset="utf-8">
<title></title>
<script src="main.js"></script>

</head>
<body>
<h1>Hello World!</h1>
<h1>Pig Latin</h1>
<hr/>
<div id="display-element"></div>
<form>
<input id="user-input" type="text">
<!-- How do you get this input sent to the pigLatin() function? -->
<button onclick="pigLatin()">Translate</button>
<button onclick="pigLatin()" type="button">Translate</button>

</form>
<hr/>
<ul id="Translated-PigLatinWord">

</ul>

<script src="main.js"></script>
</body>
</html>
158 changes: 97 additions & 61 deletions main.js
Original file line number Diff line number Diff line change
@@ -1,71 +1,107 @@
'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) => {

// Your code here

}

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

// const { strictEqual } = require('assert');
// // 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 = () => {
console.log()
let str = document.querySelector('#user-input').value;

str = str.trim('').toLowerCase();

let vowels = ["a", "e", "i", "o", "u"]

if (vowels.indexOf(str[0]) > -1) {
return str = str + "yay";
}
// words that start with one or more consonants
else {
for (let i = 0; i<str.length; i++){
if (vowels.indexOf(str[i]) > -1){
let firstCons = str.slice(0, i);
let middle = str.slice(i, str.length);
str = middle.concat(firstCons) + "ay";
break;
}
}
// return str;
}
let ul = document.getElementById('Translated-PigLatinWord')
let li = document.createElement('li');
li.appendChild(document.createTextNode(str))
ul.appendChild(li)
}


// 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
// **********

// Your code here

// 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.
// 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.