OK, so the challenge here was to create a Pig Latin translator/ Basically if the first letter or two are consonants, remove that and add - “AY” on the end. If the word begins with a vowel, then just tack “WAY” on the back end of the word and there you go.
Here is where I am :
function translatePigLatin(str) {
str=str.toLowerCase();
let c =(/[aeiou]/).exec(str);
console.log(c.index)
if(c.index=0){
console.log(str)
//str = str+"way";
console.log (str);
}
if(c.index=1){
let r=str.substr(1);
//console.log(r);
let f= str.substr(0,1);
str = r+f+"ay";
console.log(str);
}
console.log (c.index)
if(c.index=2){
let r= str.substr(2);
let f= str.substr(0,2);
str=r+f+"ay";
return str;
}
}
// console.log(str.substr(i,1));
// return str;
translatePigLatin("algorithm");
Not sure why this does not fly… … thank you in advance for your kind assistance.
It is not only the first letter or two, if your word start with a cluster of consonants you have to move all of them to the end: strength -> engthstray. You also need to consider what happens if your word has no vowels. fly -> flyay, which your function doesn’t consider as you are matching vowels.
if(c.index=0){
This is not a comparison, the operator to compare is == or === (this happen every time you have an if statement)
Interestingly, as I look over the rules for the challenge, it is unclear which they want. either the “way” or the “ay” at the end of fly. This is because the rule in the challenge states
" Pig Latin takes the first consonant (or consonant cluster) of an English word, moves it to the end of the word and suffixes an “ay”. "
it is impossible to move a cluster of consonants to the “end” of the word if the thing you are checking for does not exist. neither does it begin with a consonant.
Here is the issue… Y incorrectly is assumed to be a consonant. It can be either…
So in the case of " FLY" it is a vowel… in the case of " YOLK" it is a consonant each dependent on its usage… In this view, the correct translation would be " yflay" Unless they include some other rule that says that if the Y is within a 3 letter word then that would not apply ?
Here again I have not looked at the “official resolution” as of yet… but its a tripping point.
Thank you again for your help… !
So if the first vowel comes at the end… There is no rule in the challenge for that… AND they do not give an example of that… So for Ohio it would be Hiooway or Ohioay ? Or possibly for something like SKI the same rule would apply for the handling of the vowel preceded by a consonent cluster … and in either case… at least for me… this would be handled by the first group ? So they must be looking for something different all together I dont think this was a well thought through challenge
Yes basically when I wrote my code it did not have specific code for the situation where the vowel comes at the end of the word, because by default, my code covered that scenario by finding the first occurence of ANY vowel. But the challenge still failed it on the basis that there was not SPECIFIC code for a vowel at the end of the word.
OK so here is the code ( with the added specific code for vowel at the end of the word… what am I missing here
function translatePigLatin(str) {
str = str.toLowerCase();
console.log(str.length);
let c = /[aeiou]/.exec(str);
if (c === null) {
console.log(str + "ay");
} else if (c.index === 0) {
str = str + "way";
console.log(str);
} else if (c.index === 1) {
let r = str.substr(1);
let f = str.substr(0, 1);
str = r + f + "ay";
console.log(str);
} else if (c.index === 2) {
let r = str.substr(2);
let f = str.substr(0, 2);
str = r + f + "ay";
console.log(str);
}else if (c.index = (str.length - 1)){
str=str+"ay";
return str;
}
// console.log(str.substr(i,1));
return str;
}
translatePigLatin("ski");
I would suggest you declare your variables at the top and only do assignments inside the ifs. I would also suggest not overwriting the parameter, make a new variable.
let l, r, newStr;
You have an assignment operator in your last else if
Anyone have a clue as to what is going on with this… its really bugging me and I kind of would like to move forward. current updated version :
function translatePigLatin(str) {
str = str.toLowerCase();
console.log(str.length);
let c = /[aeiou]/.exec(str);
if (( /^[a-z].*[aeiou]$/igm).test(str)===true){
f=str.substr(0,(str.length-1));
r=str.substr(str.length-1)
str=r+f+"ay";
console.log(str);
}else
if (c === null) {
console.log(str + "ay");
} else if (c.index === 0) {
str = str + "way";
console.log(str);
} else if (c.index === 1) {
let r = str.substr(1);
let f = str.substr(0, 1);
str = r + f + "ay";
console.log(str);
} else if (c.index === 2) {
let r = str.substr(2);
let f = str.substr(0, 2);
str = r + f + "ay";
console.log(str);
}else if (c.index = (str.length - 1)){
f=str.substr(0,(str.length-1));
r=str.substr(str.length-1)
str=r+f+"ay";
console.log(str);
}
// console.log(str.substr(i,1));
return str;
}
translatePigLatin("kkkkkkkkkko");
/*
translatePigLatin("california") should return "aliforniacay".
translatePigLatin("paragraphs") should return "aragraphspay".
translatePigLatin("glove") should return "oveglay".
translatePigLatin("algorithm") should return "algorithmway".
translatePigLatin("eight") should return "eightway".
Should handle words where the first vowel comes in the end of the word.
Should handle words without vowels.
*/
OK tried this version… it also worked in console… but not here :
let regex=/aeiou/gi;
var vowelIndice = str.indexOf(regex);
let f= (str.substr(vowelIndice));
let r =str.substr(0,str.length-1);
str=f+r+"ay";
return(str);
let regex = /aeiou/gi;
var vowelIndice = str.indexOf(regex);
If you console log vowelIndice what does it return and is that what you are expecting?
Verbalize the requirements, write down the steps you need to take in order to solve this problem. Break it down into small pieces. Test your assumptions about what your code is doing each step of the way. Don’t lose sight of important things like formatting and syntax, because if you do, you can have the solution to a logical problem but fail it because of syntax errors.
One way of trying to solve this problem would be to make an array of vowels and loop over the string and vowels array doing a compare character by character. It isn’t a very elegant or fancy approach but it might help you think more carefully about the problem.