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

Hi, this is my code, I worked alongside the training video. As far as I can see my code is the same as the video and yet it has totally different output… why? i have started testing it as the final steps advise, and it looks totally different to the video…

const input = ‘turpentine and turtles’;
const vowels = [“a”, “e”, “i” , “o”, “u”];

let resultArray = ;

for (let inputIndex = 0; inputIndex < input.length; inputIndex++){
for (let vowel = 0; vowel < vowels.length; vowel++){
if (input[inputIndex] === vowels[vowel]){
}
if (input[inputIndex] === ‘e’){
resultArray.push(‘ee’) }
else if (input[inputIndex] === ‘u’){
resultArray.push(‘uu’);
}
else {
resultArray.push(input[inputIndex]);
}
}
};
console.log(resultArray);

//.join(’’).toUpperCase());
//console.log('vowel is '+ vowel);

//console.log("inputIndex = " + input[inputIndex]);

My code is the one running vertically!
Feedback would be greatly appreciated, I am a clueless beginner!!!


Hello. My apologies the reply took so long.

I have made some adjustments to the code you shared below, and included some comments give information on what the code does, what error was, and one way to solve it:

const vowels = ["a", "e", "i" , "o", "u"]; let input = "turpentine and turtles"; let resultArray = []; // Before we start working on the input we convert it to lowercase so we don't // leave out some input characters when performing the === comparison input = input.toLowerCase(); for (let inputIndex = 0; inputIndex < input.length; inputIndex++) { for (let vowel = 0; vowel < vowels.length; vowel++){ // Here you had created an if block, but didn't populate it with any code. if (input[inputIndex] === vowels[vowel]){ // The if block is supposed to check whether the current input character is a vowel. // If it is, we push it to the result array. resultArray.push(input[inputIndex]); } } // The above block will ensure all vowels are pushed to the result array // In the special case that a vowel is an "e" or a "u", we are supposed to push // an extra vowel to the result array. That's what the if block below is for. // Here's where the main problem lay: The second if block wasn't supposed to be included // in the above for loop. Otherwise every single "e" and "u" will be pushed to the console // each time the for loop runs, instead of just once as it should be. if (input[inputIndex] === "e"){ resultArray.push("e"); } else if (input[inputIndex] === "u"){ resultArray.push("u"); } } // After the above for block is executed we join the result array elements into a single string, then convert it to uppercase. resultArray = resultArray.join('').toUpperCase(); console.log(resultArray);

Hope this was able to clear some things up. If you still have questions feel free to ask.

The logic side of the code can sometimes be confusing (and frustrating!), especially when you’re just starting out. I had to tinker with the code a couple of times myself before I realized where the problem was. But one gets better at it with time and practice. And it helps to remember that everyone felt the same way once, even the gurus. You’re on the right track. Keep it up.

Happy coding.