Here’s my code for the Whale Talk Project:
Any feedback would be greatly appreciated.
Happy Coding.
Here’s my code for the Whale Talk Project:
Any feedback would be greatly appreciated.
Happy Coding.
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.”
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
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. Never change. Happy coding!
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
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:
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.
For my version I did two functions that did the complete opposite.
Hello,
Just wanted some clarification on some items on this project. While going alongside the video I’ve been stuck trying to figure out this one piece of code. Basically what I’m trying to figure out is why would ‘inputIndex’ be used and ‘i = 0;’ won’t output the numbers alongside.
Why would this code work:
vs. this code:
I seem to get stuck in this section and have gone through this specific project twice. Any help would be greatly appreciated.
Hey there. The code doesn’t render the i
in the second case because you are logging input
to the console instead:
console.log(`InputIndex is: ${input}`)
It should be:
console.log(`InputIndex is: ${i}`)
LOL. Thanks. I definitely needed another set of eyes. I must just need to take a break from learning today. Have a wonderful night Peter.
Hehe, happens to the best of us. It’s actually morning where I’m from but thanks. Have a good night as well.
Hello there.
I have a question for the code on the whale talk project.
This is my code:
const input = 'Hi, Human';
const vowels = ['a', 'e', 'i', 'o', 'u']
let resultArray = [];
for ( let inputIndex = 0; inputIndex < input.length; inputIndex++){
//console.log(inputIndex)
if (input[inputIndex] === 'e') {
resultArray.push(input[inputIndex]);
}
if (input[inputIndex] === 'u') {
resultArray.push(input[inputIndex]);
}
for (let vowelsIndex = 0; vowelsIndex < vowels.length; vowelsIndex++){
//console.log(vowelsIndex)
if (input[inputIndex] === vowels[vowelsIndex]){
//console.log(input[inputIndex])
resultArray.push(input[inputIndex]);
}
}
};
//console.log(input[0])
//console.log(resultArray)
const resultString = resultArray.join('').toUpperCase();
console.log(resultString)
It works as it supposed to be working, but i have question here about the If statement when we compare the two indexes. Why it is inside the second for loop and not the first one? Even, why it needs to be inside the for loops and not after the for loops?