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
10 changes: 5 additions & 5 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@
<html>
<head>
<meta charset="utf-8">
<title></title>
<script src="main.js"></script>
<title>Pig Latin Translator</title>
<script src="main.js" defer></script>
</head>
<body>
<h1>Hello World!</h1>
<h1>Translate any word to pig latin!</h1>
<hr/>
<div id="display-element"></div>
<form>
<form id="form">
<input id="user-input" type="text">
<!-- How do you get this input sent to the pigLatin() function? -->
<button onclick="pigLatin()">Translate</button>
<button type="button" onclick="pigLatin()">Translate</button>
</form>
<hr/>
</body>
Expand Down
134 changes: 91 additions & 43 deletions main.js
Original file line number Diff line number Diff line change
@@ -1,60 +1,108 @@
'use strict';
// '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 form = document.getElementById("form")
// form.addEventListener("submit", (e) => {
// e.preventDefault()
// })

// const input = document.getElementById("user-input")
// input.addEventListener("input", (event) => {
// console.log(event.target.value)
// })

// 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 = document.getElementById('user-input').value
word = word.toLowerCase().trim()

console.log("Your first letter is " + word [0])

const pigLatin = (word) => {

// Your code here
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)
{
document.getElementById("display-element").innerHTML = word + "yay"
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"
document.getElementById("display-element").innerHTML = finalAnswer
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();
});
}
// 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();

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