Here is the problem that I’m working on.
My Code:
function translatePigLatin(str) {
const vowel = ['a','e','i','o','u'];
let first = str[0];
let vC = 0;
//vCheck is a function to check vowel's existence
function vCheck(x){
for(let i=0; i<vowel.length; i++){
if(x === vowel[i]){
vC += 1;
}
}
if(vC > 0){
return true;
}else{
return false;
}
}
//this is for the words that starts with vowels
if(vCheck(first)){
return str.concat('way');
}else{
//this will handle the words starts with consonant
let cArr = [];
for(let i=0; i<str.length; i++){
if(vCheck(str[i]) === false){
cArr.push(str[i]);
}
}
let joinArr = cArr.join("")+'ay';
let mainArr = str.slice(cArr.length);
return mainArr.concat(joinArr);
}
}
Please feel free to share your better idea? ![]()