My code is quite complex,still works tho.Can i have a review?

Tell us what’s happening:
I solved this challenge of the intermediate algorithm lesson as below.I added some comments to explain what i am doing. Assuming a scenario i am solving this at my future job,would it be okay or i would have to find a more clear way to solve it?I feel a bit bad,cause i know there is a better way maybe to solve it but this is how i am thinking.Would appreciate any advice.Thanks!

Your code so far


function translatePigLatin(str) {

let myRegex=/^[aeiuo]/; //creating a regex with all vowels
let result=myRegex.test(str); //checking if starts with consonant or vowel

if(result){ //if str starts with vowel add 'way' at the end and return
  return str+=("way");
}else{ //if str doesnt start with vowel then create an array with each letter
  let newArr=str.split("");

  if(newArr.every(item=>item!=="a"&&item!=="e"&&item!=="i"&&item!=="u"&&item!=="o")) return str+="ay"; //use every method to see if there is any vowel in the array,if not return the str with 'ay' added at the end
  while(!result){ //if there is a vowel but it doesnt start with a vowel take the every first letter and transport it at the end of the array until the first letter is vowel.

    let consonant = newArr.shift();
    newArr.push(consonant);
    str=newArr.toString();
    result=myRegex.test(str);

  }
 
  newArr=newArr.join(""); //when first letter is a vowel,it breaks out of the while loop,joins the array to a string again,add 'ay' at the end and returns the str
  return str=newArr+("ay");
  
  }
}
translatePigLatin("consonant");

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.135 Safari/537.36.

Challenge: Pig Latin

Link to the challenge:

Well , if you are using the techniques you have learned and especially if
you are gaining a better understanding of what you have learned then
you are doing Great! If you have doubts I suggest you back up a few challenges.Redo your code in previous challenges by reading the narratives carefully while trying to apply the code you are being introduced to in the challenge. Practice means repetition. Is there a better way to accomplish a certain task ? I think the task here is to gain an understanding .

Nice logo you got there :wink: .Okay i get it and actually when i finish the intermediate i will repeat them again before i move on to the projects,but if you had to rate my code or say that you are a developer we are working together,could you work with this block of code or you may have had hard times to work on it?Thanks,for your time!

If you were on my development team I would give you an “interface”
exactly like the challenges are presented in this course. I would give you a function name a list of arguments and the desired return from the function. I would not be concerned with the code you chose to implement the function. Later in the development process the team would do a code review and then you may be asked to improve the speed of execution or reduce the resources used .

2 Likes