1
function pigIt(str){
  let latin = str.split(' ').map(x => x.slice(0,1) + 'ay')
  let latinTwo = str.split(' ').map(x => x.slice(1, str.length-1))
  for (let i = 0; i < latin.length; i++) {
     let pig = latinTwo[i].concat(latin[i])
     console.log(pig)
  } 
}

Testcase: Test.assertEquals(pigIt('Pig latin is cool'),'igPay atinlay siay oolcay')

My current array of pig contains = igPay atinlay siay oolcay

I am wondering how I can join this into a full sentence, I've tried using the join method... Any help is much appreciated!

I've tried using the join method however I get an error code.

Kevin Phan
  • 11
  • 1
  • 1
    Please [edit] the question and show how you tried the join call. – Peter B Feb 14 '23 at 01:06
  • Have you tried `return latinTwo.join(' ');`? – Unmitigated Feb 14 '23 at 01:13
  • `string.split("")` counterintuitively splits by UTF-16 code units, which is not something you usually want. [Do _not_ use `.split("")`](/a/38901550/4642212). `slice`, `substring`, and the deprecated `substr` have the same problem. Use `Array.from(string)` to split by characters instead. – Sebastian Simon Feb 14 '23 at 20:07

2 Answers2

0

A simpler approach:

function pigIt(s) { 
  return s.split(' ').map(i=>i.substring(1)+i.charAt(0)+'ay').join(' ')
}

console.log(pigIt('Pig latin is cool'))

you can also do:

function pigIt(s) {
  return s.split(' ').map(([a,...b])=>[...b,a,'ay'].join('')).join(' ')
}

console.log(pigIt('Pig latin is cool'))
Andrew Parks
  • 6,358
  • 2
  • 12
  • 27
0

You are in right direction, You just have to assign the concatenated string into a variable instead of reassigning it to a whole variable again.

Note : .join method is helpful for arrays, for strings .concat is the method to join.

Live Demo :

function pigIt(str) {
  let pig = ''
  let latin = str.split(' ').map(x => x.slice(0,1) + 'ay')
  let latinTwo = str.split(' ').map(x => x.slice(1, str.length-1))
  for (let i = 0; i < latin.length; i++) {
     pig += latinTwo[i].concat(latin[i], " ")
  }
  return pig;
}

console.log(pigIt('Pig latin is cool').trim());
Debug Diva
  • 26,058
  • 13
  • 70
  • 123