Projects

Whale Talk

Ever wanted to talk to whales like Dory? Well with this code you can!

This is a JavaScript project where it takes text and translates it into whale talk.

Project Overview

In this project, I created a simple program that translates English text into "whale talk" by extracting and repeating certain vowels. The program processes the input string, filters out non-vowel characters, and constructs a new string that represents how whales might "speak".

Result

Enter text and click Translate (or press Enter) to convert to whale talk.

Method

First thing I did was get the program to read the text from the input field. I also added a conditional to tell the code to do nothing if the text field is empty.


    // Read text from the input field (fall back to default if empty)
    const inputEl = document.getElementById('input');
    let text = inputEl ? inputEl.value : '';
    if (!text) {
        return; // do nothing if input is empty
    }
        

Next I used .toLowerCase() to transform all the text into lowercase and created arrays for the vowels and an empty array for the code to place the resulting "whale talk".


    // Work with a lowercase copy for matching
    const input = text.toLowerCase();
    const vowels = ['a', 'e', 'i', 'o', 'u'];
    // Create an empty array
    let resultArray = [];
        

After getting everything set up, it is time to do a for loop using the vowel array to get to only focus on the vowels in the text. The rule for converting to "whale talk" is to remove all consonants and doubling the vowel if it is 'e' or 'u'.


    // Loop through each character of the input and check vowels
    for (let i = 0; i < input.length; i++) {
        for (let j = 0; j < vowels.length; j++) {
            // checks for e and u: then doubles them
            if (input[i] === vowels[j]) {
                if (input[i] === 'e' || input[i] === 'u') {
                    resultArray.push(input[i], input[i]);
                } else {
                    resultArray.push(input[i]);
                }
            }
        }
    }
        

Finally it is time to print the string to console. However, in order to write an array as a string you must use the .join() function.


    // Declare a variable that joins and capitalises the string
    let resultString = resultArray.join('').toUpperCase();