Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- JAVASCRIPT: ARRAYS, LOOPS, AND OBJECTS
- 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.
- Once we have converted text to the whale language, the result is sung slowly, as is a custom in the ocean.
- To accomplish this translation, we can use our knowledge of loops. Let’s get started!
- If you get stuck during this project or would like to see an experienced developer work through it, click “Get Help“ to see a project walkthrough video.
- Inside the second for loop, write a code block that compares the input letter to every letter in the vowels array.
- 9.
- Whales double their e‘s and the u‘s in their language.
- Write an if statement that checks if each letter in the input string is equal to 'e'. If so, .push() input[i] to the resultArray.
- Note, this statement belongs after the inner for loop block inside the outer for loop. This is because you only want to perform this check once for every letter in the input.
- */
- let input = "turpentine and turtles";
- let 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] === vowels[j]){
- if(input[i] === 'e' || input[i] === 'u'){
- resultArray.push(input[i]);
- resultArray.push(input[i]);
- }
- else{
- resultArray.push(input[i]);
- }
- }
- }
- }
- console.log("Initial List: " + resultArray);
- for(let i=0; i<resultArray.length; i++){
- resultArray[i] = resultArray[i].toUpperCase();
- }
- console.log(resultArray.join(''));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement