Is my code good enough?

Good evening, just passed “Pig Latin” and wanted to ask, does my code is good enough to go further? Asking cuz I don’t feel very comfortable at this point and I have no idea if i will break through at the future.
Thanks in advance! :slight_smile:

Code:

function translatePigLatin(str) {
  str = str.split("");
  var vowels = 'aeiou';
  for (var i=0; i<str.length; i) {
    if (!/a|e|i|o|u/.test(str)) {
      str.push("ay");
      break;
    } else if (vowels.indexOf(str[i]) == -1) {
      str.push(str[i]);
      str.shift();
      if (vowels.indexOf(str[i]) !== -1) {
        str.push("ay")
        break;
      }
    } else if (vowels.indexOf(str[i]) !== -1) {
        str.push("way");
        break;
    }
  }
  str = str.join('');
  console.log(str);
  return str;
}
translatePigLatin("california");

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/pig-latin/

I think you shouldn’t worry about how “good” your code is especially when you are beginning. The only focus you should have is being able to complete challenges for now.

Cheers :slight_smile:

Like Wayne Gretzky said: “They don’t ask ‘how?’; they ask ‘how many?’”

Of course, polishing up is good for everybody. Just have a look at this from time to time to remind yourself of where you could be doing it cleaner:

You don’t write code for it to be good, you write it to solve the problem.
In your case “the problem” was failing test cases. Your code solved it. Done. Next.

1 Like
  • its good to think about conciseness and readability
  • no repeating yourself and no unnecessarily complicated code
  • for this level of problem-solving its good enough as long as you pass the tests youre fine
  • if you want you can try to think of ways to make your code more simple or easier to read
  • maybe try out some functional programming. write functions that do only one thing and used them in your main function
2 Likes

I can’t emphasis how wrong this is.

You don’t write code for it to be good, you write it to solve the problem.
In your case “the problem” was failing test cases. Your code solved it. Done. Next.