I was making a pig latin translater for a seminar on programming, but ran into issues with the if statements i was using to check for if the first letter of a word was a vowel. Even when i specifically state that the Boolean "isVowel" is false, it still would print out bananaway, the translation for vowels, rather than ananabay, the translation for consonants.
var button = document.getElementById("button");
var input = document.getElementById("input");
button.addEventListener("click", function(){
var original = String(input.value);
var translated = "";
originalArray = original.split(" ");
for (var index = 0; index < originalArray.length; index++) {
var word = originalArray[index];
var wordFirstIndex = word[0];
var rootWord = word.substr(1);
var isVowel = false;
if (wordFirstIndex = "a") {
isVowel = true;
} else if (wordFirstIndex = "e") {
isVowel = true;
} else if (wordFirstIndex = "i") {
isVowel = true;
} else if (wordFirstIndex = "o") {
isVowel = true;
} else if (wordFirstIndex = "u") {
isVowel = true;
} else {
isVowel = false;
}
if (isVowel = true) {
translated += word + "way" + " ";
} else if (!isVowel) {
translated += rootWord + wordFirstIndex + "ay" + " ";
}
}
var output = document.getElementById("output").innerHTML = translated;
});
body {
font-family: courier new;
}
#button {
color: white;
background-color: blue;
border: none;
padding: 5px 10px;
font-family: courier new;
}
#input {
padding: 10px 20px;
border: 2px;
border-style: solid;
border-color: skyblue;
}
#section1 {
font-size: 15px;
}
#output {
padding: 5px 50px;
font-size: 13px;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>repl.it</title>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<pre>
<textarea id="input" rows="5" cols="40"></textarea>
<button id="button">Translate</button>
<section id="section1">
New text:
<p id="output"></p>
</pre>
</section>
<script src="script.js"></script>
</pre>
</body>
</html>