Guest User

Untitled

a guest
Apr 24th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.47 KB | None | 0 0
  1. //messagemixer.js file
  2. const MessageMixer = {};
  3.  
  4. function palindrome(str){
  5. return `${str} ${reverseWord(str)}`;
  6. };
  7.  
  8. function pigLatin(sentence, character){
  9. return sentence.split(' ').join(character + ' ');
  10. }
  11.  
  12. function countCharacter(inputString, inputCharacter) {
  13. let count = 0;
  14. let string = inputString.toLowerCase();
  15. let character = inputCharacter.toLowerCase();
  16. for (let i = 0; i < string.length; i++) {
  17. if (string[i] === character) {
  18. count++;
  19. }
  20. }
  21. return count;
  22. };
  23.  
  24. function capitalizeFirstCharacterOfWords(string) {
  25. let arr = string.split(" ");
  26. for (let i = 0; i < arr.length; i++) {
  27. let word = arr[i];
  28. arr[i] = word[0].toUpperCase() + word.substring(1);
  29. }
  30. return arr.join(" ");
  31. };
  32.  
  33.  
  34. function reverseWord(word) {
  35. return word.split("").reverse().join("");
  36. };
  37.  
  38. function reverseAllWords(sentence) {
  39. let words = sentence.split(" ");
  40. for (let i = 0; i < words.length; i++) {
  41. words[i] = reverseWord(words[i]);
  42. }
  43. return words.join(" ");
  44. };
  45.  
  46. function replaceFirstOccurence(string, toBeReplaced, replaceWith) {
  47. return string.replace(toBeReplaced, replaceWith);
  48. };
  49.  
  50. function replaceAllOccurrences(string, toBeReplaced, replaceWith) {
  51. return string.split(toBeReplaced).join(replaceWith);
  52. };
  53.  
  54. function encode(string) {
  55. let replacementObject = { "a": "@", "s": "$", "i": "!", "o":"0" };
  56. for (let key in replacementObject) {
  57. string = replaceAllOccurrences(string, key, replacementObject[key]);
  58. }
  59. return string;
  60. };
  61. export default MessageMixer;
  62. export {encode, replaceAllOccurrences, replaceFirstOccurence, reverseAllWords, reverseWord, capitalizeFirstCharacterOfWords, countCharacter, pigLatin, palindrome};
  63.  
  64. //message.js file
  65. import MessageMixer from './messageMixer';
  66.  
  67. import {encode, replaceAllOccurrences, replaceFirstOccurence, reverseAllWords, reverseWord, capitalizeFirstCharacterOfWords, countCharacter, pigLatin, palindrome} from './messageMixer';
  68.  
  69. function displayMessage() {
  70. console.log(countCharacter("What is the color of the sky?", "t"));
  71. console.log(capitalizeFirstCharacterOfWords("What is the color of the sky?"));
  72. console.log(reverseWord("What is the color of the sky?"));
  73. console.log(reverseAllWords("What is the color of the sky?"));
  74. console.log(replaceFirstOccurence("What is the color of the sky?", "sky", "water"));
  75. console.log(encode("What is the color of the sky?"));
  76. console.log(palindrome('Basketball'));
  77. console.log(pigLatin('welcome to the NBA', 'o'));
  78. }
  79.  
  80. displayMessage();
Add Comment
Please, Sign In to add comment