Why last case does not match

Tell us what’s happening:

  **Your code so far**

function translatePigLatin(str) {
var result = str.split('');
let j = 0
let value = 0

function changeArray(array,index){
  var finalArray = '';
  var firstPart = array.splice(0,index);
  firstPart.map(item=> array.push(item))
  finalArray = array.join('')+'ay';
  return finalArray;
}

while (j<=result.length) {
    if (result[j].match(/[aeiou]/gi)) {
    	break;
 	  }
  j = j + 1;
  value = value + 1;
}


for(var i = 0; i<=result.length;i++){
  if(value > 0){
    return changeArray(result,value);
  } else if(result[i].match(/[aeiou]/gi)){
    return result.join('')+'way';
  } else {
    return result.join('')+'ay';
  }
}
}

translatePigLatin("consonant");
  **Your browser information:**

User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.114 Safari/537.36.

Challenge: Pig Latin

Link to the challenge:

It is failing because the word rhythm has no vowels and your code is not designed to handle that

///It is in this block of code where the error is coming from
while (j<=result.length) {
      if (result[j].match(/[aeiou]/gi)) {
        break;
      }
    j = j + 1;
    value = value + 1;
  }

Thank you very much for your help @caryaharper

I managed to pass the test with this code

I know it is ugly and not very handy but I can say that I understand it, any suggestions to writer more efficient and clean code?

function translatePigLatin(str) {
  var result = str.split('');
  let j = 0
  let value = 0

  function changeArray(array,index){
    var finalArray = '';
    var firstPart = array.splice(0,index);
    firstPart.map(item=> array.push(item))
    finalArray = array.join('')+'ay';
    return finalArray;
  }
  
  var allConsonantsRegex = /^[^aeiou]+$/i;
  
  if (allConsonantsRegex.test(str)) {
    return str + 'ay';
  } else {
    while (j<=result.length) {
      if (result[j].match(/[aeiou]/gi)) {
        break;
      }
      j = j + 1;
      value = value + 1;
    }
  };
  
  for(var i = 0; i<=result.length;i++){
    if(value > 0){
      return changeArray(result,value);
    } else if(value <= 0){
      return result.join('')+'way';
    } 
  }
}

translatePigLatin("rhythm");
1 Like

Take a look at the solutions in the Guide. You will see various other approaches that are much simpler than your approach. You should be able to review those and figure out how best to remove the more complex parts of your solution.

Thank you @RandellDawson

I have an extra question @caryaharper @RandellDawson , what do you think is the best method to learn with freeCodeCamp?

  1. Force myself as much and as hard as possible(this one took me about 5 hours) to solve the challenge with my own previous knowledge even if it generates this complex solutions
    or

  2. Force myself a little bit for some minutes and if I can not solve then, look at the solutions, and move forward

What would you recommend?

There is no need to rush and complete a challenge, if you can’t find a working solution immediately that is fine, and sometimes you just need to step away and do something else. Go back and look at some lessons, ask people on the fcc forum, search stack overflow, and last of all it never hurts to read some documentation. Learning code is not a race, but as someone who started learning code for a job profession I can absolutely understand wanting to just get everything done, and know everything, but it takes time, and that amount of time it takes is exactly as long as it takes you.