So for my cit class I have to write a pig Latin converter program and I'm really confused on how to use arrays and strings together.
The rules for the conversion are simple, you just move the first letter of the word to the back and then add ay. ex: hell in English would be ellhay in pig Latin
I have this so far:
<form name="form">
<p>English word/sentence:</p> <input type="text" id="english" required="required" size="80" /> <br />
<input type="button" value="Translate!" onClick="translation()" />
<p>Pig Latin translation:</p> <textarea name="piglat" rows="10" cols="60"></textarea>
</form>
<script type="text/javascript">
<!--
fucntion translation() {
var delimiter = " ";
input = document.form.english.value;
tokens = input.split(delimiter);
output = [];
len = tokens.length;
i;
for (i = 1; i<len; i++){
output.push(input[i]);
}
output.push(tokens[0]);
output = output.join(delimiter);
}
//-->
</script>
I'd really appreciate any help I can get!
function translate(str) {
str=str.toLowerCase();
var n =str.search(/[aeiuo]/);
switch (n){
case 0: str = str+"way"; break;
case -1: str = str+"ay"; break;
default :
//str= str.substr(n)+str.substr(0,n)+"ay";
str=str.replace(/([^aeiou]*)([aeiou])(\w+)/, "$2$3$1ay");
break;
}
return str;
}
translate("paragraphs")
I think the two things you really need to be looking at are the substring() method and string concatentation (adding two strings together) in general. Being that all of the objects in the array returned from your call to split() are strings, simple string concatentation works fine. For example, using these two methods, you could move the first letter of a string to the end with something like this:
var myString = "apple";
var newString = mystring.substring(1) + mystring.substring(0,1);
This code is basic, but it works. First, take care of the words that start with vowels. Otherwise, for words that start with one or more consonants, determine the number of consonants and move them to the end.
function translate(str) {
str=str.toLowerCase();
// for words that start with a vowel:
if (["a", "e", "i", "o", "u"].indexOf(str[0]) > -1) {
return str=str+"way";
}
// for words that start with one or more consonants
else {
//check for multiple consonants
for (var i = 0; i<str.length; i++){
if (["a", "e", "i", "o", "u"].indexOf(str[i]) > -1){
var firstcons = str.slice(0, i);
var middle = str.slice(i, str.length);
str = middle+firstcons+"ay";
break;}
}
return str;}
}
translate("school");
If you're struggling with arrays this might be a bit complicated, but it's concise and compact:
var toPigLatin = function(str) {
return str.replace(/(^\w)(.+)/, '$2$1ay');
};
Demo: http://jsfiddle.net/elclanrs/2ERmg/
Slightly improved version to use with whole sentences:
var toPigLatin = function(str){
return str.replace(/\b(\w)(\w+)\b/g, '$2$1ay');
};
Here's my solution
function translatePigLatin(str) {
var newStr = str;
// if string starts with vowel make 'way' adjustment
if (newStr.slice(0,1).match(/[aeiouAEIOU]/)) {
newStr = newStr + "way";
}
// else, iterate through first consonents to find end of cluster
// move consonant cluster to end, and add 'ay' adjustment
else {
var moveLetters = "";
while (newStr.slice(0,1).match(/[^aeiouAEIOU]/)) {
moveLetters += newStr.slice(0,1);
newStr = newStr.slice(1, newStr.length);
}
newStr = newStr + moveLetters + "ay";
}
return newStr;
}
Another way of doing it, using a separate function as a true or false switch.
function translatePigLatin(str) {
// returns true only if the first letter in str is a vowel
function isVowelFirstLetter() {
var vowels = ['a', 'e', 'i', 'o', 'u', 'y'];
for (i = 0; i < vowels.length; i++) {
if (vowels[i] === str[0]) {
return true;
}
}
return false;
}
// if str begins with vowel case
if (isVowelFirstLetter()) {
str += 'way';
}
else {
// consonants to move to the end of string
var consonants = '';
while (isVowelFirstLetter() === false) {
consonants += str.slice(0,1);
// remove consonant from str beginning
str = str.slice(1);
}
str += consonants + 'ay';
}
return str;
}
translatePigLatin("jstest");
this is my solution code :
function translatePigLatin(str) {
var vowel;
var consonant;
var n =str.charAt(0);
vowel=n.match(/[aeiou]/g);
if(vowel===null)
{
consonant= str.slice(1)+str.charAt(0)+”ay”;
}
else
{
consonant= str.slice(0)+”way”;
}
var regex = /[aeiou]/gi;
var vowelIndice = str.indexOf(str.match(regex)[0]);
if (vowelIndice>=2)
{
consonant = str.substr(vowelIndice) + str.substr(0, vowelIndice) + ‘ay’;
}
return consonant;
}
translatePigLatin(“gloove”);
Yet another way.
String.prototype.toPigLatin = function()
{
var str = "";
this.toString().split(' ').forEach(function(word)
{
str += (toPigLatin(word) + ' ').toString();
});
return str.slice(0, -1);
};
function toPigLatin(word)
{
//Does the word already start with a vowel?
if(word.charAt(0).match(/a*e*i*o*u*A*E*I*O*U*/g)[0])
{
return word;
}
//Match Anything before the firt vowel.
var front = word.match(/^(?:[^a?e?i?o?u?A?E?I?O?U?])+/g);
return (word.replace(front, "") + front + (word.match(/[a-z]+/g) ? 'a' : '')).toString();
}
Another way of Pig Latin:
function translatePigLatin(str) {
let vowels = /[aeiou]/g;
var n = str.search(vowels); //will find the index of vowels
switch (n){
case 0:
str = str+"way";
break;
case -1:
str = str+"ay";
break;
default:
str = str.slice(n)+str.slice(0,n)+"ay";
break;
}
return str;
}
console.log(translatePigLatin("rhythm"));
Your friends are the string function .split, and the array functions .join and .slice and .concat.
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array#Accessor_methods
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String#Methods_unrelated_to_HTML
warning: Below is a complete solution you can refer to once you have finished or spent too much time.
function letters(word) {
return word.split('')
}
function pigLatinizeWord(word) {
var chars = letters(word);
return chars.slice(1).join('') + chars[0] + 'ay';
}
function pigLatinizeSentence(sentence) {
return sentence.replace(/\w+/g, pigLatinizeWord)
}
Demo:
> pigLatinizeSentence('This, is a test!')
"hisTay, siay aay esttay!"
Related
I have been attempting to solve this codewars problem for a while in JavaScript:
"Complete the solution so that the function will break up camel casing, using a space between words. Example:"
"camelCasing" => "camel Casing"
"identifier" => "identifier"
"" => ""
I have it almost all the way, but for some reason my code is selecting the wrong space to add a blank space.
I'm hoping someone can tell me what I am doing wrong.
function solution(string) {
let splitStr = string.split("");
let newStr = string.split("");
let capStr = string.toUpperCase().split("");
for (i = 0; i < splitStr.length; i++) {
if (splitStr[i] === capStr[i]) {
newStr.splice(i, 0, ' ');
}
}
return newStr.join("");
}
console.log('camelCasing: ', solution('camelCasing'));
console.log('camelCasingTest: ', solution('camelCasingTest'));
The first insertion into newStr will be at the correct spot, but after that insertion of the space, the letters that follow it in newStr will be at an increased index. This means that when the next capital is found at i in splitStr (which did not change), the insertion into newStr (which did change) should really be at i+1.
A solution is to make your loop iterate from end to start:
function solution(string) {
let splitStr = string.split("");
let newStr = string.split("");
let capStr = string.toUpperCase().split("");
for (i = splitStr.length - 1; i >= 0; i--) {
if (splitStr[i] === capStr[i]) {
newStr.splice(i, 0, ' ');
}
}
return newStr.join("");
}
console.log('camelCasing: ', solution('camelCasing'));
console.log('camelCasingTest: ', solution('camelCasingTest'));
This kind of problem is however much easier solved with a regular expression:
function solution(string) {
return string.replace(/[A-Z]/g, " $&");
}
console.log('camelCasing: ', solution('camelCasing'));
console.log('camelCasingTest: ', solution('camelCasingTest'));
Explanation of the regular expression:
[A-Z] a capital letter from the Latin alphabet.
$& backreference to the matched letter, used in the replacement.
g global flag so all matches are replaced.
Here could be a solution with a simple loop and some if conditions
const breakCamelCase = (word) => {
let result = "";
// loop on letter
for (let letter of word) {
// if letter is uppercase and not the first letter of the word add a space followed by the letter
if (letter == letter.toUpperCase() && result) {
result += ` ${letter}`;
} else { // else just add the letter
result += letter;
}
}
return result;
}
function solution(string) {
let splitStr = string.split("");
let newStr = "";
splitStr.forEach(e =>{
if(e === e.toUpperCase()) newStr +=" "+e;
else newStr += e;
});
return newStr;
}
console.log(solution('camelCasing'));//success = "camel Casing"
console.log(solution('camelCasingTest'));
I apologize if this question has been answered somewhere - please point me in the right direction if so. I have read through a bunch of solutions and have not yet cracked it!
Sooo...basically, I need to:
Move the first letter of each word to the end of it, then add "ay" to the end of the word. Leave punctuation marks untouched.
This is my code so far:
function pigIt(str) {
var newStr = str.split(" ");
var changed = newStr.map(function(input) {
return input.substring(1) + input.charAt(0) + "ay";
});
changed = changed.join(" ");
return changed;
}
console.log(pigIt('Pig latin is cool'));
As you can see, the code will work for any input that doesn't include punctuation. Great. Now I need to maybe add a Regex somewhere to exclude punctuation but I don't know where to put it! Please help!!
You could split by the word boundary /(\W+)/ while capturing separator. Transform words only. And then join back.
function pigIt(str) {
var newStr = str.split(/(\W+)/); // ['Pig', ' ', 'latin', ',- ',..]
var changed = newStr.map(function(input) {
if (!/\w/.test(input)) return input // keep non word elements as is
return input.substring(1) + input.charAt(0) + "ay";
});
return changed.join("");
}
console.log(pigIt('Pig latin,- is cool!'));
I think because you are going to want to put the punctuation back in the same place after processing, then you will probably be better of doing it all manually.
Loop the input 1 char at a time and build a 'word buffer', every time you hit a non-letter character then process the word buffer and append the non-letter character too.
function pigIt(str) {
var process = function(s) {
if (s.length < 2) {
return s;
}
return s.substring(1) + s.charAt(0) + "ay";
};
var result = '';
var buffer = '';
for (var i = 0; i < str.length; i++) {
var c = str[i];
if (c.match(/[a-zA-Z]/i)) {
buffer += c;
} else {
if (buffer.length) {
result += process(buffer);
buffer = '';
}
result += c;
}
}
result += process(buffer);
buffer = '';
return result;
}
var output = pigIt('Pig latin is cool.');
console.log(output);
Without using the split reverse and join functions, how would one do such a thing?
The Problem Given: Reverse the words in a string
Sample Input: "Hello World"
Sample Output: "World Hello"
<script>
var newString = "";
var theString = prompt("Enter a Phrase that you would like to reverse (Ex. Hello world)");
newString = theString.split(" ").reverse().join(" ")
document.write(newString);
</script>
Arrays can be used like stacks out of the box. And stacks are LIFO, which is what you need.
function reverseWords(str) {
var word, words, reverse;
words = str.match(/(?:\w+)/g);
reverse = '';
while(word = words.pop()) {
reverse += word + ' ';
}
return reverse.trim();
}
reverseWords('hello world');
Or use the call stack as your stack:
function reverseWords(str) {
var result = '';
(function readWord(i = 0) {
var word = '';
if(i > str.length) {
return '';
}
while(str[i] !== ' ' && i < str.length) {
word += str[i];
i++;
}
readWord(++i); // Skip over delimiter.
result += word + ' ';
}());
return result.trim();
}
reverseWords('hello world');
Another idea for reversing the words in a String is using a Stack data structure. Like so:
var newString = "";
var theString = prompt("Enter a Phrase that you would like to reverse (Ex. Hello world)");
var word = "";
var c;
var stack = [];
for (var i = 0, len = theString.length; i < len; i++) {
c = theString[i];
word += c;
if (c == " " || i == (len-1)) {
word = word.trim();
stack.push(word);
word = "";
}
}
while (s = stack.pop()) {
newString += s + " ";
}
console.log(newString);
You could also go fancy and try something like this:
I couldn't come up with a shorter solution.
var newString = "";
var theString = prompt("Enter a Phrase that you would like to reverse (Ex. Hello world)");
theString.replace(/[^\s]*/g, function (value) {
newString = value + ' ' + newString;
});
document.write(newString);
Of the millions of different solutions, the least amount of typing I could come up with involves using lastIndexOf and substring.
var str = "The quick brown fox",
reversed = "",
idx;
while(true) {
idx = str.lastIndexOf(" ")
reversed = reversed + str.substring(idx).trim() + " "
if (idx < 0) break;
str = str.substring(0, idx)
}
reversed.trim() # Oh, yes, trim too
Output:
"fox brown quick The"
The simplest way to do in javascript. Here replace() have /,/g it will replace all comma from the string to space.
var msg = 'Hello world I am Programmer';
var newstr = msg.split(" ").reverse().join().replace(/,/g, ' ');
console.log(newstr)
;
So for my cit class I have to write a pig Latin converter program and I'm really confused on how to use arrays and strings together.
The rules for the conversion are simple, you just move the first letter of the word to the back and then add ay. ex: hell in English would be ellhay in pig Latin
I have this so far:
<form name="form">
<p>English word/sentence:</p> <input type="text" id="english" required="required" size="80" /> <br />
<input type="button" value="Translate!" onClick="translation()" />
<p>Pig Latin translation:</p> <textarea name="piglat" rows="10" cols="60"></textarea>
</form>
<script type="text/javascript">
<!--
fucntion translation() {
var delimiter = " ";
input = document.form.english.value;
tokens = input.split(delimiter);
output = [];
len = tokens.length;
i;
for (i = 1; i<len; i++){
output.push(input[i]);
}
output.push(tokens[0]);
output = output.join(delimiter);
}
//-->
</script>
I'd really appreciate any help I can get!
function translate(str) {
str=str.toLowerCase();
var n =str.search(/[aeiuo]/);
switch (n){
case 0: str = str+"way"; break;
case -1: str = str+"ay"; break;
default :
//str= str.substr(n)+str.substr(0,n)+"ay";
str=str.replace(/([^aeiou]*)([aeiou])(\w+)/, "$2$3$1ay");
break;
}
return str;
}
translate("paragraphs")
I think the two things you really need to be looking at are the substring() method and string concatentation (adding two strings together) in general. Being that all of the objects in the array returned from your call to split() are strings, simple string concatentation works fine. For example, using these two methods, you could move the first letter of a string to the end with something like this:
var myString = "apple";
var newString = mystring.substring(1) + mystring.substring(0,1);
This code is basic, but it works. First, take care of the words that start with vowels. Otherwise, for words that start with one or more consonants, determine the number of consonants and move them to the end.
function translate(str) {
str=str.toLowerCase();
// for words that start with a vowel:
if (["a", "e", "i", "o", "u"].indexOf(str[0]) > -1) {
return str=str+"way";
}
// for words that start with one or more consonants
else {
//check for multiple consonants
for (var i = 0; i<str.length; i++){
if (["a", "e", "i", "o", "u"].indexOf(str[i]) > -1){
var firstcons = str.slice(0, i);
var middle = str.slice(i, str.length);
str = middle+firstcons+"ay";
break;}
}
return str;}
}
translate("school");
If you're struggling with arrays this might be a bit complicated, but it's concise and compact:
var toPigLatin = function(str) {
return str.replace(/(^\w)(.+)/, '$2$1ay');
};
Demo: http://jsfiddle.net/elclanrs/2ERmg/
Slightly improved version to use with whole sentences:
var toPigLatin = function(str){
return str.replace(/\b(\w)(\w+)\b/g, '$2$1ay');
};
Here's my solution
function translatePigLatin(str) {
var newStr = str;
// if string starts with vowel make 'way' adjustment
if (newStr.slice(0,1).match(/[aeiouAEIOU]/)) {
newStr = newStr + "way";
}
// else, iterate through first consonents to find end of cluster
// move consonant cluster to end, and add 'ay' adjustment
else {
var moveLetters = "";
while (newStr.slice(0,1).match(/[^aeiouAEIOU]/)) {
moveLetters += newStr.slice(0,1);
newStr = newStr.slice(1, newStr.length);
}
newStr = newStr + moveLetters + "ay";
}
return newStr;
}
Another way of doing it, using a separate function as a true or false switch.
function translatePigLatin(str) {
// returns true only if the first letter in str is a vowel
function isVowelFirstLetter() {
var vowels = ['a', 'e', 'i', 'o', 'u', 'y'];
for (i = 0; i < vowels.length; i++) {
if (vowels[i] === str[0]) {
return true;
}
}
return false;
}
// if str begins with vowel case
if (isVowelFirstLetter()) {
str += 'way';
}
else {
// consonants to move to the end of string
var consonants = '';
while (isVowelFirstLetter() === false) {
consonants += str.slice(0,1);
// remove consonant from str beginning
str = str.slice(1);
}
str += consonants + 'ay';
}
return str;
}
translatePigLatin("jstest");
this is my solution code :
function translatePigLatin(str) {
var vowel;
var consonant;
var n =str.charAt(0);
vowel=n.match(/[aeiou]/g);
if(vowel===null)
{
consonant= str.slice(1)+str.charAt(0)+”ay”;
}
else
{
consonant= str.slice(0)+”way”;
}
var regex = /[aeiou]/gi;
var vowelIndice = str.indexOf(str.match(regex)[0]);
if (vowelIndice>=2)
{
consonant = str.substr(vowelIndice) + str.substr(0, vowelIndice) + ‘ay’;
}
return consonant;
}
translatePigLatin(“gloove”);
Yet another way.
String.prototype.toPigLatin = function()
{
var str = "";
this.toString().split(' ').forEach(function(word)
{
str += (toPigLatin(word) + ' ').toString();
});
return str.slice(0, -1);
};
function toPigLatin(word)
{
//Does the word already start with a vowel?
if(word.charAt(0).match(/a*e*i*o*u*A*E*I*O*U*/g)[0])
{
return word;
}
//Match Anything before the firt vowel.
var front = word.match(/^(?:[^a?e?i?o?u?A?E?I?O?U?])+/g);
return (word.replace(front, "") + front + (word.match(/[a-z]+/g) ? 'a' : '')).toString();
}
Another way of Pig Latin:
function translatePigLatin(str) {
let vowels = /[aeiou]/g;
var n = str.search(vowels); //will find the index of vowels
switch (n){
case 0:
str = str+"way";
break;
case -1:
str = str+"ay";
break;
default:
str = str.slice(n)+str.slice(0,n)+"ay";
break;
}
return str;
}
console.log(translatePigLatin("rhythm"));
Your friends are the string function .split, and the array functions .join and .slice and .concat.
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array#Accessor_methods
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String#Methods_unrelated_to_HTML
warning: Below is a complete solution you can refer to once you have finished or spent too much time.
function letters(word) {
return word.split('')
}
function pigLatinizeWord(word) {
var chars = letters(word);
return chars.slice(1).join('') + chars[0] + 'ay';
}
function pigLatinizeSentence(sentence) {
return sentence.replace(/\w+/g, pigLatinizeWord)
}
Demo:
> pigLatinizeSentence('This, is a test!')
"hisTay, siay aay esttay!"
function LetterChanges(str) {
var alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
for (var i = 0; i < str.length; i++) {
var index = alphabet.indexOf(str[i])
if (/[a-zA-Z]/.test(str[i])) {
str = str.replace(str[i], alphabet.charAt(index + 1));
}
if (/[aeiou]/.test(str[i])) {
str = str.replace(str[i], alphabet.charAt(index + 26));
}
}
return str;
}
When I call LetterChanges("hello"), it returns 'Ifmmp' which is correct, but when "sent" is passed it returns 'ufOt' instead of 'tfOu'. Why is that?
str.replace() replaces the first occurrence of the match in the string with the replacement. LetterChanges("sent") does the following:
i = 0 : str.replace("s", "t"), now str = "tent"
i = 1 : str.replace("e", "f"), now str = "tfnt"
i = 2 : str.replace("n", "o"), now str = "tfot", then
str.replace("o", "O"), now str = "tfOt"
i = 3 : str.replace("t", "u"), now str = "ufOt"
return str
There are several issues. The main one is that you could inadvertently change the same letter several times.
Let's see what happens to the s in sent. You first change it to t. However, when it comes to changing the final letter, which is also t, you change the first letter again, this time from t to u.
Another, smaller, issue is the handling of the letter z.
Finally, your indexing in the second if is off by one: d becomes D and not E.
You can use String.replace to avoid that:
function LetterChanges(str) {
return str.replace(/[a-zA-Z]/g, function(c){
return String.fromCharCode(c.charCodeAt(0)+1);
}).replace(/[aeiou]/g, function(c){
return c.toUpperCase();
});
}
But there is still a bug: LetterChanges('Zebra') will return '[fcsb'. I assume that is not your intention. You will have to handle the shift.
Try this one:
function LetterChanges(str) {
var alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
var result = '';
var temp;
for (var i = 0; i < str.length; i++) {
var index = alphabet.indexOf(str[i])
if (/[a-zA-Z]/.test(str[i])) {
//str = str.replace(str[i], alphabet.charAt(index + 1));
temp= alphabet.charAt(index + 1);
index = index+1;
}
else if(str[i] == ' '){
temp = ' ';
}
if (/[aeiou]/.test(temp)) {
temp = alphabet.charAt(index + 26);
}
result += temp;
}
return result;
}
var str = 'bcd12';
str = str.replace(/[a-z]/gi, function(char) { //call replace method
char = String.fromCharCode(char.charCodeAt(0)+1);//increment ascii code of char variable by 1 .FromCharCode() method will convert Unicode values into character
if (char=='{' || char=='[') char = 'a'; //if char values goes to "[" or"{" on incrementing by one as "[ ascii value is 91 just after Z" and "{ ascii value is 123 just after "z" so assign "a" to char variable..
if (/[aeiuo]/.test(char)) char = char.toUpperCase();//convert vowels to uppercase
return char;
});
console.log(str);
Check this code sample. There is no bug in it. Not pretty straight forward but Works like a charm. Cheers!
function LetterChanges(str) {
var temp = str;
var tempArr = temp.split("");//Split the input to convert it to an Array
tempArr.forEach(changeLetter);/*Not many use this but this is the referred way of using loops in javascript*/
str = tempArr.join("");
// code goes here
return str;
}
function changeLetter(ele,index,arr) {
var lowerLetters ="abcdefghijklmnopqrstuvwxyza";
var upperLetters ="ABCDEFGHIJKLMNOPQRSTUVWXYZA";
var lowLetterArr = lowerLetters.split("");
var upLetterArr = upperLetters.split("");
var i =0;
for(i;i<lowLetterArr.length;i++){
if(arr[index] === lowLetterArr[i]){
arr[index] = lowLetterArr[i+1];
arr[index]=arr[index].replace(/[aeiou]/g,arr[index].toUpperCase());
return false;
}
if(arr[index] === upLetterArr[i]){
arr[index] = upLetterArr[i+1];
arr[index]=arr[index].replace(/[aeiou]/g,arr[index].toUpperCase());
return false;
}
}
}
// keep this function call here
// to see how to enter arguments in JavaScript scroll down
LetterChanges(readline());