Whale Talk JS Project General Question

You must select a tag to post in this category. Please find the tag relating to the section of the course you are on E.g. loops, learn-compatibility

When you ask a question, don’t forget to include a link to the exercise or project you’re dealing with!

Hey guys,

Just a quick question surrounding Whale Talk in Loops. I understand the flow and logic, but I’m kind of confused about one section, as shown below. Why is [i] in square brackets? It’s confusing because “i” is iterating through a string, not an array. What am I missing at this point? Appreciate some tips.

const input = "whales";
const vowels = ["a", "e", "i", "o", "u"];

let resultArray = [];

for (let i = 0; i < input.length; i++){
  //console.log("i is "+ i);//
//i is 0
//i is 1
//i is 2
//i is 3
//i is 4

    for (let z = 0; z < vowels.length; z++){
      if (input[i] === vowels[z]){           <<<<<<<<<<<<<here    
        if (input[i] === "e") {
          resultArray.push("ee")
        }
        else if (input[i] === "u"){
          resultArray.push("uu");
        }
        else{
          resultArray.push(input[i]);
        }
      }
  }
}

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

1 Like

We can access strings and arrays by index using square brackets. Syntax is the same for both

strings are also kind of lists of characters

2 Likes

Try reading the ‘Character access’ section of MDN’s documentation for JavaScript.

Refer this link.

This would clear your question!

You can think of strings as a kind of array. We can access individual letters in a string by their index number. so if

const input = "whales";

then:

input[0] = 'w'
input[1] = 'h'
input[2] = 'a'
// and so on
1 Like

so in this case you use the brackets to signify that you want to access the index of the array. If you wanted to access the first item in the array you would put input[0], for the second item input[1] etc. In this situation you have a variable, i, and you need to access the index of the array stored at the location in i, so as the value of i changes, you iterate through the array. So i is not in brackets because it itself is an item in an array, but because you are accessing the index of an array, and i is storing the number that corresponds to the specific thing you want.

Ok - thanks a bunch everyone!

1 Like

Hi, everyone!

I am hoping to get some help on this issue that I encounter while doing the Whale Talk Project (JavaScript course)

The instructions didn’t include my issue, but now I am intrigued by it, and I have no clue how to solve it.

Instructions:
Whale Talk
Take a phrase like ‘turpentine and turtles’ and translate it into its “whale talk” equivalent: ‘UUEEIEEAUUEE’

There are a few simple rules for translating text to whale language:

There are no consonants. Only vowels excluding “y”.
The u‘s and e‘s are extra long, so we must double them in our program.

Here is my code that it’s working just fine as Codecademy concerns.
The resolution was accepted, and the system allowed me to move on to the next lesson.

const input = ‘I can always learn something new.’;
const vowels = [‘a’, ‘e’, ‘i’, ‘o’, ‘u’];
let resultArray = ;

for (let i = 0; i < input.length; i++) {
for (let v = 0; v < vowels.length; v++) {
if (input[i] === vowels[v]) {
/* for 9. this was the code then I need only modify for 10.*/
if(input[i] === ‘e’) {
resultArray.push(‘ee’)
}
else if(input[i] === ‘u’) {
resultArray.push(‘uu’);
}
else {
resultArray.push(input[i]);
}
}
}
};
console.log(resultArray.join(’’).toUpperCase());
// AAAEEAOEEIEE

But…

When I look closer into my ‘Whale Talk’, I noticed that I am missing an ‘I’ on it at the beginning of the word.
I already tested out replacing it with a lower case letter, which would include the letter ‘I’ to the terminal.

How do I tell the program to sort vowels, including upper and lower case?

I couldn’t find the answer, and it hasn’t been part of my course yet.

Any help would be appreciated.

You could temporarily turn that element of the string to lowercase. When you check conditions, you can add methods within the condition:

if (someArray.split("x")[0] === 1){
//code
}

I’m trying to just give you a hint, but if you need more help, feel free to ask!

All right, @codeneutrino!

As I said, it’s more a matter of curiosity at this point, but I will give some thoughts on your suggestion to see if I can figure it out.
Thank you for your hint for now.

1 Like

Hey @mmsink,

Not sure if you’re still poking around on this, but I just did it today. The solution I came up with was just to save my array of vowels as all uppercase letters and convert the letter I’m comparing from my input string to uppercase, then completing the rest of it more or less normally, like so:

1 Like

Hi, @quintnc!

You gotta man.
It worked perfectly and I learned a bit more from this lesson.
Thank you for your help.

const input = ‘I can always learn something new.’;

const vowels = [‘A’, ‘E’, ‘I’, ‘O’, ‘U’];

let resultArray = ;

for (let i = 0; i < input.length; i++) {

for (let v = 0; v < vowels.length; v++) {

if (input[i].toUpperCase() === vowels[v]) {

/* for 9. this was the code then I need only modify for 10.*/

  if(input[i].toUpperCase() === 'U' || input[i] === 'E'){

    resultArray.push(vowels[v], vowels[v]);

  } else {

resultArray.push(vowels[v]);

  }

}

}

};

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

// AAEEAOEEIEE

//Now it prints IAAAEAOEIE

1 Like

Sorry, about misspelling your name here.

@quintonc