Created
May 2, 2020 21:47
-
-
Save stefancoding7/b089a970c087b734870475f1353e0a51 to your computer and use it in GitHub Desktop.
Message Mixer Codecademy solution javascript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { countCharacter, capitalizeFirstCharacterOfWords, reverseWord, reverseAllWords, replaceFirstOccurence, replaceAllOccurrences, encode, palindrome, pigLatin} from './messageMixer'; | |
function displayMessage() { | |
console.log(countCharacter("What is the color of the sky?", "t")); | |
console.log(capitalizeFirstCharacterOfWords("What is the color of the sky?")); | |
console.log(reverseWord("What is the color of the sky?")); | |
console.log(reverseAllWords("What is the color of the sky?")); | |
console.log(replaceFirstOccurence("What is the color of the sky?", "sky", "water")); | |
console.log(encode("What is the color of the sky?")); | |
console.log(palindrome("What is the color of the sky?")); | |
console.log(pigLatin("What is the color of the sky?", "yq ")); | |
} | |
displayMessage(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function countCharacter(inputString, inputCharacter) { | |
let count = 0; | |
let string = inputString.toLowerCase(); | |
let character = inputCharacter.toLowerCase(); | |
for (let i = 0; i < string.length; i++) { | |
if (string[i] === character) { | |
count++; | |
} | |
} | |
return count; | |
}; | |
function reverseAllWords(sentence) { | |
let words = sentence.split(" "); | |
for (let i = 0; i < words.length; i++) { | |
words[i] = reverseWord(words[i]); | |
} | |
return words.join(" "); | |
}; | |
function capitalizeFirstCharacterOfWords(string) { | |
let arr = string.split(" "); | |
for (let i = 0; i < arr.length; i++) { | |
let word = arr[i]; | |
arr[i] = word[0].toUpperCase() + word.substring(1); | |
} | |
return arr.join(" "); | |
}; | |
function reverseWord(word) { | |
return word.split("").reverse().join(""); | |
}; | |
function reverseAllWords(sentence) { | |
let words = sentence.split(" "); | |
for (let i = 0; i < words.length; i++) { | |
words[i] = reverseWord(words[i]); | |
} | |
return words.join(" "); | |
}; | |
function replaceFirstOccurence(string, toBeReplaced, replaceWith) { | |
return string.replace(toBeReplaced, replaceWith); | |
}; | |
function replaceAllOccurrences(string, toBeReplaced, replaceWith) { | |
return string.split(toBeReplaced).join(replaceWith); | |
}; | |
function encode(string) { | |
let replacementObject = { "a": "@", "s": "$", "i": "!", "o":"0" }; | |
for (let key in replacementObject) { | |
string = replaceAllOccurrences(string, key, replacementObject[key]); | |
} | |
return string; | |
}; | |
function palindrome (str){ | |
return `${str} ${reverseWord(str)}`; | |
} | |
function pigLatin(sentence, character){ | |
return sentence.split(' ').join(character + ' '); | |
} | |
export {countCharacter, capitalizeFirstCharacterOfWords, reverseWord, reverseAllWords, replaceFirstOccurence, replaceAllOccurrences, encode, palindrome, pigLatin}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment