Is my code efficient? [Pig Latin]

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? :gift_heart:

First, congrats on getting a working solution, that is always the most important thing.

Now that you have solved the challenge, one thing you can do is look at the solutions in the hints. Comparing your solution to others is a great way to learn. Look at a few of them and then I think you’ll be able to answer your own question.

Just make sure that you understand the other solutions. You should know what every line does. If you get stuck on something then feel free to ask here.

Also, not all of those solutions are as “good” as the others. Efficiency is important no doubt, but so is readability. So as you are going through them consider which ones you think are easier to understand. Don’t always assume the trickiest or most concise solution is the best.

1 Like