Where is my error? Help please!


#1

My Project Whale Talk code did not produce the expected result, and I am hoping someone might be willing to help me figure out where I messed up. I started with the sentence “I can speak Italian” and my final output was “II CCAAANN SSPPEEEAAAKK IITTAAALLIIIAAANN”. I included my code below (both as a picture and typed out). Any input would be greatly appreciated!!!

var input = ‘I can speak Italian’;
var vowels = [‘a’, ‘e’, ‘i’, ‘o’, ‘u’];
var resultArray = [];
for (var i = 0; i < input.length; i++) {
for (var j = 0; j < vowels.length; j++) {
if (input[i] === vowels[j]) {
resultArray.push(input[i]);
}
}
if (input[i] = ‘e’) {
resultArray.push(input[i]);
}
if (input[i] = ‘u’) {
resultArray.push(input[i]);
}
}
console.log(resultArray.join(’’).toUpperCase());


#2

If you add console.log’s in your code you can print out what you’re doing as you do it, and then examine that output to figure out where it goes wrong, after which you can look closer at the part that should have caused it to behave differently (narrow down the source of the problem)

Also, just a thought, 'aoeui' is easier to type than ['a', 'o', 'e', 'u', 'i'] and both those data types support lookup by index and have lengths


#3
const input = 'Whales are the biggest animal in sea';
const vowels = ['a', 'e', 'i', 'o', 'u'];
let resultArray = [];

for (let inputIndex = 0; inputIndex < input.length; inputIndex++){
 for (let vowelsIndex = 0; vowelsIndex < vowels.lenght; vowelsIndex++){
    if(input[inputIndex] === vowels[vowelsIndex]){
      resultArray.push(input[inputIndex]);
      console.log(resultArray.length)
   }
 }
	if(input[inputIndex] === 'e' || input[inputIndex] === 'u' ){
   resultArray.push(input[inputIndex]);
		}
 }
  console.log(resultArray.join('').toUpperCase());

I did it like this but my output is ‘EEEEE’


#4

Hi @objectjumper55253,

Stumble upon your question. Unsure if you’re still stuck or progress further. You have one spelling error of length in your code, causing your code to be faulty. Locate the spelling error and change that. And the console.log(resultArray.length) doesn’t contribute much to the project code, could be omitted.

Hope this can help others who are facing roadblocks and learn to debug your code as well.