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
28 changes: 15 additions & 13 deletions index.html
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script src="main.js"></script>
</head>
<body>
<h1>Hello World!</h1>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h1>Pig Latin Translator</h1>
<hr/>
<div id="display-element"></div>
<form>
<form onsubmit="event.preventDefault(); translate();">
<input id="user-input" type="text">
<!-- How do you get this input sent to the pigLatin() function? -->
<button onclick="pigLatin()">Translate</button>
</form>
<button id="translate-button">Translate</button>
</form>
<hr/>
</body>
</html>
</html>
</body>
</html>
108 changes: 47 additions & 61 deletions main.js
Original file line number Diff line number Diff line change
@@ -1,71 +1,57 @@
// declares JavaScript's "strict" mode. it enforces stricter parsing and error handling during runtime. helps write cleaner code and catch more errors and bugs that might otherwise go unnoticed.
'use strict';

// brings in the assert module for unit testing
const assert = require('assert');
// brings in the readline module to access the command line
//sets up readline module module and creates an interface to read input from the console, and to write output on the console.
//inport the 'readline' module to read input from the console
const readline = require('readline');
// use the readline module to print out to the command line
//creates an interface for reading input. It tells it where to get its input data from, and where to write it's output
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});


//named variable that hold a finction to translate word to pigLatin
const pigLatin = (word) => {
//trims and converts words to lowercase
let inputWord = word.trim().toLowerCase();
//checks if work is empty
if (inputWord.length === 0) {
//asks user to enter a valid input
return "Please enter a valid word.";
}

//checks if the word starts with a vowel by calling th 'isVowel' helper function
if (isVowel(inputWord[0])) {
//if it does it appends "yay" to the word and returns the translated word.
return inputWord + "yay";
} else { // if it doesnt, this function extracts the first character and moves it to the end of the word, followed by "ay". This forms the translated word
//the value of the named variable takes the first character and moves it to the end of the word.
let firstCharacter = inputWord.slice(0, 1);
//this variable holds the rest of the word
let restOfWord = inputWord.slice(1);
//returns translated word
return restOfWord + firstCharacter + "ay";
}
};

//helper function to check if a letter is a vowel to check if word starts with a vowel
const isVowel = (letter) => {
// assigning the vowels to a variable. this tells the console what vowels are
const vowels = ['a', 'e', 'i', 'o', 'u'];
//this returns the character if it is a vowel when the function is running it's check
return vowels.includes(letter);
};



//prompts the user to enter a word
//calls our bucket 'rl' which holds our readline variable
rl.question('Enter a word: ', (answer) => {
//translates the word to pig latin by calling the 'pigLatin' fuction
const translation = pigLatin(answer);
//displays the translation
console.log(`Translation: ${translation}`);
// closes the readline interface
rl.close();
});

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

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