Arrays,Functions, String

Hello everyone. Can someone help me with this exercise? I don’t understand, what is wrong with the code.

Thanks in advance.

  1. Pig Latin Translation.**

Create a function that takes a string of words and moves the first letter of each word to the end of it, then adds ‘ay’ to the end of the word. This is a basic form of “Pig Latin”.

  • Move the first letter of each word to the end of the word.

  • Add “ay” to the end of the word.

  • Words starting with a vowel (A, E, I, O, U) append “way” to the end instead.

Extra Practice

  • Preserve proper capitalization as in the examples below.

Examples:

  • pigLatin(“Cats are great pets.”) ➞ “Atscay areway reatgay etspay.”

  • pigLatin(“Tom got a small piece of pie.”) ➞ “Omtay otgay away allsmay iecepay ofway iepay.”

  • pigLatin(“He told us a very exciting tale.”) ➞ “Ehay oldtay usway away eryvay excitingway aletay.”

const pigLatin=(str)=>{
    let strArray= str.split(" ");
    strArray.pop();
    let resultArray = [];
    let vowelsArray=["a","e","i","o","u"]
    for(let i=0;i<strArray.length;i++) 
      if(vowelsArray.includes(strArray[i][0])){
         strArray[i].slice(1) + strArray[i][0];
        strAway.push("way");
      } else {
        word += "ay";
      }
      word = word.toLowerCase();
      word = word[0].toUpperCase() + word.slice(1);
      resultArray.push(word);
    }
    return resultArray.join(" ") + ".";
}
const pigLatin=(str)=>{
    let strArray= str.split(" ");
    strArray.pop();
    let resultArray = [];
    let vowelsArray=["a","e","i","o","u"]
    for(let i=0;i<strArray.length;i++){
      
      let word = strArray[i].slice(1) + strArray[i][0];
     
      if(vowelsArray.includes(strArray[i][0])){
        word += "way";
      } else {
        word += "ay";
      }
      word = word.toLowerCase();
      word = word[0].toUpperCase() + word.slice(1);
      resultArray.push(word);
    }
    return resultArray.join(" ") + ".";
}
    console.log(pigLatin("Cats are great pets."));//Atscay Reaway Reatgay.
    console.log(pigLatin("Tom got a small piece of pie."));//Omtay Otgay Away Mallsay Iecepay Foway.
    console.log(pigLatin("He told us a very exciting tale."));//Ehay Oldtay Suway Away Eryvay Xcitingeway.

Hi,

Before we can input some thoughts, can you explain what is your idea of the expected behavior and what’s happening instead (from your attempts at debugging).

2 Likes

this is the output that I should get:

  • pigLatin(“Cats are great pets.”) ➞ “Atscay areway reatgay etspay.”
  • pigLatin(“Tom got a small piece of pie.”) ➞ “Omtay otgay away allsmay iecepay ofway iepay.”
  • pigLatin(“He told us a very exciting tale.”) ➞ “Ehay oldtay usway away eryvay excitingway aletay.”
console.log(pigLatin("Cats are great pets."));//Atscay Reaway Reatgay.
    console.log(pigLatin("Tom got a small piece of pie."));//Omtay Otgay Away Mallsay Iecepay Foway.
    console.log(pigLatin("He told us a very exciting tale."));//Ehay Oldtay Suway Away Eryvay Xcitingeway.


[/quote]

What was your process for trying to debug?