Whale Talk Project

Here’s my code for the Whale Talk Project:

Any feedback would be greatly appreciated.

Happy Coding.

1 Like

Here’s mine if you want to compare. I tested with the controls inputs in the last step, seems to work.

Is it prefereable to leave an if/else if without an else? or just do two ifs?

const input = "turpentine and turtles";
const vowels = ['a', 'e', 'i', 'o', 'u'];

let resultArray = [];

for (let i = 0; i < input.length; i++) {
  for (let j = 0; j < vowels.length; j++) {
    if (input[i].toLowerCase() === vowels[j]){
      resultArray.push(input[i].toLowerCase());
   }
 }
 if (input[i].toLowerCase() === 'e') {
   resultArray.push(input[i].toLowerCase());
 } else if (input[i].toLowerCase() === 'u') {
   resultArray.push(input[i].toLowerCase());
 }
}

console.log((resultArray.join('')).toUpperCase());

Hi there. Thank you for sharing.

With regard to which one is better, I’m not quite sure there will be a significant difference in performance or efficiency.

That said, I think using one if statement is provides better readability. If you can couple both conditions together using an or operator, I can’t find any reason why you would want to add an extra else if statement.

“Remember that there is no code faster than no code.”

~ Taligent’s Guide to Designing Programs.

If a piece of code isn’t absolutely essential, eliminate it.

Here’s mine:
`const input = ‘Greetings Humans’;
const vowels =[‘a’,‘e’,‘i’,‘o’,‘u’];
const resultArray = ;

for(let i=0; i < input.length; i++){
let letter = input.slice(i,i+1);
for(let j = 0; j < vowels.length; j++){
if (letter === vowels[j]) {
letter === ‘e’? resultArray.push(‘EE’):
letter === ‘u’? resultArray.push(‘UU’):
resultArray.push(vowels[j].toUpperCase());
}
}
}
console.log(resultArray.join(’’));
`

So I have done the exercise (kinda) but I found an issue and I’m not sure it’s meant to be covered in the exercise, but would appreciate some help in solving it. I noticed that if any of the vowels are capitalized, they are not pushed to the resultArray. It’s not an issue in most of the other solutions I have looked at because the phrases are short and there aren’t any capitalized vowels (or if there are people aren’t noticing them not being pushed). To highlight this I entered my input string as…

‘bEttEr to rEmain silEnt and thought a fool than to spEak and rEmovE all doubt’

and my code returned this…

OAIIAOUUAOOAOAAOAOUU.

I realise I need to change all caps to lowercase for them to push to the resultArray, but when I try to involve that method in my existing loops it goes wrong. Here’s my final code

Can anyone suggest where I need to put .toLowerCase and whether it needs to be on the input or the resultArray?

Thank you in advance for any assistance.

Why is it almost as soon as I post a query like this, I get an idea and it usually works :crazy_face:
I’m not sure this is the most efficient way to solve my issue, but it seems to work.

I just declared the string as a new variable and had input = new variable (the string) .toLowerCase. Here’s the code

Edit: Sorry I should add the console logs this as the result now…

‘EEEEOEEAIIEEAOUUAOOAOEEAAEEOEEAOUU’

Hello @bilsner.

Dang. I didn’t even realize my code doesn’t take care of uppercase letters in the input until you pointed it out. Thank you.

Your code looks solid, and converting all input to lowercase first provides an elegant solution. Before I saw your solution, I was going to suggest the following approach:

let input = 'bEttEr to rEmain silEnt and thought a fool than to spEak and rEmovE all doubt';
const vowels = ['a', 'e', 'i', 'o', 'u', ];
let resultArray = [];

for (i = 0; i < input.length; i++) {
  for (j = 0; j < vowels.length; j++){
    // notice the  toLowerCase call that converts an input letter to lowercase first before making the comparison
    if (input[i].toLowerCase() === vowels[j]) {
      resultArray.push(input[i].toUpperCase());
    }
  }
   // notice it here as well
   if (input[i].toLowerCase() === 'e' || input[i].toLowerCase() === 'u') {
    resultArray.push(input[i].toUpperCase());
  } 
}

console.log(resultArray.join(''));

Both approaches accomplish the same thing, but I would argue yours is better.
It requires less code and is more readable. It also reduces chances you’ll make an error or forget to include to include the toLowerCase call if/when you’re required to add more conditions. Well done.

I fixed my code. Here it is:

Thanks!

PS:

I think it’s because by sharing or expressing your problem to someone else helps you understand it better, and often you get the solution. That’s why rubber duck debugging is encouraged. :smile: Never change. Happy coding!

1 Like

Whatever works I guess, Looking at yours it’s immediately clear what is going on so I think that’s ok isn’t it? I love that bit about the rubber duck hahah.

About the exercise, I think it’s most likely a case of that part wasn’t really the ‘teachable moment’ part and I only noticed it because I put in such a ridiculously long quote. Either way, looks like we both ended up learning more in the run, which has to be a win for all concerned :+1:

1 Like