It works, plus itâs not hard to understand. Itâs not bad code
You can eliminate the inner for-loop by using includes().
for (i = 0; i < strArr.length; i++) {
if (vowels.includes(str[0])) return str + 'way';
if (vowels.includes(str[i])) return str.substr(i) + newArr.join('') + 'ay';
newArr.push(strArr[i]);
}
Or you can go the regex route.
var vowels = /[aeiuo]/;
for (i = 0; i < strArr.length; i++) {
if (vowels.test(str[0])) return str + 'way';
if (vowels.test(str[i])) return str.substr(i) + newArr.join('') + 'ay';
newArr.push(strArr[i]);
}
You didnât have to create the strArr array. You can directly access characters from a string as if they were arrays themselves.
for (i = 0; i < str.length; i++) {
for (j = 0; j < vowels.length; j++) {
if (str[0] === vowels[j]) {
// ...
The idea in coding is to make it look as simple as possible, So one of kevcomediaâs first two solutions get the big tick from me. However, imho the âfantastic one-linerâ maybe âcleverâ and great for a cryptic competition, but bad practice for general coding. Imagine youâre modifying someone elses code and you come across this - it would take (me) ages to work out what it does.
Well, what a way to be summoned. Now that I am here though, I may as well give my two cents on the subject.
@nikrb, you are of course completely right though I am quite certain you already know that. You have that debonair feeling around you. It is naturally true that using a js method in the way it was supposed to be used is a crime and I should be verbally abused for it.
Furthermore, I feel I should warn you about other such chicanery. This is why I will link another two examples of similar garbage code written by âcleverâ people:
The first one is from create-react-app and is written by the same people who develop React. The second one is from jQuery. Iâd advise you to stay far away from these garbage libraries and forgo the use of jQuery and React in your code.
You should instead try to adopt the imperative solutions that you could so âeasilyâ read. You âunderstoodâ the code in those so well, you actually failed to notice that for certain cases these solutions would return the helpful undefined instead of a string containing either the original input or the original input with âayâ at the end (the challenge rules donât really specify what should happen but these are cases that certainly appear when chatting or texting). Once that happens and you attempt to do anything with the data you will inevitably get the even more helpful uncaught type error that is usually associated with such âwellâ written code.
It seems obvious that working code should be the first step, but youâd be surprised at how often developers get caught up in trying to âmake it rightâ before theyâve even made it work.
So what does âmake it rightâ mean? How do we know that code is ârightâ?
Code is ârightâ when it works as expected and is easy to change. Code is easy to change when itâs easy to read and understand. And code is easy to read understand when it has the following properties:
clear, honest, and descriptive variable and function names
descibes what it is doing, moreso than how it is doing it
is unencumbered by dependencies
has fewer steps, but not so few that it becomes hard to follow
If I were refactoring your code, I would strive to eliminate the nested for loops and complex if/else-if statement. I would be looking to end up with something like:
function translatePigLatin(word) {
var letters = word.split('');
var preConsonants = [];
while(letters.length > 0 &&
!'aeiou'.includes(letters[0])) {
preConsonants.push(letters.shift());
}
return letters.concat(preConsonants).join('') +
((preConsonants.length > 0) ? 'ay' : 'way');
}
@rmdawson71 I stopped at âmake it workâ. Looks like you got to âmake it fastâ. I like the use of match and indexOf to eliminate the explicit loop. Very nice!
P.S. [SOLVED] You have a small typo on line 4: index should be vowelPosition