Pig Latin splice not working as expected

Tell us what’s happening:
Hi, so here I am using splice to remove the first consonant from the array and return the altered array. But the console suggests that the variable ‘result’ contains only the removed element? I tried to use an index number directly as well but, still not working. Can someone tell me why this is doing so?

Your code so far


function translatePigLatin(str) {
  let firstCon = str.match(/[^aeiou]/);
  let result = str.split("").splice(0,1);
  //.splice(str.indexOf(firstCon, 1).push(firstCon + "ay").join("");
  console.log(result)
  return result
  

}

translatePigLatin("consonant");

Your browser information:

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

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

Return Value

An array containing the deleted elements. If only one element is removed, an array of one element is returned. If no elements are removed, an empty array is returned.

MDN is a great resource for web development. I use it all the time to check the details of functions. In the case of splice: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice

Return value
An array containing the deleted elements. If only one element is removed, an array of one element is returned. If no elements are removed, an empty array is returned.

So it modifies the array and returns the removed element. You can just use the modified array. Now if you want to keep the original array intact, don’t use splice or run it on a copy.

ahh, so it doesn’t return the amended array at all! I must have dreamt all of this then. Thanks for sharing the page, I had been staring at it for quite a while but somehow just missed that specific paragraph.

Learning how to find the relavant information in documentation is a skill you build up over time. :smiley:

1 Like

I agree MDN is the best developer for JS methods