Advertisement
makispaiktis

Codecademy - 20th Exercise (Loops in Javascript)

Dec 16th, 2019 (edited)
476
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  
  3. JAVASCRIPT: ARRAYS, LOOPS, AND OBJECTS
  4. Whale Talk
  5. Take a phrase like ‘turpentine and turtles’ and translate it into its “whale talk” equivalent: ‘UUEEIEEAUUEE’.
  6.  
  7. There are a few simple rules for translating text to whale language:
  8.  
  9. There are no consonants. Only vowels excluding “y”.
  10. The u‘s and e‘s are extra long, so we must double them in our program.
  11. Once we have converted text to the whale language, the result is sung slowly, as is a custom in the ocean.
  12.  
  13. To accomplish this translation, we can use our knowledge of loops. Let’s get started!
  14.  
  15. 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.
  16.  
  17.  
  18. Inside the second for loop, write a code block that compares the input letter to every letter in the vowels array.
  19.  
  20. 9.
  21. Whales double their e‘s and the u‘s in their language.
  22.  
  23. 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.
  24.  
  25. 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.
  26. */
  27.  
  28.  
  29. let input = "turpentine and turtles";
  30. let vowels = ['a', 'e', 'i', 'o', 'u'];
  31. let resultArray = [];
  32.  
  33. for(let i=0; i<input.length; i++){
  34.   for(let j=0; j<vowels.length; j++){
  35.     if(input[i] === vowels[j]){
  36.       if(input[i] === 'e' || input[i] === 'u'){
  37.         resultArray.push(input[i]);
  38.         resultArray.push(input[i]);
  39.       }
  40.       else{
  41.         resultArray.push(input[i]);
  42.       }
  43.     }
  44.   }
  45. }
  46. console.log("Initial List: " + resultArray);
  47. for(let i=0; i<resultArray.length; i++){
  48.   resultArray[i] = resultArray[i].toUpperCase();
  49. }
  50. console.log(resultArray.join(''));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement