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
21 changes: 21 additions & 0 deletions 02week/pigLatin.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>PigLatin</title>
<script src="PigLatin.js"></script>
</head>
<body>
<h1>Pig Latin Translator</h1>
<div>
<p>Enter English Word:</p>
<input id="inputElement" />
<button onclick="translateNow()">Translate</button>
</div>
<br />
<p>Your word in Pig Latin:</p>
<p id="outputElement"></p>
</body>
</html>
66 changes: 44 additions & 22 deletions 02week/pigLatin.js
Original file line number Diff line number Diff line change
@@ -1,51 +1,73 @@
'use strict';
"use strict";

const assert = require('assert');
const readline = require('readline');
const assert = require("assert");
const readline = require("readline");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});

function detectVowel(word) {
let vowels = "aeiou";

for (let i = 0; i < word.length; i++) {
if (vowels.indexOf(word[i]) !== -1) {
return i;
}
}
}

function pigLatin(word) {
word = word.toLowerCase().trim();

// Your code here
let firstVowel = detectVowel(word);

if (firstVowel > 0) {
return word.slice(firstVowel) + word.slice(0, firstVowel) + "ay";
}
return word + "yay";
}

function translateNow() {
var inputBox = document.getElementById("inputElement");

var englishWord = inputBox.value;

var pigWord = pigLatin(englishWord);

var nowYouSeeIt = document.getElementById("outputElement");

nowYouSeeIt.innerText = pigWord;
}

function getPrompt() {
rl.question('word ', (answer) => {
console.log( pigLatin(answer) );
rl.question("word ", answer => {
console.log(pigLatin(answer));
getPrompt();
});
}

// Tests

if (typeof describe === 'function') {

describe('#pigLatin()', () => {
it('should translate a simple word', () => {
assert.equal(pigLatin('car'), 'arcay');
assert.equal(pigLatin('dog'), 'ogday');
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 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');
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');
it("should lowercase and trim word before translation", () => {
assert.equal(pigLatin("HeLlO "), "ellohay");
assert.equal(pigLatin(" RoCkEt"), "ocketray");
});
});
} else {

getPrompt();

}
97 changes: 97 additions & 0 deletions 02week/tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
"use strict";

const assert = require("assert");
const readline = require("readline");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});

const rockPaperScissors = (hand1, hand2) => {
hand1 = hand1.trim().toLowerCase();
hand2 = hand2.trim().toLowerCase();

if (typeof hand1 !== "string" || typeof hand2 !== "string") {
return `Must enter "rock", "paper" or "scissors"`;
} else if (
(hand1 !== "rock" && hand1 !== "paper" && hand1 !== "scissors") ||
(hand2 !== "rock" && hand2 !== "paper" && hand2 !== "scissors")
) {
return `Must enter "rock", "paper" or "scissors"`;
} else if (hand1 === hand2) {
return "It's a tie!";
} else if (hand1 === "rock" && hand2 === "scissors") {
return `Hand 1 wins!`;
} else if (hand1 === "paper" && hand2 === "rock") {
return `Hand 1 wins!`;
} else if (hand1 === "scissors" && hand2 === "paper") {
return `Hand 1 wins!`;
} else if (hand1 === "scissors" && hand2 === "rock") {
return `Hand 2 wins!`;
} else if (hand1 === "rock" && hand2 === "paper") {
return `Hand 2 wins!`;
} else if (hand1 === "paper" && hand2 === "scissors") {
return `Hand 2 wins!`;
}
};

function getPrompt() {
rl.question("hand1: ", answer1 => {
rl.question("hand2: ", answer2 => {
console.log(rockPaperScissors(answer1, answer2));
getPrompt();
});
});
}

// Tests

if (typeof describe === "function") {
describe("#rockPaperScissors()", () => {
it("should detect a tie", () => {
assert.equal(rockPaperScissors("rock", "rock"), "It's a tie!");
assert.equal(rockPaperScissors("paper", "paper"), "It's a tie!");
assert.equal(rockPaperScissors("scissors", "scissors"), "It's a tie!");
});
it("should detect which hand won", () => {
assert.equal(rockPaperScissors("rock", "paper"), `Hand 2 wins!`);
assert.equal(rockPaperScissors("paper", "scissors"), `Hand 2 wins!`);
assert.equal(rockPaperScissors("rock", "scissors"), `Hand 1 wins!`);
});
it('should scrub input to ensure lowercase with "trim"ed whitepace', () => {
assert.equal(rockPaperScissors("rOcK", " paper "), `Hand 2 wins!`);
assert.equal(rockPaperScissors("Paper", "SCISSORS"), `Hand 2 wins!`);
assert.equal(rockPaperScissors("rock ", "sCiSsOrs"), `Hand 1 wins!`);
});
it("should scrub input to only accept rock paper or scissors", () => {
assert.equal(
rockPaperScissors("hammer", " paper "),
`Must enter "rock", "paper" or "scissors"`
);
assert.equal(
rockPaperScissors("Paper", "brick"),
`Must enter "rock", "paper" or "scissors"`
);
assert.equal(
rockPaperScissors("ball ", "sCiSsOrs"),
`Must enter "rock", "paper" or "scissors"`
);
});
it("must enter string with no numbers", () => {
assert.equal(
rockPaperScissors("9776", " paper "),
`Must enter "rock", "paper" or "scissors"`
);
assert.equal(
rockPaperScissors("Paper", "99999"),
`Must enter "rock", "paper" or "scissors"`
);
assert.equal(
rockPaperScissors("4355", "sCiSsOrs"),
`Must enter "rock", "paper" or "scissors"`
);
});
});
} else {
getPrompt();
}