Feedback on pigLatin function code


#1

Hi everyone, I just finished the exercises in fundamentals 5 and was looking for some feedback on the pigLatin function. The provided solution looks much different than mine despite them both achieving the same goal. So I was wondering if my code is okay or if I should try to make it more like the “official” solution. The biggest red flag to me about my current code is the flustercluck of a regex that I have. Am I right in assuming that good code should simplify regex’s as much as possible?
Thanks!

function translate(input) {
	const ending = 'ay';
	const consoTest = /^[^aieou]+[q][u]|^[q][u]|^[^aieou]+/gi;
    let singleWords = input.split(' ');
	return singleWords.map(word => {
    	if (word.toString().search(consoTest) < 0) {
			return word.concat(ending);
        } else if (word.toString().search(consoTest) >= 0 ) {
			return word
	                        .concat(word.match(consoTest).toString())
				.concat(ending)
				.replace(consoTest, '');
		}
	})
		.join(' ')
}

#2

If it works then that’s ok. Sometimes regex is complicated but you’ve done the right thing in wrapping it up in a variable. You could consider a better name for the variable perhaps.